|
| 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 | + |
| 9 | +import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node'; |
| 10 | +import { Architect, Target } from '@angular-devkit/architect/src/index2'; |
| 11 | +import { TestingArchitectHost } from '@angular-devkit/architect/testing/index2'; |
| 12 | +import { experimental, join, logging, normalize, schema } from '@angular-devkit/core'; |
| 13 | +import { NodeJsSyncHost } from '@angular-devkit/core/node'; |
| 14 | +import * as fs from 'fs'; |
| 15 | +import * as path from 'path'; |
| 16 | + |
| 17 | +const devkitRoot = normalize((global as any)._DevKitRoot); // tslint:disable-line:no-any |
| 18 | +const workspaceRoot = join(devkitRoot, 'tests/angular_devkit/build_angular/hello-world-app/'); |
| 19 | +const lintTarget: Target = { project: 'app', target: 'lint' }; |
| 20 | + |
| 21 | +// tslint:disable-next-line:no-big-function |
| 22 | +describe('Tslint Target', () => { |
| 23 | + // const filesWithErrors = { 'src/foo.ts': 'const foo = "";\n' }; |
| 24 | + let testArchitectHost: TestingArchitectHost; |
| 25 | + let architect: Architect; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + const vfHost = new NodeJsSyncHost(); |
| 29 | + const configContent = fs.readFileSync(path.join(workspaceRoot, 'angular.json'), 'utf-8'); |
| 30 | + const workspaceJson = JSON.parse(configContent); |
| 31 | + |
| 32 | + const registry = new schema.CoreSchemaRegistry(); |
| 33 | + registry.addPostTransform(schema.transforms.addUndefinedDefaults); |
| 34 | + |
| 35 | + const workspace = new experimental.workspace.Workspace(workspaceRoot, vfHost); |
| 36 | + await workspace.loadWorkspaceFromJson(workspaceJson).toPromise(); |
| 37 | + |
| 38 | + testArchitectHost = new TestingArchitectHost( |
| 39 | + workspaceRoot, workspaceRoot, |
| 40 | + new WorkspaceNodeModulesArchitectHost(workspace, workspaceRoot), |
| 41 | + ); |
| 42 | + architect = new Architect(testArchitectHost, registry); |
| 43 | + }); |
| 44 | + |
| 45 | + it('works', async () => { |
| 46 | + const run = await architect.scheduleTarget({ project: 'app', target: 'lint' }); |
| 47 | + const output = await run.result; |
| 48 | + expect(output.success).toBe(true); |
| 49 | + await run.stop(); |
| 50 | + }, 30000); |
| 51 | + |
| 52 | + it(`should show project name as status and in the logs`, async () => { |
| 53 | + // Check logs. |
| 54 | + const logger = new logging.Logger('lint-info'); |
| 55 | + const allLogs: string[] = []; |
| 56 | + logger.subscribe(entry => allLogs.push(entry.message)); |
| 57 | + |
| 58 | + const run = await architect.scheduleTarget(lintTarget, {}, { logger }); |
| 59 | + |
| 60 | + // Check status updates. |
| 61 | + const allStatus: string[] = []; |
| 62 | + run.progress.subscribe(progress => { |
| 63 | + if (progress.status !== undefined) { |
| 64 | + allStatus.push(progress.status); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + const output = await run.result; |
| 69 | + expect(output.success).toBe(true); |
| 70 | + expect(allStatus).toContain(jasmine.stringMatching(/linting.*"app".*/i)); |
| 71 | + expect(allLogs).toContain(jasmine.stringMatching(/linting.*"app".*/i)); |
| 72 | + await run.stop(); |
| 73 | + }); |
| 74 | + |
| 75 | + it(`should not show project name when formatter is non human readable`, async () => { |
| 76 | + const overrides = { |
| 77 | + format: 'checkstyle', |
| 78 | + }; |
| 79 | + |
| 80 | + // Check logs. |
| 81 | + const logger = new logging.Logger('lint-info'); |
| 82 | + const allLogs: string[] = []; |
| 83 | + logger.subscribe(entry => allLogs.push(entry.message)); |
| 84 | + |
| 85 | + const run = await architect.scheduleTarget(lintTarget, overrides, { logger }); |
| 86 | + |
| 87 | + // Check status updates. |
| 88 | + const allStatus: string[] = []; |
| 89 | + run.progress.subscribe(progress => { |
| 90 | + if (progress.status !== undefined) { |
| 91 | + allStatus.push(progress.status); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + const output = await run.result; |
| 96 | + expect(output.success).toBe(true); |
| 97 | + expect(allStatus).toContain(jasmine.stringMatching(/linting.*"app".*/i)); |
| 98 | + expect(allLogs).not.toContain(jasmine.stringMatching(/linting.*"app".*/i)); |
| 99 | + await run.stop(); |
| 100 | + }, 30000); |
| 101 | + |
| 102 | + // it('should report lint error once', (done) => { |
| 103 | + // host.writeMultipleFiles({'src/app/app.component.ts': 'const foo = "";\n' }); |
| 104 | + // const logger = new TestLogger('lint-error'); |
| 105 | + // |
| 106 | + // runTargetSpec(host, tslintTargetSpec, undefined, DefaultTimeout, logger).pipe( |
| 107 | + // tap((buildEvent) => expect(buildEvent.success).toBe(false)), |
| 108 | + // tap(() => { |
| 109 | + // // this is to make sure there are no duplicates |
| 110 | + // expect(logger.includes(`" should be \'\nERROR`)).toBe(false); |
| 111 | + // |
| 112 | + // expect(logger.includes(`" should be '`)).toBe(true); |
| 113 | + // expect(logger.includes(`Lint errors found in the listed files`)).toBe(true); |
| 114 | + // }), |
| 115 | + // ).toPromise().then(done, done.fail); |
| 116 | + // }, 30000); |
| 117 | + // |
| 118 | + // it('supports exclude with glob', (done) => { |
| 119 | + // host.writeMultipleFiles(filesWithErrors); |
| 120 | + // const overrides: Partial<TslintBuilderOptions> = { exclude: ['**/foo.ts'] }; |
| 121 | + // |
| 122 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 123 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 124 | + // ).toPromise().then(done, done.fail); |
| 125 | + // }, 30000); |
| 126 | + // |
| 127 | + // it('supports exclude with relative paths', (done) => { |
| 128 | + // host.writeMultipleFiles(filesWithErrors); |
| 129 | + // const overrides: Partial<TslintBuilderOptions> = { exclude: ['src/foo.ts'] }; |
| 130 | + // |
| 131 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 132 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 133 | + // ).toPromise().then(done, done.fail); |
| 134 | + // }, 30000); |
| 135 | + // |
| 136 | + // it(`supports exclude with paths starting with './'`, (done) => { |
| 137 | + // host.writeMultipleFiles(filesWithErrors); |
| 138 | + // const overrides: Partial<TslintBuilderOptions> = { exclude: ['./src/foo.ts'] }; |
| 139 | + // |
| 140 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 141 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 142 | + // ).toPromise().then(done, done.fail); |
| 143 | + // }, 30000); |
| 144 | + // |
| 145 | + // it('supports fix', (done) => { |
| 146 | + // host.writeMultipleFiles(filesWithErrors); |
| 147 | + // const overrides: Partial<TslintBuilderOptions> = { fix: true }; |
| 148 | + // |
| 149 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 150 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 151 | + // tap(() => { |
| 152 | + // const fileName = normalize('src/foo.ts'); |
| 153 | + // const content = virtualFs.fileBufferToString(host.scopedSync().read(fileName)); |
| 154 | + // expect(content).toContain(`const foo = '';`); |
| 155 | + // }), |
| 156 | + // ).toPromise().then(done, done.fail); |
| 157 | + // }, 30000); |
| 158 | + // |
| 159 | + // it('supports force', (done) => { |
| 160 | + // host.writeMultipleFiles(filesWithErrors); |
| 161 | + // const logger = new TestLogger('lint-force'); |
| 162 | + // const overrides: Partial<TslintBuilderOptions> = { force: true }; |
| 163 | + // |
| 164 | + // runTargetSpec(host, tslintTargetSpec, overrides, DefaultTimeout, logger).pipe( |
| 165 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 166 | + // tap(() => { |
| 167 | + // expect(logger.includes(`" should be '`)).toBe(true); |
| 168 | + // expect(logger.includes(`Lint errors found in the listed files`)).toBe(true); |
| 169 | + // }), |
| 170 | + // ).toPromise().then(done, done.fail); |
| 171 | + // }, 30000); |
| 172 | + // |
| 173 | + // it('supports format', (done) => { |
| 174 | + // host.writeMultipleFiles(filesWithErrors); |
| 175 | + // const logger = new TestLogger('lint-format'); |
| 176 | + // const overrides: Partial<TslintBuilderOptions> = { format: 'stylish' }; |
| 177 | + // |
| 178 | + // runTargetSpec(host, tslintTargetSpec, overrides, DefaultTimeout, logger).pipe( |
| 179 | + // tap((buildEvent) => expect(buildEvent.success).toBe(false)), |
| 180 | + // tap(() => { |
| 181 | + // expect(logger.includes(`quotemark`)).toBe(true); |
| 182 | + // }), |
| 183 | + // ).toPromise().then(done, done.fail); |
| 184 | + // }, 30000); |
| 185 | + // |
| 186 | + // it('supports finding configs', (done) => { |
| 187 | + // host.writeMultipleFiles({ |
| 188 | + // 'src/app/foo/foo.ts': `const foo = '';\n`, |
| 189 | + // 'src/app/foo/tslint.json': ` |
| 190 | + // { |
| 191 | + // "rules": { |
| 192 | + // "quotemark": [ |
| 193 | + // true, |
| 194 | + // "double" |
| 195 | + // ] |
| 196 | + // } |
| 197 | + // } |
| 198 | + // `, |
| 199 | + // }); |
| 200 | + // const overrides: Partial<TslintBuilderOptions> = { tslintConfig: undefined }; |
| 201 | + // |
| 202 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 203 | + // tap((buildEvent) => expect(buildEvent.success).toBe(false)), |
| 204 | + // ).toPromise().then(done, done.fail); |
| 205 | + // }, 30000); |
| 206 | + // |
| 207 | + // it('supports overriding configs', (done) => { |
| 208 | + // host.writeMultipleFiles({ |
| 209 | + // 'src/app/foo/foo.ts': `const foo = '';\n`, |
| 210 | + // 'src/app/foo/tslint.json': ` |
| 211 | + // { |
| 212 | + // "rules": { |
| 213 | + // "quotemark": [ |
| 214 | + // true, |
| 215 | + // "double" |
| 216 | + // ] |
| 217 | + // } |
| 218 | + // } |
| 219 | + // `, |
| 220 | + // }); |
| 221 | + // const overrides: Partial<TslintBuilderOptions> = { tslintConfig: 'tslint.json' }; |
| 222 | + // |
| 223 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 224 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 225 | + // ).toPromise().then(done, done.fail); |
| 226 | + // }, 30000); |
| 227 | + // |
| 228 | + // it('supports using files with no project', (done) => { |
| 229 | + // const overrides: Partial<TslintBuilderOptions> = { |
| 230 | + // tsConfig: undefined, |
| 231 | + // files: ['src/app/**/*.ts'], |
| 232 | + // }; |
| 233 | + // |
| 234 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 235 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 236 | + // ).toPromise().then(done, done.fail); |
| 237 | + // }, 30000); |
| 238 | + // |
| 239 | + // it('supports using one project as a string', (done) => { |
| 240 | + // const overrides: Partial<TslintBuilderOptions> = { |
| 241 | + // tsConfig: 'src/tsconfig.app.json', |
| 242 | + // }; |
| 243 | + // |
| 244 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 245 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 246 | + // ).toPromise().then(done, done.fail); |
| 247 | + // }, 30000); |
| 248 | + // |
| 249 | + // it('supports using one project as an array', (done) => { |
| 250 | + // const overrides: Partial<TslintBuilderOptions> = { |
| 251 | + // tsConfig: ['src/tsconfig.app.json'], |
| 252 | + // }; |
| 253 | + // |
| 254 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 255 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 256 | + // ).toPromise().then(done, done.fail); |
| 257 | + // }, 30000); |
| 258 | + // |
| 259 | + // it('supports using two projects', (done) => { |
| 260 | + // const overrides: Partial<TslintBuilderOptions> = { |
| 261 | + // tsConfig: ['src/tsconfig.app.json', 'src/tsconfig.spec.json'], |
| 262 | + // }; |
| 263 | + // |
| 264 | + // runTargetSpec(host, tslintTargetSpec, overrides).pipe( |
| 265 | + // tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 266 | + // ).toPromise().then(done, done.fail); |
| 267 | + // }, 30000); |
| 268 | + // |
| 269 | + // it('errors when type checking is used without a project', (done) => { |
| 270 | + // const overrides: Partial<TslintBuilderOptions> = { |
| 271 | + // tsConfig: undefined, |
| 272 | + // typeCheck: true, |
| 273 | + // }; |
| 274 | + // |
| 275 | + // runTargetSpec(host, tslintTargetSpec, overrides) |
| 276 | + // .subscribe(undefined, () => done(), done.fail); |
| 277 | + // }, 30000); |
| 278 | +}); |
0 commit comments