Skip to content

Commit 7883af4

Browse files
committed
test(@angular-devkit/schematics): add an example and test for custom tslint rules
Material and RxJS were asking for those. This Spec is used as an example until we get proper documentation.
1 parent f8debf3 commit 7883af4

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

packages/angular_devkit/schematics/tasks/tslint-fix/executor_spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,65 @@ describe('TsLintTaskExecutor', () => {
7070
).subscribe(undefined, done.fail, done);
7171
});
7272

73+
it('supports custom rules in the project (pass)', done => {
74+
const testRunner = new SchematicTestRunner(
75+
'@_/test',
76+
path.join(__dirname, 'test/collection.json'),
77+
);
78+
79+
const host = new TempScopedNodeJsSyncHost();
80+
host.write(normalize('/file.ts'), virtualFs.stringToFileBuffer(`
81+
console.log('hello world');
82+
`)).subscribe();
83+
const tree = new UnitTestTree(new HostTree(host));
84+
85+
const messages: string[] = [];
86+
87+
concat(
88+
testRunner.runSchematicAsync('custom-rule', { shouldPass: true }, tree),
89+
new Observable<void>(obs => {
90+
process.chdir(getSystemPath(host.root));
91+
testRunner.logger.subscribe(x => messages.push(x.message));
92+
testRunner.engine.executePostTasks().subscribe(obs);
93+
}),
94+
).subscribe(undefined, done.fail, done);
95+
});
96+
97+
it('supports custom rules in the project (fail)', done => {
98+
const testRunner = new SchematicTestRunner(
99+
'@_/test',
100+
path.join(__dirname, 'test/collection.json'),
101+
);
102+
103+
const host = new TempScopedNodeJsSyncHost();
104+
host.write(normalize('/file.ts'), virtualFs.stringToFileBuffer(`
105+
console.log('hello world');
106+
`)).subscribe();
107+
const tree = new UnitTestTree(new HostTree(host));
108+
109+
const messages: string[] = [];
110+
let error = false;
111+
112+
concat(
113+
testRunner.runSchematicAsync('custom-rule', { shouldPass: false }, tree),
114+
new Observable<void>(obs => {
115+
process.chdir(getSystemPath(host.root));
116+
testRunner.logger.subscribe(x => messages.push(x.message));
117+
testRunner.engine.executePostTasks().subscribe(obs);
118+
}).pipe(
119+
catchError(() => {
120+
error = true;
121+
122+
return [];
123+
}),
124+
),
125+
new Observable<void>(obs => {
126+
expect(messages.find(msg => /\bcustom-rule fail\b/.test(msg))).not.toBeUndefined();
127+
expect(error).toBe(true);
128+
129+
obs.complete();
130+
}),
131+
).subscribe(undefined, done.fail, done);
132+
});
133+
73134
});

packages/angular_devkit/schematics/tasks/tslint-fix/test/collection.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"schematics": {
3+
"custom-rule": {
4+
"description": ".",
5+
"factory": "./custom-rule"
6+
},
37
"run-task": {
48
"description": ".",
59
"factory": "./run-task"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {
9+
Rule,
10+
SchematicContext,
11+
Tree,
12+
} from '@angular-devkit/schematics'; // tslint:disable-line:no-implicit-dependencies
13+
import {
14+
TslintFixTask,
15+
} from '@angular-devkit/schematics/tasks'; // tslint:disable-line:no-implicit-dependencies
16+
import * as path from 'path';
17+
18+
export default function(options: { shouldPass: boolean }): Rule {
19+
return (_: Tree, context: SchematicContext) => {
20+
context.addTask(new TslintFixTask({
21+
rulesDirectory: path.join(__dirname, 'rules'),
22+
rules: {
23+
'custom-rule': [true, options.shouldPass],
24+
},
25+
}, {
26+
includes: '*.ts',
27+
silent: false,
28+
}));
29+
};
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
/**
3+
* @license
4+
* Copyright Google Inc. All Rights Reserved.
5+
*
6+
* Use of this source code is governed by an MIT-style license that can be
7+
* found in the LICENSE file at https://angular.io/license
8+
*/
9+
Object.defineProperty(exports, "__esModule", { value: true });
10+
const Lint = require('tslint');
11+
12+
13+
class Rule extends Lint.Rules.AbstractRule {
14+
apply(sourceFile) {
15+
const shouldPass = this.getOptions().ruleArguments[0];
16+
if (!shouldPass) {
17+
return [ new Lint.RuleFailure(sourceFile, 0, 0, 'custom-rule fail', this.ruleName) ];
18+
} else {
19+
return [];
20+
}
21+
}
22+
}
23+
24+
Rule.metadata = {
25+
ruleName: 'custom-rule',
26+
description: 'Test.',
27+
rationale: 'Do not use this.',
28+
options: [{ type: 'boolean' }],
29+
optionsDescription: '.',
30+
type: 'functionality',
31+
typescriptOnly: false,
32+
};
33+
34+
exports.Rule = Rule;

0 commit comments

Comments
 (0)