Skip to content

Commit

Permalink
test: add @bazel/runfiles test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Aug 17, 2024
1 parent dd4b1ca commit b74cd5b
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ examples/npm_package/packages/pkg_a/package.json=1006424040
examples/npm_package/packages/pkg_b/package.json=1041247977
examples/npm_package/packages/pkg_d/package.json=1110895851
examples/npm_package/packages/pkg_e/package.json=-2145239245
examples/runfiles/package.json=-2022435552
examples/webpack_cli/package.json=1911342006
js/private/coverage/bundle/package.json=-1543718929
js/private/image/package.json=-1260474848
Expand All @@ -28,5 +29,5 @@ npm/private/test/vendored/is-odd/package.json=1041695223
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
npm/private/test/vendored/semver-max/package.json=578664053
package.json=-275319675
pnpm-lock.yaml=1584807079
pnpm-lock.yaml=715709952
pnpm-workspace.yaml=-762451270
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ examples/npm_package/packages/pkg_b/node_modules/
examples/npm_package/packages/pkg_c/node_modules/
examples/npm_package/packages/pkg_d/node_modules/
examples/npm_package/packages/pkg_e/node_modules/
examples/runfiles/node_modules
examples/webpack_cli/node_modules/
js/private/coverage/bundle/node_modules
js/private/image/node_modules
Expand Down
23 changes: 23 additions & 0 deletions examples/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages()

js_test(
name = "runfiles",
data = [
"test_fixture.md",
"test_fixture.md.generated_file_suffix",
":node_modules/@bazel/runfiles",
],
entry_point = "test.js",
)

# Path of file must start similar to `test_fixture.md` in order to regression-test a
# scenario where the runfile resolution would accidentally resolve the path to
# `test_fixture.md` through a runfile manifest entry that starts similarly.
genrule(
name = "gen-data",
outs = ["test_fixture.md.generated_file_suffix"],
cmd = """echo "Generated" > $@""",
)
7 changes: 7 additions & 0 deletions examples/runfiles/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@examples/runfiles",
"private": true,
"dependencies": {
"@bazel/runfiles": "^5.8.1"
}
}
97 changes: 97 additions & 0 deletions examples/runfiles/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const { join, dirname } = 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)
}
}

describe('runfile resolution', () => {
it('should properly resolve the "test_fixture.md" file', () => {
const testFixturePath = runfiles.resolve(
'aspect_rules_js/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(
'aspect_rules_js\\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 a subdirectory of a runfile', () => {
const packagePath = runfiles.resolve('aspect_rules_js/examples')
// Alternate with trailing slash
const packagePath2 = runfiles.resolve('aspect_rules_js/examples/')
const expectedPath = dirname(
dirname(
runfiles.resolve(
'aspect_rules_js/examples/runfiles/test_fixture.md.generated_file_suffix'
)
)
)

assert(
normalizePath(packagePath) == normalizePath(expectedPath),
`Expected to resolve a subdirectory of a runfile: ${packagePath} vs ${expectedPath}`
)
assert(
normalizePath(packagePath2) == normalizePath(expectedPath),
`Expected to resolve a subdirectory of a runfile: ${packagePath2} vs ${expectedPath}`
)
})

it('should properly resolve the workspace root of a runfile', () => {
const packagePath = runfiles.resolve('aspect_rules_js')
// Alternate with trailing slash
const packagePath2 = runfiles.resolve('aspect_rules_js/')
const expectedPath = dirname(
dirname(
dirname(
runfiles.resolve(
'aspect_rules_js/examples/runfiles/test_fixture.md.generated_file_suffix'
)
)
)
)

assert(
normalizePath(packagePath) == normalizePath(expectedPath),
`Expected to resolve the workspace root of a runfile: ${packagePath} vs ${expectedPath}`
)
assert(
normalizePath(packagePath2) == normalizePath(expectedPath),
`Expected to resolve the workspace root of a runfile: ${packagePath2} vs ${expectedPath}`
)
})
})

/**
* 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, '/')
}
1 change: 1 addition & 0 deletions examples/runfiles/test_fixture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file part of the sources
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b74cd5b

Please sign in to comment.