Skip to content

Commit

Permalink
fix(core): test executor should fail properly (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Apr 13, 2022
1 parent 53bdc17 commit e2db293
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 49 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ on:
push:
branches: [master, dev]

env:
NX_BRANCH: ${{ github.event.number }}
NX_RUN_GROUP: ${{ github.run_id }}

jobs:
build:
runs-on: ubuntu-latest
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
name: Run PR checks

on: pull_request


env:
NX_BRANCH: ${{ github.event.number }}
NX_RUN_GROUP: ${{ github.run_id }}

jobs:
build:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ name: Release

on: workflow_dispatch

env:
NX_BRANCH: ${{ github.event.number }}
NX_RUN_GROUP: ${{ github.run_id }}

jobs:
build:
runs-on: ubuntu-latest
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *'


env:
NX_BRANCH: main
NX_RUN_GROUP: smoke

jobs:
smoke:
Expand Down
2 changes: 1 addition & 1 deletion .releaserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
'@semantic-release/exec',
{
prepareCmd:
'npx ts-node tools/scripts/patch-package-versions ${nextRelease.version}',
'npx ts-node tools/scripts/patch-package-versions --version ${nextRelease.version} --project all',
publishCmd: [
'npx ts-node tools/scripts/publish-all ${nextRelease.version} ${nextRelease.channel}',
'nx deploy docs-site',
Expand Down
40 changes: 38 additions & 2 deletions e2e/core-e2e/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('nx-dotnet e2e', () => {
runCommand('git checkout -b "affected-tests"');
updateFile('package.json', (f) => {
const json = JSON.parse(f);
json.dependencies['@nrwl/angular'] = 'latest';
json.dependencies['@nrwl/angular'] = json.devDependencies['nx'];
return JSON.stringify(json);
});
runPackageManagerInstall();
Expand Down Expand Up @@ -162,7 +162,11 @@ describe('nx-dotnet e2e', () => {
`generate @nx-dotnet/core:app ${app} --language="C#" --template="webapi"`,
);
const promise = runNxCommandAsync(`lint ${app}`).then((x) => x.stderr);
await expect(promise).resolves.toContain('WHITESPACE');
await expect(promise).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining('WHITESPACE'),
}),
);
});
});

Expand Down Expand Up @@ -336,6 +340,7 @@ describe('nx-dotnet e2e', () => {
expect(slnFile).toContain(app + '-test');
});
});

describe('inferred targets', () => {
let api: string;
let projectFolder: string;
Expand Down Expand Up @@ -386,6 +391,37 @@ describe('nx-dotnet e2e', () => {
writeFileSync(join(projectFolder, 'project.json'), projectJsonContents);
});
});

describe('@nx-dotnet/core:test', () => {
it('should test with xunit', () => {
const appProject = uniq('app');
const testProject = `${appProject}-test`;
runNxCommand(
`generate @nx-dotnet/core:app ${appProject} --language="C#" --template="webapi" --test-runner xunit`,
);

expect(() => runNxCommand(`test ${testProject}`)).not.toThrow();

updateFile(
`apps/${testProject}/UnitTest1.cs`,
`using Xunit;
namespace Proj.${names(appProject).className}.Test;
public class UnitTest1
{
// This test should fail, as the e2e test is checking for test failures.
[Fact]
public void Test1()
{
Assert.Equal(1, 2)
}
}`,
);

expect(() => runNxCommand(`test ${testProject}`)).toThrow();
});
});
});

function initializeGitRepo(cwd: string) {
Expand Down
4 changes: 4 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
{
"target": "prebuild",
"projects": "self"
},
{
"target": "prebuild",
"projects": "dependencies"
}
]
},
Expand Down
4 changes: 3 additions & 1 deletion packages/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"prebuild": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": ["npx ts-node tools/scripts/patch-package-versions"]
"commands": [
"npx ts-node tools/scripts/patch-package-versions --project core"
]
}
},
"build": {
Expand Down
31 changes: 18 additions & 13 deletions packages/core/src/executors/test/executor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExecutorContext } from '@nrwl/devkit';
import { ExecutorContext, logger } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';

