Skip to content

Commit

Permalink
fix(core): #20 test template arg cannot be passed from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 28, 2021
1 parent 9273001 commit e9e47e0
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 33 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# [0.3.0-dev.1](https://github.com/nx-dotnet/nx-dotnet/compare/v0.2.1-dev.1...v0.3.0-dev.1) (2021-04-28)


### Bug Fixes

* **core:** [#20](https://github.com/nx-dotnet/nx-dotnet/issues/20) test template arg cannot be passed from command line ([f74900a](https://github.com/nx-dotnet/nx-dotnet/commit/f74900a1eac1d323813fda3f571112137b67c8ef))
* **repo:** semantic-release not updating package.json ([de9fcd9](https://github.com/nx-dotnet/nx-dotnet/commit/de9fcd9cf5accfe99f602a04cbfd9a44f72f1deb))


### Features

* **core:** dotnet test support ([adbb532](https://github.com/nx-dotnet/nx-dotnet/commit/adbb5328b7d80e02418d7b5fbf3aed54b3a0ee33))

## [0.2.1-dev.1](https://github.com/nx-dotnet/nx-dotnet/compare/v0.2.0...v0.2.1-dev.1) (2021-04-27)


Expand Down
51 changes: 49 additions & 2 deletions packages/core/src/executors/test/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
import { ExecutorContext } from '@nrwl/devkit';

import { mkdirSync, writeFileSync } from 'fs';

import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
import { rimraf } from '@nx-dotnet/utils';

import executor from './executor';
import { TestExecutorSchema } from './schema';

const options: TestExecutorSchema = {};
const root = process.cwd() + '/tmp';

jest.mock('../../../../dotnet/src/lib/core/dotnet.client');

describe('Test Executor', () => {
it('can run', async () => {
const output = await executor(options);
let context: ExecutorContext;
let dotnetClient: DotNetClient;

beforeEach(() => {
context = {
root: root,
cwd: root,
projectName: 'my-app',
targetName: 'build',
workspace: {
version: 2,
projects: {
'my-app': {
root: `${root}/apps/my-app`,
sourceRoot: `${root}/apps/my-app`,
targets: {
build: {
executor: '@nx-dotnet/core:build',
},
},
},
},
},
isVerbose: false,
};
dotnetClient = new DotNetClient(mockDotnetFactory());
});

afterEach(async () => {
await rimraf(root);
});

it('runs dotnet test', async () => {
const srcRoot = context.workspace.projects['my-app'].sourceRoot as string;
mkdirSync(srcRoot, { recursive: true });
writeFileSync(srcRoot + '/test.csproj', '');
const output = await executor(options, context, dotnetClient);
expect(output.success).toBe(true);
const mock = dotnetClient as jest.Mocked<DotNetClient>;
expect(mock.test).toHaveBeenCalledTimes(1);
});
});
2 changes: 2 additions & 0 deletions packages/core/src/executors/test/executor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExecutorContext } from '@nrwl/devkit';

import {
DotNetClient,
dotnetFactory,
Expand All @@ -8,6 +9,7 @@ import {
getExecutedProjectConfiguration,
getProjectFileForNxProject,
} from '@nx-dotnet/utils';

import { TestExecutorSchema } from './schema';

export default async function runExecutor(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generators/app/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('nx-dotnet library generator', () => {
name: 'test',
language: 'C#',
template: 'webapi',
'test-template': 'none',
testTemplate: 'none',
skipOutputPathManipulation: true,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generators/lib/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('nx-dotnet library generator', () => {
name: 'test',
language: 'C#',
template: 'classlib',
'test-template': 'none',
testTemplate: 'none',
skipOutputPathManipulation: true,
};

Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/generators/utils/generate-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('nx-dotnet project generator', () => {
name: 'test',
language: 'C#',
template: 'classlib',
'test-template': 'none',
testTemplate: 'none',
skipOutputPathManipulation: true,
};

Expand All @@ -46,7 +46,7 @@ describe('nx-dotnet project generator', () => {
await GenerateProject(appTree, options, dotnetClient, 'library');
const config = readProjectConfiguration(appTree, 'test');
expect(config.targets.serve).not.toBeDefined();
})
});

it('should tag generated projects', async () => {
await GenerateProject(appTree, options, dotnetClient, 'library');
Expand Down Expand Up @@ -76,7 +76,14 @@ describe('nx-dotnet project generator', () => {
await GenerateProject(appTree, options, dotnetClient, 'application');
const config = readProjectConfiguration(appTree, 'test');
expect(config.targets.serve).toBeDefined();
})
});

it('should generate test project', async () => {
options.testTemplate = 'nunit';
await GenerateProject(appTree, options, dotnetClient, 'application');
const config = readProjectConfiguration(appTree, 'test');
expect(config.targets.serve).toBeDefined();
});

/**
* This test requires a live dotnet client.
Expand All @@ -101,7 +108,6 @@ describe('nx-dotnet project generator', () => {
?.childNamed('OutputPath')?.val as string;
expect(outputPath).toBeTruthy();

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const absoluteDistPath = resolve(config.root, outputPath);
const expectedDistPath = resolve('./dist/libs/test');

Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/generators/utils/generate-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ async function GenerateTestProject(
dotnetClient: DotNetClient,
projectType: ProjectType
) {
const testName = schema.name + '-test';
const testRoot = schema.projectRoot + '-test';
const testProjectName = schema.projectName + '-test';

Expand Down Expand Up @@ -115,11 +114,11 @@ async function GenerateTestProject(

if (isDryRun()) {
newParams.push({
flag: 'dry-run',
flag: 'dryRun',
});
}

dotnetClient.new(schema['test-template'], newParams);
dotnetClient.new(schema.testTemplate, newParams);

if (!isDryRun() && !schema.skipOutputPathManipulation) {
const testCsProj = await findProjectFileInPath(testRoot);
Expand Down Expand Up @@ -177,7 +176,7 @@ export async function GenerateProject(
) {
initSchematic(host);

options['test-template'] = options['test-template'] ?? 'none';
options.testTemplate = options.testTemplate ?? 'none';

const normalizedOptions = normalizeOptions(host, options, projectType);

Expand Down Expand Up @@ -218,13 +217,13 @@ export async function GenerateProject(

if (isDryRun()) {
newParams.push({
flag: 'dry-run',
flag: 'dryRun',
});
}

dotnetClient.new(normalizedOptions.template, newParams);

if (options['test-template'] !== 'none') {
if (options['testTemplate'] !== 'none') {
await GenerateTestProject(
normalizedOptions,
host,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/models/project-generator-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export interface NxDotnetProjectGeneratorSchema {
directory?: string;
template: string;
language: string;
'test-template': 'nunit' | 'mstest' | 'xunit' | 'none';
testTemplate: 'nunit' | 'mstest' | 'xunit' | 'none';
skipOutputPathManipulation: boolean;
}
25 changes: 19 additions & 6 deletions packages/dotnet/src/lib/core/dotnet.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@nx-dotnet/utils';

import {
buildKeyMap,
dotnetBuildOptions,
dotnetNewOptions,
dotnetRunOptions,
Expand All @@ -19,20 +20,32 @@ export class DotNetClient {
constructor(private cliCommand: LoadedCLI) {}

new(template: dotnetTemplate, parameters?: dotnetNewOptions): Buffer {
const paramString = parameters ? getParameterString(parameters) : '';
const cmd = `${this.cliCommand.command} new ${template} ${paramString}`;
let cmd = `${this.cliCommand.command} new ${template}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(parameters, 'flag', testKeyMap);
const paramString = parameters ? getParameterString(parameters) : '';
cmd = `${cmd} ${paramString}`;
}
return this.logAndExecute(cmd);
}

build(project: string, parameters?: dotnetBuildOptions): Buffer {
const paramString = parameters ? getParameterString(parameters) : '';
const cmd = `${this.cliCommand.command} build ${project} ${paramString}`;
let cmd = `${this.cliCommand.command} build ${project}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(parameters, 'flag', buildKeyMap);
const paramString = parameters ? getParameterString(parameters) : '';
cmd = `${cmd} ${paramString}`;
}
return this.logAndExecute(cmd);
}

run(project: string, parameters?: dotnetRunOptions): ChildProcess {
const paramString = parameters ? getParameterString(parameters) : '';
const cmd = `run --project ${project} ${paramString}`;
let cmd = `run --project ${project} `;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(parameters, 'flag', testKeyMap);
const paramString = parameters ? getParameterString(parameters) : '';
cmd = `${cmd} ${paramString}`;
}
console.log(`Executing Command: ${cmd}`);
return spawn(this.cliCommand.command, cmd.split(' '), { stdio: 'inherit' });
}
Expand Down
15 changes: 11 additions & 4 deletions packages/dotnet/src/lib/models/dotnet-build/dotnet-build-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ export type dotnetBuildFlags =
| 'configuration'
| 'framework'
| 'force'
| 'no-dependencies'
| 'no-incremental'
| 'no-restore'
| 'noDependencies'
| 'noIncremental'
| 'noRestore'
| 'nologo'
| 'output'
| 'source'
| 'verbosity'
| 'version-suffix'
| 'versionSuffix'
| 'runtime';

export const buildKeyMap: Partial<{ [key in dotnetBuildFlags]: string }> = {
noRestore: 'no-restore',
noIncremental: 'no-incremental',
noDependencies: 'no-dependencies',
versionSuffix: 'version-suffix',
};
15 changes: 11 additions & 4 deletions packages/dotnet/src/lib/models/dotnet-new/dotnet-new-flags.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
export type dotnetNewFlags =
| 'dry-run'
| 'dryRun'
| 'force'
| 'language'
| 'name'
| 'install'
| 'nuget-source'
| 'nugetSource'
| 'output'
| 'uninstall'
| 'update-apply'
| 'update-check';
| 'updateApply'
| 'updateCheck';

export const newKeyMap: Partial<{ [key in dotnetNewFlags]: string }> = {
dryRun: 'dry-run',
updateApply: 'update-apply',
updateCheck: 'update-check',
nugetSource: 'nuget-source',
};
15 changes: 11 additions & 4 deletions packages/dotnet/src/lib/models/dotnet-run/dotnet-run-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ export type dotnetRunFlags =
| 'configuration'
| 'framework'
| 'force'
| 'no-dependencies'
| 'no-incremental'
| 'no-restore'
| 'noDependencies'
| 'noIncremental'
| 'noRestore'
| 'nologo'
| 'output'
| 'source'
| 'verbosity'
| 'version-suffix'
| 'versionSuffix'
| 'runtime';

export const runKeyMap: Partial<{ [key in dotnetRunFlags]: string }> = {
noDependencies: 'no-dependencies',
noRestore: 'no-restore',
versionSuffix: 'version-suffix',
noIncremental: 'no-incremental',
};

0 comments on commit e9e47e0

Please sign in to comment.