Skip to content

Commit

Permalink
build: support running compiler-cli tests on windows
Browse files Browse the repository at this point in the history
In order to support running "compiler-cli" tests that use the "test_support.ts"
utilities on Windows with Bazel, we need to imporve the logic that resolves NPM
packages and symlinks them into a temporary directory.

A more Bazel idiomatic and windows compatible way of resolving Bazel runfiles
is to use the "RUNFILES_MANIFEST" if present. This ensures that the NPM
packages can be also symlinked on Windows, and tests can execute properly
on Windows. Read more about why this is needed here:

* bazelbuild/bazel#3726 (comment)
  • Loading branch information
devversion committed Jan 28, 2019
1 parent 8d7eac0 commit 8ebb1ea
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/compiler-cli/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ts_library(
testonly = True,
srcs = [
"mocks.ts",
"runfile_helpers.ts",
"test_support.ts",
],
visibility = [
Expand Down
27 changes: 8 additions & 19 deletions packages/compiler-cli/test/ngcc/ngcc_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {join} from 'path';
const Module = require('module');

import {mainNgcc} from '../../src/ngcc/src/main';
import {getAngularPackagesFromRunfiles} from '../runfile_helpers';

describe('ngcc main()', () => {
beforeEach(createMockFileSystem);
Expand Down Expand Up @@ -40,8 +41,7 @@ describe('ngcc main()', () => {


function createMockFileSystem() {
const packagesPath = join(process.env.TEST_SRCDIR !, 'angular/packages');
mockFs({'/node_modules/@angular': loadPackages(packagesPath)});
mockFs({'/node_modules/@angular': loadAngularPackages()});
spyOn(Module, '_resolveFilename').and.callFake(mockResolve);
}

Expand All @@ -50,27 +50,16 @@ function restoreRealFileSystem() {
}


/**
* Load the built Angular packages into an in-memory structure.
* @param packagesPath the path to the folder containing the built packages.
*/
function loadPackages(packagesPath: string): Directory {
/** Load the built Angular packages into an in-memory structure. */
function loadAngularPackages(): Directory {
const packagesDirectory: Directory = {};
readdirSync(packagesPath).forEach(name => {
const packagePath = join(packagesPath, name);
if (!statSync(packagePath).isDirectory()) {
return;
}
const npmPackagePath = join(packagePath, 'npm_package');
if (!existsSync(npmPackagePath)) {
return;
}
packagesDirectory[name] = loadDirectory(npmPackagePath);
});

getAngularPackagesFromRunfiles().forEach(
({name, pkgPath}) => { packagesDirectory[name] = loadDirectory(pkgPath); });

return packagesDirectory;
}


/**
* Load real files from the filesystem into an "in-memory" structure,
* which can be used with `mock-fs`.
Expand Down
48 changes: 48 additions & 0 deletions packages/compiler-cli/test/runfile_helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 * as fs from 'fs';
import * as path from 'path';

/**
* Gets all built Angular NPM package artifacts by querying the Bazel runfiles.
* In case there is a runfiles manifest (e.g. on Windows), the packages are resolved
* through the manifest because the runfiles are not symlinked and cannot be searched
* within the real filesystem.
*/
export function getAngularPackagesFromRunfiles() {
// Path to the Bazel runfiles manifest if present. This file is present if runfiles are
// not symlinked into the runfiles directory.
const runfilesManifestPath = process.env.RUNFILES_MANIFEST_FILE;

if (!runfilesManifestPath) {
const packageRunfilesDir = path.join(process.env.RUNFILES !, 'angular/packages');

return fs.readdirSync(packageRunfilesDir)
.map(name => ({name, pkgPath: path.join(packageRunfilesDir, name, 'npm_package/')}))
.filter(({pkgPath}) => fs.existsSync(pkgPath));
}

return fs.readFileSync(runfilesManifestPath, 'utf8')
.split('\n')
.map(mapping => mapping.split(' '))
.filter(([runfilePath]) => runfilePath.match(/^angular\/packages\/[\w-]+\/npm_package$/))
.map(([runfilePath, realPath]) => ({
name: path.relative('angular/packages', runfilePath).split(path.sep)[0],
pkgPath: realPath,
}));
}

/**
* Resolves a NPM package from the Bazel runfiles. We need to resolve the Bazel tree
* artifacts using a "resolve file" because the NodeJS module resolution does not allow
* resolving to directory paths.
*/
export function resolveNpmTreeArtifact(manifestPath: string, resolveFile = 'package.json') {
return path.dirname(require.resolve(path.posix.join(manifestPath, resolveFile)));
}
59 changes: 33 additions & 26 deletions packages/compiler-cli/test/test_support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import * as ng from '../index';
import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from './runfile_helpers';

// TEST_TMPDIR is always set by Bazel.
const tmpdir = process.env.TEST_TMPDIR!;
const tmpdir = process.env.TEST_TMPDIR !;

export function makeTempDir(): string {
let dir: string;
Expand Down Expand Up @@ -51,6 +52,7 @@ function createTestSupportFor(basePath: string) {
'baseUrl': basePath,
'declaration': true,
'target': ts.ScriptTarget.ES5,
'newLine': ts.NewLineKind.LineFeed,
'module': ts.ModuleKind.ES2015,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'lib': Object.freeze([
Expand All @@ -62,7 +64,16 @@ function createTestSupportFor(basePath: string) {
};


return {basePath, write, writeFiles, createCompilerOptions, shouldExist, shouldNotExist};
return {
// We normalize the basePath into a posix path, so that multiple assertions which compare
// paths don't need to normalize the path separators each time.
basePath: normalizeSeparators(basePath),
write,
writeFiles,
createCompilerOptions,
shouldExist,
shouldNotExist
};

function write(fileName: string, content: string) {
const dir = path.dirname(fileName);
Expand Down Expand Up @@ -95,37 +106,29 @@ function createTestSupportFor(basePath: string) {
}
}

export function setupBazelTo(basePath: string) {
if (!process.env.TEST_SRCDIR) {
throw new Error('`setupBazelTo()` must only be called from in a Bazel job.');
}
const sources = process.env.TEST_SRCDIR;
const packages = path.join(sources, 'angular/packages');
const nodeModulesPath = path.join(basePath, 'node_modules');
export function setupBazelTo(tmpDirPath: string) {
const nodeModulesPath = path.join(tmpDirPath, 'node_modules');
const angularDirectory = path.join(nodeModulesPath, '@angular');
fs.mkdirSync(nodeModulesPath);

// Link the built angular packages
fs.mkdirSync(nodeModulesPath);
fs.mkdirSync(angularDirectory);
const packageNames = fs.readdirSync(packages).filter(
name => fs.statSync(path.join(packages, name)).isDirectory() &&
fs.existsSync(path.join(packages, name, 'npm_package')));
for (const pkg of packageNames) {
fs.symlinkSync(path.join(packages, `${pkg}/npm_package`), path.join(angularDirectory, pkg));
}

// Link rxjs
const rxjsSource = path.join(sources, 'rxjs');
const rxjsDest = path.join(nodeModulesPath, 'rxjs');
if (fs.existsSync(rxjsSource)) {
fs.symlinkSync(rxjsSource, rxjsDest);
}
getAngularPackagesFromRunfiles().forEach(
({pkgPath, name}) => { fs.symlinkSync(pkgPath, path.join(angularDirectory, name), 'dir'); });

// Link typescript
const typescriptSource = path.join(sources, 'ngdeps/node_modules/typescript');
const typeScriptSource = resolveNpmTreeArtifact('ngdeps/node_modules/typescript');
const typescriptDest = path.join(nodeModulesPath, 'typescript');
if (fs.existsSync(typescriptSource)) {
fs.symlinkSync(typescriptSource, typescriptDest);
fs.symlinkSync(typeScriptSource, typescriptDest, 'dir');

// Link "rxjs" if it has been set up as a runfile. "rxjs" is linked optionally because
// not all compiler-cli tests need "rxjs" set up.
try {
const rxjsSource = resolveNpmTreeArtifact('rxjs', 'index.js');
const rxjsDest = path.join(nodeModulesPath, 'rxjs');
fs.symlinkSync(rxjsSource, rxjsDest, 'dir');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e;
}
}

Expand All @@ -148,3 +151,7 @@ export function expectNoDiagnosticsInProgram(options: ng.CompilerOptions, p: ng.
...p.getNgSemanticDiagnostics()
]);
}

export function normalizeSeparators(path: string): string {
return path.replace(/\\/g, '/');
}

0 comments on commit 8ebb1ea

Please sign in to comment.