import { resolve } from 'path';
Expand Down Expand Up @@ -29,18 +29,23 @@ export default async function runExecutor(
dotnetClient.cwd = projectDirectory;
const { watch, ...parsedOptions } = options;

const result = dotnetClient.test(
resolve(appRootPath, projectFilePath),
watch,
parsedOptions,
);
try {
const result = dotnetClient.test(
resolve(appRootPath, projectFilePath),
watch,
parsedOptions,
);

if (watch && isChildProcess(result)) {
await handleChildProcessPassthrough(result);
await rimraf(projectDirectory + '/bin');
await rimraf(projectDirectory + '/obj');
if (watch && isChildProcess(result)) {
await handleChildProcessPassthrough(result);
await rimraf(projectDirectory + '/bin');
await rimraf(projectDirectory + '/obj');
}
return {
success: true,
};
} catch (e) {
logger.error(e);
return { success: false };
}
return {
success: true,
};
}
1 change: 1 addition & 0 deletions packages/core/src/generators/app/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"description": "Which template should be used for creating the tests project?",
"default": "nunit",
"enum": ["nunit", "xunit", "mstest", "none"],
"aliases": ["testRunner"],
"x-prompt": {
"message": "Which template should be used for creating the tests project",
"type": "list",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/generators/lib/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"type": "string",
"description": "Which template should be used for creating the tests project?",
"default": "nunit",
"aliases": ["testRunner"],
"x-prompt": {
"message": "Which template should be used for creating the tests project",
"type": "list",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/generators/test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"description": "Which template should be used for creating the tests project?",
"default": "nunit",
"enum": ["nunit", "xunit", "mstest"],
"aliases": ["testRunner"],
"x-prompt": {
"message": "Which template should be used for creating the tests project",
"type": "list",
Expand Down
4 changes: 3 additions & 1 deletion packages/dotnet/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"prebuild": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": ["npx ts-node tools/scripts/patch-package-versions"]
"commands": [
"npx ts-node tools/scripts/patch-package-versions --project dotnet"
]
}
},
"build": {
Expand Down
4 changes: 3 additions & 1 deletion packages/dotnet/src/lib/core/dotnet.client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe('dotnet client', () => {
beforeEach(() => {
spawnSyncSpy = jest
.spyOn(cp, 'spawnSync')
.mockImplementation(jest.fn());
.mockReturnValue({ status: 0 } as Partial<
cp.SpawnSyncReturns<Buffer>
> as cp.SpawnSyncReturns<Buffer>);
});

afterEach(() => {
Expand Down
10 changes: 6 additions & 4 deletions packages/dotnet/src/lib/core/dotnet.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ export class DotNetClient {
}

private logAndExecute(params: string[]): void {
console.log(
`Executing Command: ${this.cliCommand.command} "${params.join('" "')}"`,
);
spawnSync(this.cliCommand.command, params, {
const cmd = `${this.cliCommand.command} "${params.join('" "')}"`;
console.log(`Executing Command: ${cmd}`);
const res = spawnSync(this.cliCommand.command, params, {
cwd: this.cwd || process.cwd(),
stdio: 'inherit',
});
if (res.status !== 0) {
throw new Error(`dotnet execution returned status code ${res.status}`);
}
}

private execute(params: string[]): Buffer {
Expand Down
4 changes: 3 additions & 1 deletion packages/nx-ghpages/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"prebuild": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": ["npx ts-node tools/scripts/patch-package-versions"]
"commands": [
"npx ts-node tools/scripts/patch-package-versions --project nx-ghpages"
]
}
},
"build": {
Expand Down
4 changes: 3 additions & 1 deletion packages/nxdoc/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"prebuild": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": ["npx ts-node tools/scripts/patch-package-versions"]
"commands": [
"npx ts-node tools/scripts/patch-package-versions --project nxdoc"
]
}
},
"build": {
Expand Down
4 changes: 3 additions & 1 deletion packages/typescript/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"prebuild": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": ["npx ts-node tools/scripts/patch-package-versions"]
"commands": [
"npx ts-node tools/scripts/patch-package-versions --project typescript"
]
}
},
"build": {
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"prebuild": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": ["npx ts-node tools/scripts/patch-package-versions"]
"commands": [
"npx ts-node tools/scripts/patch-package-versions --project utils"
]
}
},
"build": {
Expand Down
11 changes: 9 additions & 2 deletions tools/scripts/patch-package-versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import { execSync } from 'child_process';
import { join } from 'path';

import * as yargsParser from 'yargs-parser';

import {
existsSync,
getWorkspacePackages,
Expand All @@ -14,6 +16,7 @@ import {

export function PatchPackageVersions(
newVersion: string,
pkg: string,
updateGit = true,
prebuild = false,
) {
Expand All @@ -32,7 +35,10 @@ export function PatchPackageVersions(
});
}

const projects = Object.values(workspace.projects);
const projects =
pkg === 'all'
? Object.values(workspace.projects)
: [workspace.projects[pkg]];

projects.forEach((projectConfiguration, idx) => {
if (!projectConfiguration.targets?.build) {
Expand Down Expand Up @@ -89,5 +95,6 @@ function patchDependenciesSection(
}

if (require.main === module) {
PatchPackageVersions(process.argv[2], false, true);
const args = yargsParser(process.argv);
PatchPackageVersions(args.version, args.project, false, true);
}
2 changes: 1 addition & 1 deletion tools/scripts/publish-all/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function publishAll(version: string, tag = 'latest') {
stdio: 'inherit',
});

PatchPackageVersions(version, false);
PatchPackageVersions(version, 'all', false);

const projects = Object.values(workspace.projects);
const environment = {
Expand Down
6 changes: 5 additions & 1 deletion tools/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Workspaces } from '@nrwl/tao/src/shared/workspace';

import { readFileSync, statSync, writeFileSync } from 'fs';
import { join } from 'path';
import { format } from 'prettier';

export function existsSync(path: string) {
let results;
Expand All @@ -16,7 +17,10 @@ export function readJson(path: string) {
}

export function writeJson(path: string, object: any) {
return writeFileSync(path, JSON.stringify(object, null, 2));
const contents = format(JSON.stringify(object, null, 2), {
parser: 'json',
});
return writeFileSync(path, contents);
}

export function readWorkspaceJson() {
Expand Down

0 comments on commit e2db293

Please sign in to comment.