Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom spec file suffix #1077

Merged
merged 17 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/lib/class/class.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,38 @@ describe('Class Factory', () => {
'export class FooEntity {}\n',
);
});
it('should create a spec file', async () => {
const options: ClassOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('class', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.spec.ts'),
).not.toBeUndefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: ClassOptions = {
name: 'foo',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('class', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.test.ts'),
).toBeDefined();
});
});
11 changes: 10 additions & 1 deletion src/lib/class/class.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function transform(options: ClassOptions): ClassOptions {
const location: Location = new NameParser().parse(target);

target.name = normalizeToKebabOrSnakeCase(location.name);
target.specFileSuffix = normalizeToKebabOrSnakeCase(
options.specFileSuffix || 'spec',
);
if (target.name.includes('.')) {
target.className = strings.classify(target.name).replace('.', '');
} else {
Expand All @@ -52,7 +55,13 @@ function transform(options: ClassOptions): ClassOptions {
function generate(options: ClassOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
Copy link
Contributor Author

@garritfra garritfra Jun 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if using a static file extension may result in a bug when using JS. Let me know if this is fine, and I'll revert this.

const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix);
}),
template({
...strings,
...options,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/class/class.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface ClassOptions {
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
* @default "spec"
*/
specFileSuffix?: string;
/**
* Flag to indicate if a directory is created.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/class/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
},
"path": {
"type": "string",
"format": "path",
Expand Down
34 changes: 34 additions & 0 deletions src/lib/controller/controller.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,38 @@ describe('Controller Factory', () => {
'export class FooModule {}\n',
);
});
it('should create a spec file', async () => {
const options: ControllerOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('controller', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.controller.spec.ts'),
).not.toBeUndefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: ControllerOptions = {
name: 'foo',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('controller', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.controller.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.controller.test.ts'),
).not.toBeUndefined();
});
});
12 changes: 11 additions & 1 deletion src/lib/controller/controller.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function transform(source: ControllerOptions): ControllerOptions {
target.language =
target.language !== undefined ? target.language : DEFAULT_LANGUAGE;

target.specFileSuffix = normalizeToKebabOrSnakeCase(
source.specFileSuffix || 'spec',
);

target.path = target.flat
? target.path
: join(target.path as Path, target.name);
Expand All @@ -60,7 +64,13 @@ function transform(source: ControllerOptions): ControllerOptions {
function generate(options: ControllerOptions) {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix)
}),
template({
...strings,
...options,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/controller/controller.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export interface ControllerOptions {
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
* @default "spec"
*/
specFileSuffix?: string;
/**
* Flag to indicate if a directory is created.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/controller/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"type": "boolean",
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
}
},
"required": ["name"]
Expand Down
34 changes: 34 additions & 0 deletions src/lib/filter/filter.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,38 @@ describe('Filter Factory', () => {
'}\n',
);
});
it('should create a spec file', async () => {
const options: FilterOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('filter', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.filter.spec.ts'),
).not.toBeUndefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: FilterOptions = {
name: 'foo',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('filter', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.filter.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.filter.test.ts'),
).toBeDefined();
});
});
11 changes: 10 additions & 1 deletion src/lib/filter/filter.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function transform(options: FilterOptions): FilterOptions {
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.language = target.language !== undefined ? target.language : 'ts';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
options.specFileSuffix || 'spec',
);

target.path = target.flat
? target.path
Expand All @@ -42,7 +45,13 @@ function transform(options: FilterOptions): FilterOptions {
function generate(options: FilterOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix)
}),
template({
...strings,
...options,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/filter/filter.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface FilterOptions {
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
* @default "spec"
*/
specFileSuffix?: string;
/**
* Flag to indicate if a directory is created.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/filter/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"type": "boolean",
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
}
},
"required": ["name"]
Expand Down
34 changes: 34 additions & 0 deletions src/lib/gateway/gateway.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,38 @@ describe('Gateway Factory', () => {
'}\n',
);
});
it('should create a spec file', async () => {
const options: GatewayOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('gateway', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.gateway.spec.ts'),
).toBeDefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: GatewayOptions = {
name: 'foo',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('gateway', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.gateway.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.gateway.test.ts'),
).toBeDefined();
});
});
10 changes: 10 additions & 0 deletions src/lib/gateway/gateway.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function transform(options: GatewayOptions): GatewayOptions {
}
target.metadata = 'providers';
target.type = 'gateway';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
options.specFileSuffix || 'spec',
);

const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
Expand All @@ -62,6 +65,13 @@ function generate(options: GatewayOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix)
}),
template({
...strings,
...options,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/gateway/gateway.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface GatewayOptions {
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
* @default "spec"
*/
specFileSuffix?: string;
/**
* Flag to indicate if a directory is created.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/gateway/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"type": "boolean",
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
}
},
"required": ["name"]
Expand Down
34 changes: 34 additions & 0 deletions src/lib/guard/guard.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,38 @@ describe('Guard Factory', () => {
'}\n',
);
});
it('should create a spec file', async () => {
const options: GuardOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.guard.spec.ts'),
).not.toBeUndefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: GuardOptions = {
name: 'foo',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.guard.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.guard.test.ts'),
).not.toBeUndefined();
});
});