Skip to content

Commit

Permalink
Add missing test for arguments to node programs
Browse files Browse the repository at this point in the history
This would have prevented the need to revert a commit:
404c4b0
  • Loading branch information
alexeagle committed Oct 31, 2017
1 parent ce6ad20 commit 490d9af
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions examples/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ nodejs_binary(
name = "rollup",
entry_point = "rollup/bin/rollup",
node_modules = ":node_modules",
# Tests that templated_args works
# NB: Space before the / is needed to prevent shell on Windows from
# converting this argument to a path under C:\
templated_args = ["--intro", "' /*Generated by Bazel*/'"],
)

load(":rollup.bzl", "rollup")
Expand Down
2 changes: 1 addition & 1 deletion examples/rollup/rollup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('bundling', () => {
// TODO(#32) Can shorten the path if https://github.com/bazelbuild/rules_nodejs/issues/32 is resolved
const bundle = 'build_bazel_rules_nodejs/examples/rollup/bundle.js';
const actual = fs.readFileSync(require.resolve(bundle), { encoding: 'utf-8' });
const expected = 'const name = \'Alice\';\n\nconsole.log(`Hello, ${name}`);\n';
const expected = '/*Generated by Bazel*/\n\nconst name = \'Alice\';\n\nconsole.log(`Hello, ${name}`);\n';
expect(actual).toEqual(expected);
});
});
4 changes: 2 additions & 2 deletions internal/devmode_js_sources.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load(":node.bzl", "sources_aspect", "expand_location_into_runfiles")
load(":node.bzl", "sources_aspect", "expand_path_into_runfiles")

def _devmode_js_sources_impl(ctx):
files = depset()
Expand All @@ -10,7 +10,7 @@ def _devmode_js_sources_impl(ctx):
files += d.files

ctx.actions.write(ctx.outputs.manifest, "".join([
expand_location_into_runfiles(ctx, f.path) + "\n" for f in files
expand_path_into_runfiles(ctx, f.path) + "\n" for f in files
]))
return [DefaultInfo(files = files)]

Expand Down
13 changes: 10 additions & 3 deletions internal/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ def _write_loader_script(ctx):
executable=True,
)

def expand_location_into_runfiles(ctx, input):
"""Given a string that might contain $(location) label expansions,
def expand_location_into_runfiles(ctx, path):
"""If the path has a location expansion, expand it. Otherwise return as-is.
"""
if path.find('$(location') < 0:
return path
return expand_path_into_runfiles(ctx, path)

def expand_path_into_runfiles(ctx, path):
"""Given a file path that might contain a $(location) label expansion,
provide the path to the file in runfiles.
See https://docs.bazel.build/versions/master/skylark/lib/ctx.html#expand_location
"""
targets = ctx.attr.data if hasattr(ctx.attr, "data") else []
expanded = ctx.expand_location(input, targets)
expanded = ctx.expand_location(path, targets)
if expanded.startswith(ctx.bin_dir.path):
expanded = expanded[len(ctx.bin_dir.path + "/"):]
if expanded.startswith(ctx.genfiles_dir.path):
Expand Down

0 comments on commit 490d9af

Please sign in to comment.