From 596dfb88a62a7632d975654f96e88146bc2de94e Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Mon, 22 Feb 2021 14:27:30 -0800 Subject: [PATCH] test(bazel): use bazel runfiles helper for resolving file names (#40955) Use bazel's runfiles helper to resolve file name paths rather than require. PR Close #40955 --- .../bazel/test/ng_package/common_package.spec.ts | 5 ++++- packages/bazel/test/ng_package/core_package.spec.ts | 5 ++++- .../bazel/test/ng_package/example_package.spec.ts | 13 ++++++++----- packages/bazel/test/ngc-wrapped/flat_module_test.ts | 5 ++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/bazel/test/ng_package/common_package.spec.ts b/packages/bazel/test/ng_package/common_package.spec.ts index 9eb5c0b7ba..50e881b37b 100644 --- a/packages/bazel/test/ng_package/common_package.spec.ts +++ b/packages/bazel/test/ng_package/common_package.spec.ts @@ -11,10 +11,13 @@ import * as fs from 'fs'; import * as path from 'path'; import * as shx from 'shelljs'; +/** Runfiles helper from bazel to resolve file name paths. */ +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!); + // Resolve the "npm_package" directory by using the runfile resolution. Note that we need to // resolve the "package.json" of the package since otherwise NodeJS would resolve the "main" // file, which is not necessarily at the root of the "npm_package". -shx.cd(path.dirname(require.resolve('angular/packages/common/npm_package/package.json'))); +shx.cd(path.dirname(runfiles.resolve('angular/packages/common/npm_package/package.json'))); describe('@angular/common ng_package', () => { describe('should have the locales files', () => { diff --git a/packages/bazel/test/ng_package/core_package.spec.ts b/packages/bazel/test/ng_package/core_package.spec.ts index d86b92c44b..7c5876c2ec 100644 --- a/packages/bazel/test/ng_package/core_package.spec.ts +++ b/packages/bazel/test/ng_package/core_package.spec.ts @@ -10,10 +10,13 @@ import {ivyEnabled, obsoleteInIvy} from '@angular/private/testing'; import * as path from 'path'; import * as shx from 'shelljs'; +/** Runfiles helper from bazel to resolve file name paths. */ +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!); + // Resolve the "npm_package" directory by using the runfile resolution. Note that we need to // resolve the "package.json" of the package since otherwise NodeJS would resolve the "main" // file, which is not necessarily at the root of the "npm_package". -shx.cd(path.dirname(require.resolve('angular/packages/core/npm_package/package.json'))); +shx.cd(path.dirname(runfiles.resolve('angular/packages/core/npm_package/package.json'))); /** * Utility functions that allows me to create fs paths diff --git a/packages/bazel/test/ng_package/example_package.spec.ts b/packages/bazel/test/ng_package/example_package.spec.ts index e321f30928..50f3eb8fdc 100644 --- a/packages/bazel/test/ng_package/example_package.spec.ts +++ b/packages/bazel/test/ng_package/example_package.spec.ts @@ -11,6 +11,9 @@ import {createPatch} from 'diff'; import * as fs from 'fs'; import * as path from 'path'; +/** Runfiles helper from bazel to resolve file name paths. */ +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!); + type TestPackage = { displayName: string; packagePath: string; goldenFilePath: string; }; @@ -21,18 +24,18 @@ const packagesToTest: TestPackage[] = [ // Resolve the "npm_package" directory by using the runfile resolution. Note that we need to // resolve the "package.json" of the package since otherwise NodeJS would resolve the "main" // file, which is not necessarily at the root of the "npm_package". - packagePath: path.dirname( - require.resolve('angular/packages/bazel/test/ng_package/example/npm_package/package.json')), - goldenFilePath: require.resolve('./example_package.golden') + packagePath: path.dirname(runfiles.resolve( + 'angular/packages/bazel/test/ng_package/example/npm_package/package.json')), + goldenFilePath: runfiles.resolvePackageRelative('./example_package.golden') }, { displayName: 'Example with ts_library NPM package', // Resolve the "npm_package" directory by using the runfile resolution. Note that we need to // resolve the "package.json" of the package since otherwise NodeJS would resolve the "main" // file, which is not necessarily at the root of the "npm_package". - packagePath: path.dirname(require.resolve( + packagePath: path.dirname(runfiles.resolve( 'angular/packages/bazel/test/ng_package/example-with-ts-library/npm_package/package.json')), - goldenFilePath: require.resolve('./example_with_ts_library_package.golden') + goldenFilePath: runfiles.resolvePackageRelative('./example_with_ts_library_package.golden') }, ]; diff --git a/packages/bazel/test/ngc-wrapped/flat_module_test.ts b/packages/bazel/test/ngc-wrapped/flat_module_test.ts index f1e1dedbd0..492ca59d48 100644 --- a/packages/bazel/test/ngc-wrapped/flat_module_test.ts +++ b/packages/bazel/test/ngc-wrapped/flat_module_test.ts @@ -10,13 +10,16 @@ import {obsoleteInIvy, onlyInIvy} from '@angular/private/testing'; import {existsSync, readFileSync} from 'fs'; import {dirname, join} from 'path'; +/** Runfiles helper from bazel to resolve file name paths. */ +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!); + describe('flat_module ng_module', () => { let packageOutput: string; let flatModuleOutFile: string; beforeAll(() => { packageOutput = - dirname(require.resolve('angular/packages/bazel/test/ngc-wrapped/flat_module/index.js')); + dirname(runfiles.resolve('angular/packages/bazel/test/ngc-wrapped/flat_module/index.js')); flatModuleOutFile = join(packageOutput, 'flat_module.js'); });