build: remove unused "test.sh" leftover code in compiler-cli (#28352)

Since we recently removed the `test.sh` script, and now run
all tests with Bazel, we can remove the unused logic that makes
compiler-cli tests pass in non-Bazel.

This cleans up the tests, and also makes it easier to write tests
without worrying about two ways of the Angular package output
(Bazel `ng_package` rules vs. old `build.sh` logic of building)

PR Close #28352
This commit is contained in:
Paul Gschwendtner 2019-01-25 19:44:49 +01:00 committed by Matias Niemelä
parent e11ac7f24b
commit b91a25bfb2
8 changed files with 31 additions and 289 deletions

View File

@ -6,27 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AotCompilerHost, AotSummaryResolver, CompileMetadataResolver, CompilerConfig, DEFAULT_INTERPOLATION_CONFIG, DirectiveNormalizer, DirectiveResolver, DomElementSchemaRegistry, HtmlParser, I18NHtmlParser, InterpolationConfig, JitSummaryResolver, Lexer, NgAnalyzedModules, NgModuleResolver, ParseTreeResult, Parser, PipeResolver, ResourceLoader, StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost, SummaryResolver, TemplateParser, analyzeNgModules, createOfflineCompileUrlResolver} from '@angular/compiler';
import {AotSummaryResolver, CompileMetadataResolver, CompilerConfig, DEFAULT_INTERPOLATION_CONFIG, DirectiveNormalizer, DirectiveResolver, DomElementSchemaRegistry, HtmlParser, I18NHtmlParser, InterpolationConfig, JitSummaryResolver, Lexer, NgAnalyzedModules, NgModuleResolver, ParseTreeResult, Parser, PipeResolver, ResourceLoader, StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost, SummaryResolver, TemplateParser, analyzeNgModules, createOfflineCompileUrlResolver} from '@angular/compiler';
import {ViewEncapsulation, ɵConsole as Console} from '@angular/core';
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {DiagnosticTemplateInfo} from '../../src/diagnostics/expression_diagnostics';
import {getClassFromStaticSymbol, getClassMembers, getPipesTable, getSymbolQuery} from '../../src/diagnostics/typescript_symbols';
import {getClassMembers, getPipesTable, getSymbolQuery} from '../../src/diagnostics/typescript_symbols';
import {Directory, MockAotContext} from '../mocks';
import {isInBazel, setup} from '../test_support';
function calculateAngularPath() {
if (isInBazel()) {
const support = setup();
return path.join(support.basePath, 'node_modules/@angular/*');
} else {
const moduleFilename = module.filename.replace(/\\/g, '/');
const distIndex = moduleFilename.indexOf('/dist/all');
return moduleFilename.substr(0, distIndex) + '/packages/*';
}
}
import {setup} from '../test_support';
const realFiles = new Map<string, string>();
@ -36,6 +25,8 @@ export class MockLanguageServiceHost implements ts.LanguageServiceHost {
private assumedExist = new Set<string>();
constructor(private scripts: string[], files: Directory, currentDirectory: string = '/') {
const support = setup();
this.options = {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
@ -49,7 +40,7 @@ export class MockLanguageServiceHost implements ts.LanguageServiceHost {
strictNullChecks: true,
baseUrl: currentDirectory,
lib: ['lib.es2015.d.ts', 'lib.dom.d.ts'],
paths: {'@angular/*': [calculateAngularPath()]}
paths: {'@angular/*': [path.join(support.basePath, 'node_modules/@angular/*')]}
};
this.context = new MockAotContext(currentDirectory, files);
}

View File

@ -8,17 +8,9 @@
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {mainXi18n} from '../src/extract_i18n';
import {isInBazel, makeTempDir, setup} from './test_support';
function getNgRootDir() {
const moduleFilename = module.filename.replace(/\\/g, '/');
const distIndex = moduleFilename.indexOf('/dist/all');
return moduleFilename.substr(0, distIndex);
}
import {makeTempDir, setup} from './test_support';
const EXPECTED_XMB = `<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE messagebundle [
@ -214,31 +206,10 @@ describe('extract_i18n command line', () => {
beforeEach(() => {
errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error);
if (isInBazel()) {
const support = setup();
write = (fileName: string, content: string) => { support.write(fileName, content); };
basePath = support.basePath;
outDir = path.join(basePath, 'built');
} else {
basePath = makeTempDir();
write = (fileName: string, content: string) => {
const dir = path.dirname(fileName);
if (dir !== '.') {
const newDir = path.join(basePath, dir);
if (!fs.existsSync(newDir)) fs.mkdirSync(newDir);
}
fs.writeFileSync(path.join(basePath, fileName), content, {encoding: 'utf-8'});
};
outDir = path.resolve(basePath, 'built');
const ngRootDir = getNgRootDir();
const nodeModulesPath = path.resolve(basePath, 'node_modules');
fs.mkdirSync(nodeModulesPath);
fs.symlinkSync(
path.resolve(ngRootDir, 'dist', 'all', '@angular'),
path.resolve(nodeModulesPath, '@angular'));
fs.symlinkSync(
path.resolve(ngRootDir, 'node_modules', 'rxjs'), path.resolve(nodeModulesPath, 'rxjs'));
}
const support = setup();
write = (fileName: string, content: string) => { support.write(fileName, content); };
basePath = support.basePath;
outDir = path.join(basePath, 'built');
write('tsconfig-base.json', `{
"compilerOptions": {
"experimentalDecorators": true,

View File

@ -11,14 +11,7 @@ import * as path from 'path';
import * as ts from 'typescript';
import {main, readCommandLineAndConfiguration, watchMode} from '../src/main';
import {isInBazel, makeTempDir, setup} from './test_support';
function getNgRootDir() {
const moduleFilename = module.filename.replace(/\\/g, '/');
const distIndex = moduleFilename.indexOf('/dist/all');
return moduleFilename.substr(0, distIndex);
}
import {setup} from './test_support';
describe('ngc transformer command-line', () => {
let basePath: string;
@ -44,33 +37,12 @@ describe('ngc transformer command-line', () => {
beforeEach(() => {
errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error);
if (isInBazel) {
const support = setup();
basePath = support.basePath;
outDir = path.join(basePath, 'built');
process.chdir(basePath);
write = (fileName: string, content: string) => { support.write(fileName, content); };
} else {
basePath = makeTempDir();
process.chdir(basePath);
write = (fileName: string, content: string) => {
const dir = path.dirname(fileName);
if (dir != '.') {
const newDir = path.join(basePath, dir);
if (!fs.existsSync(newDir)) fs.mkdirSync(newDir);
}
fs.writeFileSync(path.join(basePath, fileName), content, {encoding: 'utf-8'});
};
outDir = path.resolve(basePath, 'built');
const ngRootDir = getNgRootDir();
const nodeModulesPath = path.resolve(basePath, 'node_modules');
fs.mkdirSync(nodeModulesPath);
fs.symlinkSync(
path.resolve(ngRootDir, 'dist', 'all', '@angular'),
path.resolve(nodeModulesPath, '@angular'));
fs.symlinkSync(
path.resolve(ngRootDir, 'node_modules', 'rxjs'), path.resolve(nodeModulesPath, 'rxjs'));
}
const support = setup();
basePath = support.basePath;
outDir = path.join(basePath, 'built');
process.chdir(basePath);
write = (fileName: string, content: string) => { support.write(fileName, content); };
write('tsconfig-base.json', `{
"compilerOptions": {
"experimentalDecorators": true,
@ -269,19 +241,9 @@ describe('ngc transformer command-line', () => {
expect(exitCode).toEqual(0);
expect(fs.existsSync(path.resolve(outDir, 'mymodule.ngfactory.js'))).toBe(true);
if (isInBazel()) {
// In bazel we use the packaged version so the factory is at the root and we
// get the flattened factory.
expect(fs.existsSync(
path.resolve(outDir, 'node_modules', '@angular', 'core', 'core.ngfactory.js')))
.toBe(true);
} else {
expect(fs.existsSync(path.resolve(
outDir, 'node_modules', '@angular', 'core', 'src',
'application_module.ngfactory.js')))
.toBe(true);
}
expect(fs.existsSync(
path.resolve(outDir, 'node_modules', '@angular', 'core', 'core.ngfactory.js')))
.toBe(true);
});
describe('comments', () => {
@ -386,18 +348,9 @@ describe('ngc transformer command-line', () => {
const exitCode = main(['-p', path.join(basePath, 'tsconfig.json')], errorSpy);
expect(exitCode).toEqual(0);
expect(fs.existsSync(path.resolve(outDir, 'mymodule.ngfactory.js'))).toBe(true);
if (isInBazel()) {
// In bazel we use the packaged version so the factory is at the root and we
// get the flattened factory.
expect(fs.existsSync(
path.resolve(outDir, 'node_modules', '@angular', 'core', 'core.ngfactory.js')))
.toBe(true);
} else {
expect(fs.existsSync(path.resolve(
outDir, 'node_modules', '@angular', 'core', 'src',
'application_module.ngfactory.js')))
.toBe(true);
}
expect(fs.existsSync(
path.resolve(outDir, 'node_modules', '@angular', 'core', 'core.ngfactory.js')))
.toBe(true);
});
describe(`emit generated files depending on the source file`, () => {
@ -1217,10 +1170,6 @@ describe('ngc transformer command-line', () => {
}
`);
if (!isInBazel()) {
// This is not necessary in bazel as it uses the npm_package
expect(main(['-p', path.join(basePath, 'tsconfig-ng.json')], errorSpy)).toBe(0);
}
expect(main(['-p', path.join(basePath, 'lib1', 'tsconfig-lib1.json')], errorSpy)).toBe(0);
expect(main(['-p', path.join(basePath, 'lib2', 'tsconfig-lib2.json')], errorSpy)).toBe(0);
expect(main(['-p', path.join(basePath, 'app', 'tsconfig-app.json')], errorSpy)).toBe(0);
@ -1257,110 +1206,6 @@ describe('ngc transformer command-line', () => {
shouldExist('app/main.js');
});
if (!isInBazel()) {
// This is an unnecessary test bazel as it always uses flat modules
it('should be able to compile libraries with summaries and flat modules', () => {
writeFiles();
compile();
// libraries
// make `shouldExist` / `shouldNotExist` relative to `node_modules`
outDir = path.resolve(basePath, 'node_modules');
shouldExist('flat_module/index.ngfactory.js');
shouldExist('flat_module/index.ngsummary.json');
// app
// make `shouldExist` / `shouldNotExist` relative to `built`
outDir = path.resolve(basePath, 'built');
shouldExist('app/main.ngfactory.js');
const factory = fs.readFileSync(path.resolve(outDir, 'app/main.ngfactory.js')).toString();
// reference to the module itself
expect(factory).toMatch(/from "flat_module"/);
// no reference to a deep file
expect(factory).not.toMatch(/from "flat_module\//);
function writeFiles() {
createFlatModuleInNodeModules();
// Angular + flat module
write('tsconfig-lib.json', `{
"extends": "./tsconfig-base.json",
"angularCompilerOptions": {
"generateCodeForLibraries": true
},
"compilerOptions": {
"outDir": "."
},
"include": ["node_modules/@angular/core/**/*", "node_modules/flat_module/**/*"],
"exclude": [
"node_modules/@angular/core/test/**",
"node_modules/@angular/core/testing/**"
]
}`);
// Application
write('app/tsconfig-app.json', `{
"extends": "../tsconfig-base.json",
"angularCompilerOptions": {
"generateCodeForLibraries": false
},
"compilerOptions": {
"rootDir": ".",
"outDir": "../built/app"
}
}`);
write('app/main.ts', `
import {NgModule} from '@angular/core';
import {FlatModule} from 'flat_module';
@NgModule({
imports: [FlatModule]
})
export class AppModule {}
`);
}
function createFlatModuleInNodeModules() {
// compile the flat module
writeFlatModule('index.js');
expect(main(['-p', basePath], errorSpy)).toBe(0);
// move the flat module output into node_modules
const flatModuleNodeModulesPath = path.resolve(basePath, 'node_modules', 'flat_module');
fs.renameSync(outDir, flatModuleNodeModulesPath);
fs.renameSync(
path.resolve(basePath, 'src/flat.component.html'),
path.resolve(flatModuleNodeModulesPath, 'src/flat.component.html'));
// and remove the sources.
fs.renameSync(path.resolve(basePath, 'src'), path.resolve(basePath, 'flat_module_src'));
fs.unlinkSync(path.resolve(basePath, 'public-api.ts'));
// add a flatModuleIndexRedirect
write('node_modules/flat_module/redirect.metadata.json', `{
"__symbolic": "module",
"version": 3,
"metadata": {},
"exports": [
{
"from": "./index"
}
],
"flatModuleIndexRedirect": true,
"importAs": "flat_module"
}`);
write('node_modules/flat_module/redirect.d.ts', `export * from './index';`);
// add a package.json to use the redirect
write('node_modules/flat_module/package.json', `{"typings": "./redirect.d.ts"}`);
}
function compile() {
expect(main(['-p', path.join(basePath, 'tsconfig-lib.json')], errorSpy)).toBe(0);
expect(main(['-p', path.join(basePath, 'app', 'tsconfig-app.json')], errorSpy)).toBe(0);
}
});
}
describe('enableResourceInlining', () => {
it('should inline templateUrl and styleUrl in JS and metadata', () => {
writeConfig(`{

View File

@ -14,11 +14,6 @@ const Module = require('module');
import {mainNgcc} from '../../src/ngcc/src/main';
describe('ngcc main()', () => {
if (!isInBazel()) {
// These tests should be excluded from the non-Bazel build.
return;
}
beforeEach(createMockFileSystem);
afterEach(restoreRealFileSystem);
@ -100,10 +95,6 @@ interface Directory {
[pathSegment: string]: string|Directory;
}
function isInBazel() {
return process.env.TEST_SRCDIR != null;
}
function mockResolve(p: string): string|null {
if (existsSync(p)) {
const stat = statSync(p);
@ -122,4 +113,4 @@ function mockResolve(p: string): string|null {
}
}
return null;
}
}

View File

@ -14,7 +14,7 @@ import * as ts from 'typescript';
import {createCompilerHost, createProgram} from '../../ngtools2';
import {main, mainDiagnosticsForTest, readNgcCommandLineAndConfiguration} from '../../src/main';
import {LazyRoute} from '../../src/ngtsc/routing';
import {TestSupport, isInBazel, setup} from '../test_support';
import {setup, TestSupport} from '../test_support';
function setupFakeCore(support: TestSupport): void {
if (!process.env.TEST_SRCDIR) {
@ -42,10 +42,6 @@ export class NgtscTestEnvironment {
* Set up a new testing environment.
*/
static setup(): NgtscTestEnvironment {
if (!NgtscTestEnvironment.supported) {
throw new Error(`Attempting to setup ngtsc tests in an unsupported environment`);
}
const support = setup();
const outDir = path.join(support.basePath, 'built');
process.chdir(support.basePath);
@ -135,6 +131,4 @@ export class NgtscTestEnvironment {
const program = createProgram({rootNames, host, options});
return program.listLazyRoutes(entryPoint);
}
static get supported(): boolean { return isInBazel(); }
}

View File

@ -25,11 +25,6 @@ const contentQueryRegExp = (predicate: string, descend: boolean, ref?: string):
};
describe('ngtsc behavioral tests', () => {
if (!NgtscTestEnvironment.supported) {
// These tests should be excluded from the non-Bazel build.
return;
}
let env !: NgtscTestEnvironment;
beforeEach(() => { env = NgtscTestEnvironment.setup(); });

View File

@ -42,11 +42,6 @@ export declare class CommonModule {
}
describe('ngtsc type checking', () => {
if (!NgtscTestEnvironment.supported) {
// These tests should be excluded from the non-Bazel build.
return;
}
let env !: NgtscTestEnvironment;
beforeEach(() => {

View File

@ -7,26 +7,12 @@
*/
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as ts from 'typescript';
import * as ng from '../index';
// TEST_TMPDIR is set by bazel.
const tmpdir = process.env.TEST_TMPDIR || os.tmpdir();
function getNgRootDir() {
const moduleFilename = module.filename.replace(/\\/g, '/');
const distIndex = moduleFilename.indexOf('/dist/all');
return moduleFilename.substr(0, distIndex);
}
export function writeTempFile(name: string, contents: string): string {
const id = (Math.random() * 1000000).toFixed(0);
const fn = path.join(tmpdir, `tmp.${id}.${name}`);
fs.writeFileSync(fn, contents);
return fn;
}
// TEST_TMPDIR is always set by Bazel.
const tmpdir = process.env.TEST_TMPDIR!;
export function makeTempDir(): string {
let dir: string;
@ -143,36 +129,10 @@ export function setupBazelTo(basePath: string) {
}
}
function setupBazel(): TestSupport {
const basePath = makeTempDir();
setupBazelTo(basePath);
return createTestSupportFor(basePath);
}
function setupTestSh(): TestSupport {
const basePath = makeTempDir();
const ngRootDir = getNgRootDir();
const nodeModulesPath = path.resolve(basePath, 'node_modules');
fs.mkdirSync(nodeModulesPath);
fs.symlinkSync(
path.resolve(ngRootDir, 'dist', 'all', '@angular'),
path.resolve(nodeModulesPath, '@angular'));
fs.symlinkSync(
path.resolve(ngRootDir, 'node_modules', 'rxjs'), path.resolve(nodeModulesPath, 'rxjs'));
fs.symlinkSync(
path.resolve(ngRootDir, 'node_modules', 'typescript'),
path.resolve(nodeModulesPath, 'typescript'));
return createTestSupportFor(basePath);
}
export function isInBazel() {
return process.env.TEST_SRCDIR != null;
}
export function setup(): TestSupport {
return isInBazel() ? setupBazel() : setupTestSh();
const tmpDirPath = makeTempDir();
setupBazelTo(tmpDirPath);
return createTestSupportFor(tmpDirPath);
}
export function expectNoDiagnostics(options: ng.CompilerOptions, diags: ng.Diagnostics) {