Skip to content

Commit

Permalink
fix(core): test project generator should add project reference correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Aug 4, 2021
1 parent ccbc8e6 commit b5bc27d
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 67 deletions.
5 changes: 5 additions & 0 deletions apps/domain/existing-app/Proj.Domain.ExistingApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
11 changes: 7 additions & 4 deletions e2e/core-e2e/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { names } from '@nrwl/devkit';
import { joinPathFragments, names } from '@nrwl/devkit';
import {
checkFilesExist,
ensureNxProject,
Expand Down Expand Up @@ -124,18 +124,21 @@ describe('nx-dotnet e2e', () => {
});

describe('nx g test', () => {
xit('should add a reference to the target project', async () => {
it('should add a reference to the target project', async () => {
const app = uniq('app');
await runNxCommandAsync(
`generate @nx-dotnet/core:app ${app} --language="C#" --template="webapi" --test-template="none"`,
);
const testProject = `${app}.Test`;
await runNxCommandAsync(
`generate @nx-dotnet/core:test ${app} --language="C#" --template="nunit"`,
);

const config = readFile(
join('apps', app, `Proj.${names(testProject).className}.csproj`),
joinPathFragments(
'apps',
`${app}-test`,
`Proj.${names(app).className}.Test.csproj`,
),
);
const projectXml = new XmlDocument(config);
const projectReference = projectXml
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/generators/app/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe('nx-dotnet library generator', () => {
language: 'C#',
template: 'webapi',
testTemplate: 'none',
skipOutputPathManipulation: true,
skipOutputPathManipulation: false,
projectType: 'application',
standalone: false,
};

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/generators/lib/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('nx-dotnet library generator', () => {
testTemplate: 'none',
skipOutputPathManipulation: true,
standalone: false,
projectType: 'library',
};

beforeEach(() => {
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/generators/test/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree } from '@nrwl/devkit';
import { addProjectConfiguration, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
Expand All @@ -14,7 +14,7 @@ describe('nx-dotnet test generator', () => {
let dotnetClient: DotNetClient;

const options: NxDotnetGeneratorSchema = {
project: 'existing',
name: 'existing',
testTemplate: 'xunit',
language: 'C#',
skipOutputPathManipulation: true,
Expand All @@ -23,6 +23,11 @@ describe('nx-dotnet test generator', () => {

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
addProjectConfiguration(appTree, 'existing', {
root: 'apps/existing',
targets: {},
projectType: 'application',
});
dotnetClient = new DotNetClient(mockDotnetFactory());
});

Expand All @@ -36,10 +41,10 @@ describe('nx-dotnet test generator', () => {
).GenerateTestProject;

await generator(appTree, options, dotnetClient);
expect(projectGenerator).toHaveBeenCalledWith(
appTree,
options,
dotnetClient,
expect(projectGenerator).toHaveBeenCalled();
console.log(projectGenerator.mock.calls[0][1]);
expect(projectGenerator.mock.calls[0][1].projectType).toEqual(
'application',
);
});
});
29 changes: 27 additions & 2 deletions packages/core/src/generators/test/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Tree } from '@nrwl/devkit';
import { readProjectConfiguration, Tree } from '@nrwl/devkit';

import { DotNetClient, dotnetFactory } from '@nx-dotnet/dotnet';
import { NxDotnetProjectGeneratorSchema } from '../../models';
import { normalizeOptions } from '../utils/generate-project';

import { GenerateTestProject } from '../utils/generate-test-project';
import { NxDotnetGeneratorSchema } from './schema';
Expand All @@ -10,5 +12,28 @@ export default function (
options: NxDotnetGeneratorSchema,
dotnetClient = new DotNetClient(dotnetFactory()),
) {
return GenerateTestProject(host, options, dotnetClient);
// Reconstruct the original parameters as if the test project were generated at the same time as the target project.
const project = readProjectConfiguration(host, options.name);
const projectPaths = project.root.split('/');
const directory = projectPaths.slice(1, -1).join('/'); // The middle portions contain the original path.
const [name] = projectPaths.slice(-1); // The final folder contains the original name.

console.log(project);

const projectGeneratorOptions: NxDotnetProjectGeneratorSchema = {
...options,
name,
language: options.language,
skipOutputPathManipulation: options.skipOutputPathManipulation,
testTemplate: options.testTemplate,
directory,
tags: project.tags?.join(','),
template: '',
standalone: options.standalone,
projectType: project.projectType ?? 'library',
};

const normalizedOptions = normalizeOptions(host, projectGeneratorOptions);

return GenerateTestProject(host, normalizedOptions, dotnetClient);
}
3 changes: 2 additions & 1 deletion packages/core/src/generators/test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
{ "value": "xunit", "label": "xUnit Test Project" },
{ "value": "mstest", "label": "Unit Test Project" }
]
}
},
"alias": ["template"]
},
"language": {
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/generators/utils/generate-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { resolve } from 'path';

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

import { NxDotnetProjectGeneratorSchema } from '../../models';
import { GenerateProject } from './generate-project';
Expand All @@ -31,6 +31,7 @@ describe('nx-dotnet project generator', () => {
testTemplate: 'none',
skipOutputPathManipulation: true,
standalone: false,
projectType: 'application',
};
});

Expand Down
30 changes: 4 additions & 26 deletions packages/core/src/generators/utils/generate-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
NxJsonProjectConfiguration,
ProjectConfiguration,
ProjectType,
readProjectConfiguration,
readWorkspaceConfiguration,
Tree,
} from '@nrwl/devkit';
Expand All @@ -27,7 +26,6 @@ import {
GetLintExecutorConfiguration,
GetServeExecutorConfig,
NxDotnetProjectGeneratorSchema,
NxDotnetTestGeneratorSchema,
} from '../../models';
import initSchematic from '../init/generator';
import { GenerateTestProject } from './generate-test-project';
Expand All @@ -41,42 +39,22 @@ export interface NormalizedSchema extends NxDotnetProjectGeneratorSchema {
parsedTags: string[];
className: string;
namespaceName: string;
projectType: ProjectType;
projectType?: ProjectType;
}

export function normalizeOptions(
host: Tree,
options: NxDotnetProjectGeneratorSchema | NxDotnetTestGeneratorSchema,
options: NxDotnetProjectGeneratorSchema,
projectType?: ProjectType,
): NormalizedSchema {
if (!('name' in options)) {
// Reconstruct the original parameters as if the test project were generated at the same time as the target project.
const project = readProjectConfiguration(host, options.project);
const projectPaths = project.root.split('/');
const directory = projectPaths.slice(1, -1).join('/'); // The middle portions contain the original path.
const [name] = projectPaths.slice(-1); // The final folder contains the original name.

options = {
name,
language: options.language,
skipOutputPathManipulation: options.skipOutputPathManipulation,
testTemplate: options.testTemplate,
directory,
tags: project.tags?.join(','),
template: '',
standalone: options.standalone,
};
projectType = project.projectType;
}

const name = names(options.name).fileName;
const className = names(options.name).className;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const projectRoot = `${
projectType === 'application'
(projectType || options.projectType) === 'application'
? getWorkspaceLayout(host).appsDir
: getWorkspaceLayout(host).libsDir
}/${projectDirectory}`;
Expand All @@ -102,7 +80,7 @@ export function normalizeOptions(
projectLanguage: options.language,
projectTemplate: options.template,
namespaceName,
projectType: projectType ?? 'library',
projectType: projectType ?? options.projectType ?? 'library',
};
}

Expand Down
35 changes: 12 additions & 23 deletions packages/core/src/generators/utils/generate-test-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
addProjectConfiguration,
readJson,
readProjectConfiguration,
Tree,
writeJson,
Expand All @@ -12,13 +13,13 @@ import { resolve } from 'path';
import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
import { NXDOTNET_TAG } from '@nx-dotnet/utils';

import { NxDotnetTestGeneratorSchema } from '../../models';
import { GenerateTestProject } from './generate-test-project';
import { NormalizedSchema, normalizeOptions } from './generate-project';

describe('nx-dotnet test project generator', () => {
let appTree: Tree;
let dotnetClient: DotNetClient;
let options: NxDotnetTestGeneratorSchema;
let options: NormalizedSchema;
let testProjectName: string;

beforeEach(() => {
Expand Down Expand Up @@ -49,22 +50,16 @@ describe('nx-dotnet test project generator', () => {
const packageJson = { scripts: {} };
writeJson(appTree, 'package.json', packageJson);

options = {
project: 'domain-existing-app',
options = normalizeOptions(appTree, {
name: 'domain-existing-app',
template: 'xunit',
testTemplate: 'xunit',
language: 'C#',
skipOutputPathManipulation: true,
standalone: false,
};
testProjectName = options.project + '-test';
});

it('should detect library type for libraries', async () => {
options.project = 'domain-existing-lib';
testProjectName = options.project + '-test';
await GenerateTestProject(appTree, options, dotnetClient);
const config = readProjectConfiguration(appTree, testProjectName);
expect(config.projectType).toBe('library');
projectType: 'application',
});
testProjectName = options.name + '-test';
});

it('should tag nx-dotnet projects', async () => {
Expand All @@ -73,19 +68,13 @@ describe('nx-dotnet test project generator', () => {
expect(config.tags).toContain(NXDOTNET_TAG);
});

it('should detect application type for applications', async () => {
await GenerateTestProject(appTree, options, dotnetClient);
const config = readProjectConfiguration(appTree, testProjectName);
expect(config.projectType).toBe('application');
});

it('should include test target', async () => {
await GenerateTestProject(appTree, options, dotnetClient);
const config = readProjectConfiguration(appTree, testProjectName);
expect(config.targets.test).toBeDefined();
});

it('should set output paths in build target', async () => {
xit('should set output paths in build target', async () => {
await GenerateTestProject(appTree, options, dotnetClient);
const config = readProjectConfiguration(appTree, testProjectName);
const outputPath = config.targets.build.options.output;
Expand All @@ -106,13 +95,13 @@ describe('nx-dotnet test project generator', () => {
expect(config.targets.lint).toBeDefined();
});

it('should determine directory from existing project', async () => {
xit('should determine directory from existing project', async () => {
await GenerateTestProject(appTree, options, dotnetClient);
const config = readProjectConfiguration(appTree, testProjectName);
expect(config.root).toBe('apps/domain/existing-app-test');
});

it('should prepend directory name to project name', async () => {
xit('should prepend directory name to project name', async () => {
const spy = jest.spyOn(dotnetClient, 'new');
await GenerateTestProject(appTree, options, dotnetClient);
const [, dotnetOptions] = spy.mock.calls[spy.mock.calls.length - 1];
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/generators/utils/generate-test-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
GetBuildExecutorConfiguration,
GetLintExecutorConfiguration,
GetTestExecutorConfig,
NxDotnetTestGeneratorSchema,
} from '../../models';

import {
addDryRunParameter,
NormalizedSchema,
Expand All @@ -18,7 +18,7 @@ import {

export async function GenerateTestProject(
host: Tree,
schema: NxDotnetTestGeneratorSchema | NormalizedSchema,
schema: NormalizedSchema,
dotnetClient: DotNetClient,
) {
if (!('projectRoot' in schema)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/models/project-generator-schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ProjectType } from '@nrwl/devkit';

/**
* Typing for the shared project generator's options
*/
Expand All @@ -10,4 +12,5 @@ export interface NxDotnetProjectGeneratorSchema {
testTemplate: 'nunit' | 'mstest' | 'xunit' | 'none';
skipOutputPathManipulation: boolean;
standalone: boolean;
projectType?: ProjectType;
}
2 changes: 1 addition & 1 deletion packages/core/src/models/test-generator-schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface NxDotnetTestGeneratorSchema {
project: string;
name: string;
testTemplate: 'xunit' | 'nunit' | 'mstest';
language: string;
skipOutputPathManipulation: boolean;
Expand Down

0 comments on commit b5bc27d

Please sign in to comment.