2018-03-01 13:41:35 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2018-03-01 13:41:35 -05:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-03-02 17:19:01 -05:00
|
|
|
import * as child_process from 'child_process';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2021-04-08 03:26:53 -04:00
|
|
|
import {assertFileEqual} from './helpers';
|
2018-03-02 17:19:01 -05:00
|
|
|
|
2018-10-29 16:25:00 -04:00
|
|
|
const BINARY_PATH = require.resolve('../ts-api-guardian/bin/ts-api-guardian');
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
describe('cli: e2e test', () => {
|
2018-03-01 13:41:35 -05:00
|
|
|
const outDir = path.join(process.env['TEST_TMPDIR'], 'tmp');
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
if (!fs.existsSync(outDir)) {
|
|
|
|
fs.mkdirSync(outDir);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
2021-04-08 03:26:53 -04:00
|
|
|
fs.rmdirSync(outDir, {recursive: true});
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
it('should print usage without any argument', () => {
|
|
|
|
const {stderr} = execute([]);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stderr).toMatch(/Usage/);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should show help message with --help', () => {
|
|
|
|
const {stdout} = execute(['--help']);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stdout).toMatch(/Usage/);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate golden file with --out', () => {
|
|
|
|
const simpleFile = path.join(outDir, 'simple.d.ts');
|
2018-03-01 13:41:35 -05:00
|
|
|
const {status, stderr} = execute(['--out', simpleFile, 'test/fixtures/simple.d.ts']);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(status).toBe(0, stderr);
|
2018-03-02 17:19:01 -05:00
|
|
|
assertFileEqual(simpleFile, 'test/fixtures/simple_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should verify golden file with --verify and exit cleanly on no difference', () => {
|
|
|
|
const {stdout, status} =
|
|
|
|
execute(['--verify', 'test/fixtures/simple_expected.d.ts', 'test/fixtures/simple.d.ts']);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stdout).toBe('');
|
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should verify golden file with --verify and exit with error on difference', () => {
|
|
|
|
const {stdout, status} = execute(
|
|
|
|
['--verify', 'test/fixtures/verify_expected.d.ts', 'test/fixtures/verify_entrypoint.d.ts']);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stdout).toBe(fs.readFileSync('test/fixtures/verify.patch').toString());
|
|
|
|
expect(status).toBe(1);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate multiple golden files with --outDir and --rootDir', () => {
|
|
|
|
const {status} = execute([
|
|
|
|
'--outDir', outDir, '--rootDir', 'test/fixtures', 'test/fixtures/simple.d.ts',
|
|
|
|
'test/fixtures/sorting.d.ts'
|
|
|
|
]);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
assertFileEqual(path.join(outDir, 'simple.d.ts'), 'test/fixtures/simple_expected.d.ts');
|
|
|
|
assertFileEqual(path.join(outDir, 'sorting.d.ts'), 'test/fixtures/sorting_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should verify multiple golden files with --verifyDir and --rootDir', () => {
|
|
|
|
copyFile('test/fixtures/simple_expected.d.ts', path.join(outDir, 'simple.d.ts'));
|
|
|
|
copyFile('test/fixtures/sorting_expected.d.ts', path.join(outDir, 'sorting.d.ts'));
|
|
|
|
const {stdout, status} = execute([
|
|
|
|
'--verifyDir', outDir, '--rootDir', 'test/fixtures', 'test/fixtures/simple.d.ts',
|
|
|
|
'test/fixtures/sorting.d.ts'
|
|
|
|
]);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stdout).toBe('');
|
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate respecting --stripExportPattern', () => {
|
2018-10-29 16:25:00 -04:00
|
|
|
const {status} = execute([
|
2018-03-02 17:19:01 -05:00
|
|
|
'--out', path.join(outDir, 'underscored.d.ts'), '--stripExportPattern', '^__.*',
|
|
|
|
'test/fixtures/underscored.d.ts'
|
|
|
|
]);
|
2021-04-08 03:26:53 -04:00
|
|
|
|
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
assertFileEqual(
|
|
|
|
path.join(outDir, 'underscored.d.ts'), 'test/fixtures/underscored_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw for aliased stripped exports', () => {
|
2018-10-29 16:25:00 -04:00
|
|
|
const {status} = execute([
|
2018-03-02 17:19:01 -05:00
|
|
|
'--out', path.join(outDir, 'stripped_alias.d.ts'), '--stripExportPattern', '^__.*',
|
|
|
|
'test/fixtures/stripped_alias.d.ts'
|
|
|
|
]);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
assertFileEqual(
|
|
|
|
path.join(outDir, 'stripped_alias.d.ts'), 'test/fixtures/stripped_alias_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should verify respecting --stripExportPattern', () => {
|
|
|
|
const {stdout, status} = execute([
|
|
|
|
'--verify', 'test/fixtures/underscored_expected.d.ts', 'test/fixtures/underscored.d.ts',
|
|
|
|
'--stripExportPattern', '^__.*'
|
|
|
|
]);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stdout).toBe('');
|
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should respect --allowModuleIdentifiers', () => {
|
|
|
|
const {stdout, status} = execute([
|
|
|
|
'--verify', 'test/fixtures/module_identifier_expected.d.ts', '--allowModuleIdentifiers',
|
|
|
|
'foo', 'test/fixtures/module_identifier.d.ts'
|
|
|
|
]);
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(stdout).toBe('');
|
|
|
|
expect(status).toBe(0);
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function copyFile(sourceFile: string, targetFile: string) {
|
|
|
|
fs.writeFileSync(targetFile, fs.readFileSync(sourceFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute(args: string[]): {stdout: string, stderr: string, status: number} {
|
2018-10-29 16:25:00 -04:00
|
|
|
// We need to determine the directory that includes the `ts-api-guardian` npm_package that
|
|
|
|
// will be used to spawn the CLI binary. This is a workaround because technically we shouldn't
|
|
|
|
// spawn a child process that doesn't have the custom NodeJS module resolution for Bazel.
|
2019-04-26 04:08:04 -04:00
|
|
|
const nodePath = [
|
|
|
|
path.join(require.resolve('npm/node_modules/chalk/package.json'), '../../'),
|
|
|
|
path.join(require.resolve('../lib/cli.js'), '../../'),
|
|
|
|
].join(process.platform === 'win32' ? ';' : ':');
|
|
|
|
|
2018-10-29 16:25:00 -04:00
|
|
|
const output = child_process.spawnSync(process.execPath, [BINARY_PATH, ...args], {
|
2018-03-01 13:41:35 -05:00
|
|
|
env: {
|
2018-10-29 16:25:00 -04:00
|
|
|
'NODE_PATH': nodePath,
|
2018-03-01 13:41:35 -05:00
|
|
|
}
|
|
|
|
});
|
2021-04-08 03:26:53 -04:00
|
|
|
|
|
|
|
expect(output.error).toBeFalsy(`Child process failed or timed out: ${output.error}`);
|
|
|
|
expect(output.signal).toBeFalsy(`Child process killed by signal ${output.signal}`);
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
stdout: output.stdout.toString(),
|
|
|
|
stderr: output.stderr.toString(),
|
|
|
|
status: output.status
|
|
|
|
};
|
|
|
|
}
|