Skip to content

Commit

Permalink
Merge pull request #1077 from garritfra/feature/custom-spec-suffix
Browse files Browse the repository at this point in the history
Support custom spec file suffix
  • Loading branch information
kamilmysliwiec committed Jul 8, 2022
2 parents 3f319bf + 95bc2f7 commit f353c6c
Show file tree
Hide file tree
Showing 83 changed files with 703 additions and 22 deletions.
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';
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
File renamed without changes.
File renamed without changes.
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();
});
});

0 comments on commit f353c6c

Please sign in to comment.