-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow multiple run_node calls to be made from the same rule context
- Loading branch information
Showing
6 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary") | ||
load("//internal/providers/test:run_node_test.bzl", "js_write_file") | ||
|
||
# two nodejs_binary targets provide two separate executables in the run_node test | ||
nodejs_binary( | ||
name = "writer_bin", | ||
entry_point = "js-write-file.js", | ||
) | ||
|
||
nodejs_binary( | ||
name = "writer_bin2", | ||
data = [ | ||
# the JS files doesn't actually consume this dependency, but it causes a different modules | ||
# manifest file to be written in for both run_node actions in the js_write_file rule below | ||
"@npm//shelljs", | ||
], | ||
entry_point = "js-write-file.js", | ||
) | ||
|
||
js_write_file( | ||
name = "write_file", | ||
content = "test file content", | ||
) | ||
|
||
# tests that the rule above generated two files for each run_node action | ||
[ | ||
generated_file_test( | ||
name = "run_node_test_%s" % file, | ||
src = "_run_node_out.txt", | ||
generated = "%s.txt" % file, | ||
) | ||
for file in [ | ||
"out", | ||
"out2", | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test file content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const writeFileSync = require('fs').writeFileSync; | ||
|
||
const content = process.argv[2]; | ||
const outputPath = process.argv[3]; | ||
|
||
writeFileSync(outputPath, content, {encoding: 'utf8'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Contrived rule for writing some content to a file using run_node | ||
Tests that two run_node calls can be made from the same rule context, where each run_node call | ||
uses a different executable (and therefore the content of the modules manifest file is different) | ||
""" | ||
|
||
load("//:providers.bzl", "run_node") | ||
|
||
def _js_write_file_impl(ctx): | ||
run_node( | ||
ctx = ctx, | ||
executable = "_writer", | ||
mnemonic = "writer", | ||
inputs = [], | ||
arguments = [ | ||
ctx.attr.content, | ||
ctx.outputs.out.path, | ||
], | ||
outputs = [ctx.outputs.out], | ||
) | ||
|
||
run_node( | ||
ctx = ctx, | ||
executable = "_writer2", | ||
# mnemonic is left as None here to test the node_modules manifest writer handling None | ||
inputs = [], | ||
arguments = [ | ||
ctx.attr.content, | ||
ctx.outputs.out2.path, | ||
], | ||
outputs = [ctx.outputs.out2], | ||
) | ||
|
||
js_write_file = rule( | ||
implementation = _js_write_file_impl, | ||
outputs = { | ||
"out": "out.txt", | ||
"out2": "out2.txt", | ||
}, | ||
attrs = { | ||
"content": attr.string(), | ||
"_writer": attr.label( | ||
default = Label("//internal/providers/test:writer_bin"), | ||
cfg = "host", | ||
executable = True, | ||
), | ||
"_writer2": attr.label( | ||
default = Label("//internal/providers/test:writer_bin2"), | ||
cfg = "host", | ||
executable = True, | ||
), | ||
}, | ||
) |