|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); |
| 4 | +const isWindows = process.platform === 'win32'; |
| 5 | +const runfilesExt = isWindows ? 'bat' : 'sh'; |
| 6 | + |
| 7 | +function normPath(p) { |
| 8 | + let result = p.replace(/\\/g, '/'); |
| 9 | + if (isWindows) { |
| 10 | + // On Windows, we normalize to lowercase for so path mismatches such as 'C:/Users' and |
| 11 | + // 'c:/users' don't break the specs. |
| 12 | + result = result.toLowerCase(); |
| 13 | + if (/[a-zA-Z]\:/.test(result)) { |
| 14 | + // Handle c:/ and /c/ mismatch |
| 15 | + result = `/${result[0]}${result.slice(2)}`; |
| 16 | + } |
| 17 | + } |
| 18 | + return result; |
| 19 | +} |
| 20 | + |
| 21 | +function expectPathsToMatch(a, b) { |
| 22 | + if (Array.isArray(a) && Array.isArray(b)) { |
| 23 | + a = a.map(p => normPath(p)); |
| 24 | + b = b.map(p => normPath(p)); |
| 25 | + expect(a).toEqual(b); |
| 26 | + } else { |
| 27 | + expect(normPath(a)).toBe(normPath(b)); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +describe('launcher.sh environment', function() { |
| 32 | + it('should setup correct bazel environment variables when in runfiles', function() { |
| 33 | + const runfilesRoot = normPath(process.env['RUNFILES']); |
| 34 | + const match = runfilesRoot.match(/\/bazel-out\//); |
| 35 | + expect(!!match).toBe(true); |
| 36 | + const execroot = runfilesRoot.slice(0, match.index); |
| 37 | + expectPathsToMatch(path.basename(runfilesRoot), `env_test.${runfilesExt}.runfiles`); |
| 38 | + expectPathsToMatch(process.env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs'); |
| 39 | + expectPathsToMatch(process.env['BAZEL_TARGET'], '//internal/node/test:env_test'); |
| 40 | + expectPathsToMatch(process.cwd(), `${process.env['RUNFILES']}/build_bazel_rules_nodejs`); |
| 41 | + expectPathsToMatch(process.env['PWD'], `${process.env['RUNFILES']}/build_bazel_rules_nodejs`); |
| 42 | + expectPathsToMatch(process.env['BAZEL_PATCH_ROOT'], process.env['RUNFILES']); |
| 43 | + expectPathsToMatch(process.env['BAZEL_NODE_MODULES_ROOT'], 'npm/node_modules'); |
| 44 | + const expectedGuards = [ |
| 45 | + `${execroot}/node_modules`, |
| 46 | + `${runfilesRoot}/npm/node_modules`, |
| 47 | + `${runfilesRoot}/build_bazel_rules_nodejs/external/npm/node_modules`, |
| 48 | + ] |
| 49 | + expectPathsToMatch(process.env['BAZEL_PATCH_GUARDS'].split(','), expectedGuards); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should setup correct bazel environment variables when in execroot with no third party deps', |
| 53 | + function() { |
| 54 | + const env = require(runfiles.resolvePackageRelative('dump_build_env.json')); |
| 55 | + // On Windows, the RUNFILES path ends in a /MANIFEST segment in this context |
| 56 | + const runfilesRoot = normPath(isWindows ? path.dirname(env['RUNFILES']) : env['RUNFILES']); |
| 57 | + const match = runfilesRoot.match(/\/bazel-out\//); |
| 58 | + expect(!!match).toBe(true); |
| 59 | + const execroot = runfilesRoot.slice(0, match.index); |
| 60 | + expectPathsToMatch(path.basename(runfilesRoot), `dump_build_env.${runfilesExt}.runfiles`); |
| 61 | + expectPathsToMatch(env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs'); |
| 62 | + expectPathsToMatch(env['BAZEL_TARGET'], '//internal/node/test:dump_build_env'); |
| 63 | + expectPathsToMatch(env['PWD'], execroot); |
| 64 | + expectPathsToMatch(env['BAZEL_PATCH_ROOT'], path.dirname(execroot)); |
| 65 | + expectPathsToMatch(env['BAZEL_NODE_MODULES_ROOT'], 'build_bazel_rules_nodejs/node_modules'); |
| 66 | + const expectedGuards = [ |
| 67 | + `${execroot}/node_modules`, |
| 68 | + ] |
| 69 | + expectPathsToMatch(env['BAZEL_PATCH_GUARDS'].split(','), expectedGuards); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should setup correct bazel environment variables when in execroot with third party deps', |
| 73 | + function() { |
| 74 | + const env = require(runfiles.resolvePackageRelative('dump_build_env_alt.json')); |
| 75 | + // On Windows, the RUNFILES path ends in a /MANIFEST segment in this context |
| 76 | + const runfilesRoot = normPath(isWindows ? path.dirname(env['RUNFILES']) : env['RUNFILES']); |
| 77 | + const match = runfilesRoot.match(/\/bazel-out\//); |
| 78 | + expect(!!match).toBe(true); |
| 79 | + const execroot = runfilesRoot.slice(0, match.index); |
| 80 | + expectPathsToMatch( |
| 81 | + path.basename(runfilesRoot), `dump_build_env_alt.${runfilesExt}.runfiles`); |
| 82 | + expectPathsToMatch(env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs'); |
| 83 | + expectPathsToMatch(env['BAZEL_TARGET'], '//internal/node/test:dump_build_env_alt'); |
| 84 | + expectPathsToMatch(env['PWD'], execroot); |
| 85 | + expectPathsToMatch(env['BAZEL_PATCH_ROOT'], path.dirname(execroot)); |
| 86 | + expectPathsToMatch(env['BAZEL_NODE_MODULES_ROOT'], 'npm/node_modules'); |
| 87 | + const expectedGuards = [ |
| 88 | + `${execroot}/node_modules`, |
| 89 | + `${path.dirname(execroot)}/npm/node_modules`, |
| 90 | + `${execroot}/external/npm/node_modules`, |
| 91 | + ] |
| 92 | + expectPathsToMatch(env['BAZEL_PATCH_GUARDS'].split(','), expectedGuards); |
| 93 | + }); |
| 94 | +}); |
0 commit comments