2017-10-13 12:03:28 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-10-13 12:03:28 -04: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {runOneBuild} from '@angular/bazel';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {createTsConfig} from './tsconfig_template';
|
|
|
|
|
|
|
|
export interface TestSupport {
|
|
|
|
basePath: string;
|
|
|
|
runfilesPath: string;
|
|
|
|
angularCorePath: string;
|
2019-06-20 15:50:54 -04:00
|
|
|
typesRoots: string;
|
2017-10-13 12:03:28 -04:00
|
|
|
writeConfig({
|
2020-04-13 19:40:21 -04:00
|
|
|
srcTargetPath,
|
|
|
|
depPaths,
|
|
|
|
pathMapping,
|
2017-10-13 12:03:28 -04:00
|
|
|
}: {
|
|
|
|
srcTargetPath: string,
|
|
|
|
depPaths?: string[],
|
|
|
|
pathMapping?: Array<{moduleName: string; path: string;}>,
|
2018-01-29 18:01:38 -05:00
|
|
|
}): {compilerOptions: ts.CompilerOptions};
|
2017-10-13 12:03:28 -04:00
|
|
|
read(fileName: string): string;
|
|
|
|
write(fileName: string, content: string): void;
|
|
|
|
writeFiles(...mockDirs: {[fileName: string]: string}[]): void;
|
|
|
|
shouldExist(fileName: string): void;
|
|
|
|
shouldNotExist(fileName: string): void;
|
2017-10-24 07:54:08 -04:00
|
|
|
runOneBuild(): boolean;
|
2017-10-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
export function setup({
|
|
|
|
bazelBin = 'bazel-bin',
|
|
|
|
tsconfig = 'tsconfig.json',
|
|
|
|
}: {
|
|
|
|
bazelBin?: string,
|
|
|
|
tsconfig?: string,
|
|
|
|
} = {}): TestSupport {
|
2017-12-06 09:56:49 -05:00
|
|
|
const runfilesPath = process.env['TEST_SRCDIR'];
|
2017-10-13 12:03:28 -04:00
|
|
|
|
|
|
|
const basePath = makeTempDir(runfilesPath);
|
|
|
|
|
|
|
|
const bazelBinPath = path.resolve(basePath, bazelBin);
|
|
|
|
fs.mkdirSync(bazelBinPath);
|
|
|
|
|
2019-04-26 04:15:17 -04:00
|
|
|
const angularCorePath = path.dirname(require.resolve('angular/packages/core'));
|
2017-10-13 12:03:28 -04:00
|
|
|
const tsConfigJsonPath = path.resolve(basePath, tsconfig);
|
|
|
|
|
2019-06-20 15:50:54 -04:00
|
|
|
const emptyTsConfig = ts.readConfigFile(
|
|
|
|
require.resolve('angular/packages/bazel/test/ngc-wrapped/empty/empty_tsconfig.json'), read);
|
|
|
|
const typesRoots = (emptyTsConfig as any).config.compilerOptions.typeRoots[0];
|
|
|
|
|
2017-10-13 12:03:28 -04:00
|
|
|
return {
|
|
|
|
basePath,
|
|
|
|
runfilesPath,
|
|
|
|
angularCorePath,
|
2019-06-20 15:50:54 -04:00
|
|
|
typesRoots,
|
2017-10-13 12:03:28 -04:00
|
|
|
write,
|
|
|
|
read,
|
|
|
|
writeFiles,
|
|
|
|
writeConfig,
|
|
|
|
shouldExist,
|
|
|
|
shouldNotExist,
|
2019-06-20 15:50:54 -04:00
|
|
|
runOneBuild: runOneBuildImpl,
|
2017-10-13 12:03:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// -----------------
|
|
|
|
// helpers
|
|
|
|
|
2018-01-29 18:01:38 -05:00
|
|
|
function mkdirp(dirname: string) {
|
|
|
|
const parent = path.dirname(dirname);
|
|
|
|
if (!fs.existsSync(parent)) {
|
|
|
|
mkdirp(parent);
|
|
|
|
}
|
|
|
|
fs.mkdirSync(dirname);
|
|
|
|
}
|
|
|
|
|
2017-10-13 12:03:28 -04:00
|
|
|
function write(fileName: string, content: string) {
|
|
|
|
const dir = path.dirname(fileName);
|
|
|
|
if (dir != '.') {
|
|
|
|
const newDir = path.resolve(basePath, dir);
|
2018-01-29 18:01:38 -05:00
|
|
|
if (!fs.existsSync(newDir)) mkdirp(newDir);
|
2017-10-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
fs.writeFileSync(path.resolve(basePath, fileName), content, {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
|
|
|
function read(fileName: string) {
|
|
|
|
return fs.readFileSync(path.resolve(basePath, fileName), {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeFiles(...mockDirs: {[fileName: string]: string}[]) {
|
2020-04-13 19:40:21 -04:00
|
|
|
mockDirs.forEach((dir) => {
|
|
|
|
Object.keys(dir).forEach((fileName) => {
|
|
|
|
write(fileName, dir[fileName]);
|
|
|
|
});
|
|
|
|
});
|
2017-10-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeConfig({
|
2020-04-13 19:40:21 -04:00
|
|
|
srcTargetPath,
|
|
|
|
depPaths = [],
|
|
|
|
pathMapping = [],
|
2017-10-13 12:03:28 -04:00
|
|
|
}: {
|
|
|
|
srcTargetPath: string,
|
|
|
|
depPaths?: string[],
|
|
|
|
pathMapping?: Array<{moduleName: string; path: string;}>,
|
|
|
|
}) {
|
|
|
|
srcTargetPath = path.resolve(basePath, srcTargetPath);
|
|
|
|
const compilationTargetSrc = listFilesRecursive(srcTargetPath);
|
|
|
|
const target = '//' + path.relative(basePath, srcTargetPath);
|
|
|
|
const files = [...compilationTargetSrc];
|
|
|
|
|
|
|
|
depPaths = depPaths.concat([angularCorePath]);
|
2019-04-26 04:15:17 -04:00
|
|
|
pathMapping = pathMapping.concat([
|
|
|
|
{moduleName: '@angular/core', path: angularCorePath},
|
|
|
|
{moduleName: 'angular/packages/core', path: angularCorePath}
|
|
|
|
]);
|
2017-10-13 12:03:28 -04:00
|
|
|
|
|
|
|
for (const depPath of depPaths) {
|
|
|
|
files.push(...listFilesRecursive(depPath).filter(f => f.endsWith('.d.ts')));
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathMappingObj = {};
|
|
|
|
for (const mapping of pathMapping) {
|
|
|
|
pathMappingObj[mapping.moduleName] = [mapping.path];
|
2019-04-26 04:15:17 -04:00
|
|
|
pathMappingObj[path.posix.join(mapping.moduleName, '*')] =
|
|
|
|
[path.posix.join(mapping.path, '*')];
|
2017-10-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const emptyTsConfig = ts.readConfigFile(
|
2019-04-26 04:15:17 -04:00
|
|
|
require.resolve('angular/packages/bazel/test/ngc-wrapped/empty/empty_tsconfig.json'), read);
|
2017-10-13 12:03:28 -04:00
|
|
|
|
|
|
|
const tsconfig = createTsConfig({
|
|
|
|
defaultTsConfig: emptyTsConfig.config,
|
|
|
|
rootDir: basePath,
|
|
|
|
target: target,
|
2020-04-13 19:40:21 -04:00
|
|
|
outDir: bazelBinPath,
|
|
|
|
compilationTargetSrc,
|
2017-10-13 12:03:28 -04:00
|
|
|
files: files,
|
|
|
|
pathMapping: pathMappingObj,
|
|
|
|
});
|
|
|
|
write(path.resolve(basePath, tsConfigJsonPath), JSON.stringify(tsconfig, null, 2));
|
2018-01-29 18:01:38 -05:00
|
|
|
return tsconfig;
|
2017-10-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function shouldExist(fileName: string) {
|
|
|
|
if (!fs.existsSync(path.resolve(basePath, fileName))) {
|
|
|
|
throw new Error(`Expected ${fileName} to be emitted (basePath: ${basePath})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldNotExist(fileName: string) {
|
|
|
|
if (fs.existsSync(path.resolve(basePath, fileName))) {
|
|
|
|
throw new Error(`Did not expect ${fileName} to be emitted (basePath: ${basePath})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
function runOneBuildImpl(): boolean {
|
|
|
|
return runOneBuild(['@' + tsConfigJsonPath]);
|
|
|
|
}
|
2017-10-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
2018-09-10 06:44:48 -04:00
|
|
|
function makeTempDir(baseDir: string): string {
|
2017-10-13 12:03:28 -04:00
|
|
|
const id = (Math.random() * 1000000).toFixed(0);
|
|
|
|
const dir = path.join(baseDir, `tmp.${id}`);
|
|
|
|
fs.mkdirSync(dir);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function listFilesRecursive(dir: string, fileList: string[] = []) {
|
2017-12-06 09:56:49 -05:00
|
|
|
fs.readdirSync(dir).forEach(file => {
|
2017-10-13 12:03:28 -04:00
|
|
|
if (fs.statSync(path.join(dir, file)).isDirectory()) {
|
|
|
|
listFilesRecursive(path.join(dir, file), fileList);
|
|
|
|
} else {
|
|
|
|
fileList.push(path.join(dir, file));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return fileList;
|
|
|
|
}
|