blob: 823be9109fb62772b20560a7f8ebb18a0f915e48 [file]
const { join } = require('path')
const { Runfiles } = require('@bazel/runfiles')
function describe(name, fn) {
console.log(name)
fn()
}
function it(name, fn) {
console.log(` ${name}`)
fn()
}
function assert(t, msg) {
if (!t) {
throw new Error(msg)
}
}
const runfiles = new Runfiles(process.env)
describe('runfiles env', () => {
it('should have the RUNFILES_DIR env var for use by tools such as @bazel/runfiles', () => {
assert(
!!process.env.RUNFILES_DIR,
'Expected the RUNFILES_DIR env var to be set'
)
})
})
describe('runfile resolution', () => {
it('should properly resolve the files with the module(name)', () => {
const testFixturePath = runfiles.resolve(
'examples/runfiles/test_fixture.md'
)
const expectedPath = join(__dirname, 'test_fixture.md')
assert(
normalizePath(testFixturePath) == normalizePath(expectedPath),
`Expected the test fixture to be resolved next to the spec source file: ${testFixturePath} vs ${expectedPath}`
)
})
it('should properly resolve with forward slashes', () => {
const testFixturePath = runfiles.resolve(
'examples\\runfiles\\test_fixture.md'
)
const expectedPath = join(__dirname, 'test_fixture.md')
assert(
normalizePath(testFixturePath) == normalizePath(expectedPath),
`Expected the test fixture to be resolved next to the spec source file: ${testFixturePath} vs ${expectedPath}`
)
})
it('should properly resolve with the _main module alias', () => {
const testFixturePath = runfiles.resolve(
'_main/runfiles/test_fixture.md'
)
const expectedPath = join(__dirname, 'test_fixture.md')
assert(
normalizePath(testFixturePath) == normalizePath(expectedPath),
`Expected the test fixture to be resolved next to the spec source file: ${testFixturePath} vs ${expectedPath}`
)
})
it('should properly resolve a runfile within a direct module dependency', () => {
const fsPatchPath = runfiles.resolve(
'aspect_rules_js/js/private/node-patches/fs.cjs'
)
assert(!!fsPatchPath, `Expected to find fs patches`)
assert(
fsPatchPath.indexOf('/aspect_rules_js/') == -1,
`Expected to find fs patches in a resolved bzlmod directory`
)
})
})
/**
* Normalizes the delimiters within the specified path. This is useful for test assertions
* where paths might be computed using different path delimiters.
*/
function normalizePath(value) {
return value.replace(/\\/g, '/')
}