-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a287de
commit 8e6a15c
Showing
14 changed files
with
443 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
docs/*.md | ||
lib/tests/jq/*.json | ||
lib/tests/write_source_files/a2.js | ||
lib/tests/write_source_files/b2.js |
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,61 @@ | ||
<!-- Generated with Stardoc: http://skydoc.bazel.build --> | ||
|
||
Public API for write_source_files | ||
|
||
<a id="#write_source_files"></a> | ||
|
||
## write_source_files | ||
|
||
<pre> | ||
write_source_files(<a href="#write_source_files-name">name</a>, <a href="#write_source_files-files">files</a>, <a href="#write_source_files-kwargs">kwargs</a>) | ||
</pre> | ||
|
||
Write to one or more files in the source tree. Stamp out tests that ensure the files exists and are up to date. | ||
|
||
Usage: | ||
|
||
```starlark | ||
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") | ||
|
||
write_source_files( | ||
name = "write_foobar", | ||
files = { | ||
"foobar.json": "//some/generated:file", | ||
}, | ||
) | ||
``` | ||
|
||
To update the source file, run: | ||
```bash | ||
bazel run //:write_foobar | ||
``` | ||
|
||
A test will fail if the source file doesn't exist | ||
```bash | ||
bazel test //... | ||
|
||
//:foobar.json does not exist. To create & update this file, run: | ||
|
||
bazel run //:write_foobar | ||
``` | ||
|
||
...or if it's out of date. | ||
```bash | ||
bazel test //... | ||
|
||
//:foobar.json is out-of-date. To update this file, run: | ||
|
||
bazel run //:write_foobar | ||
``` | ||
|
||
|
||
**PARAMETERS** | ||
|
||
|
||
| Name | Description | Default Value | | ||
| :------------- | :------------- | :------------- | | ||
| <a id="write_source_files-name"></a>name | Name of the executable target that creates or updates the source file | none | | ||
| <a id="write_source_files-files"></a>files | A dict where the keys are source files to write to and the values are labels pointing to the desired content. Source files must be within the same bazel package as the target. | none | | ||
| <a id="write_source_files-kwargs"></a>kwargs | Other common named parameters such as <code>tags</code> or <code>visibility</code> | none | | ||
|
||
|
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,12 @@ | ||
"Test rule that always fails and prints a message" | ||
|
||
def _fail_with_message_test_impl(ctx): | ||
fail(ctx.attr.message) | ||
|
||
fail_with_message_test = rule( | ||
attrs = { | ||
"message": attr.string(mandatory = True), | ||
}, | ||
implementation = _fail_with_message_test_impl, | ||
test = True, | ||
) |
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,65 @@ | ||
"write_source_file implementation" | ||
|
||
load("//lib:utils.bzl", "is_external_label") | ||
|
||
_write_source_files_attrs = { | ||
"in_files": attr.label_list(allow_files = True, allow_empty = False, mandatory = True), | ||
"out_files": attr.label_list(allow_files = True, allow_empty = False, mandatory = True), | ||
"is_windows": attr.bool(mandatory = True), | ||
} | ||
|
||
def _write_source_files_impl(ctx): | ||
if ctx.attr.is_windows: | ||
fail("write_source_file is not yet implemented for windows") | ||
|
||
if (len(ctx.attr.in_files) != len(ctx.attr.out_files)): | ||
fail("in_files and out_files must be the same length") | ||
|
||
for i in range(len(ctx.attr.in_files)): | ||
out_file_label = ctx.attr.out_files[i].label | ||
if is_external_label(out_file_label): | ||
fail("out file %s must be a source file in the user workspace" % out_file_label) | ||
|
||
if not ctx.files.out_files[i].is_source: | ||
fail("out file %s must be a source file, not a generated file" % out_file_label) | ||
|
||
if out_file_label.package != ctx.label.package: | ||
fail("out file %s (in package '%s') must be a source file within the target's package: '%s'" % (out_file_label, out_file_label.package, ctx.label.package)) | ||
|
||
updater = ctx.actions.declare_file( | ||
ctx.label.name + "_update.sh", | ||
) | ||
|
||
ctx.actions.write( | ||
output = updater, | ||
content = """ | ||
#!/usr/bin/env bash | ||
set -o errexit -o nounset -o pipefail | ||
runfiles_dir=$PWD | ||
# BUILD_WORKSPACE_DIRECTORY not set when running as a test, uses the sandbox instead | ||
if [[ ! -z "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then | ||
cd "$BUILD_WORKSPACE_DIRECTORY" | ||
fi | ||
""" + "\n".join([ | ||
""" | ||
in=$runfiles_dir/{in_file} | ||
out={out_file} | ||
mkdir -p "$(dirname "$out")" | ||
echo "Copying $in to $out in $PWD" | ||
cp -f "$in" "$out" | ||
chmod 644 "$out" | ||
""".format(in_file = ctx.files.in_files[i].short_path, out_file = ctx.files.out_files[i].short_path) | ||
for i in range(len(ctx.attr.in_files)) | ||
]), | ||
) | ||
|
||
return DefaultInfo( | ||
executable = updater, | ||
runfiles = ctx.runfiles(files = ctx.files.in_files), | ||
) | ||
|
||
write_source_files_lib = struct( | ||
attrs = _write_source_files_attrs, | ||
implementation = _write_source_files_impl, | ||
) |
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,34 @@ | ||
load("//lib/tests/write_source_files:write_source_files_test.bzl", "write_source_files_test") | ||
load("//lib:write_source_files.bzl", "write_source_files") | ||
|
||
genrule( | ||
name = "a-desired", | ||
outs = ["a-desired.js"], | ||
cmd = "echo 'console.log(\"a*\")' > $@", | ||
) | ||
|
||
genrule( | ||
name = "b-desired", | ||
outs = ["b-desired.js"], | ||
cmd = "echo 'console.log(\"b*\")' > $@", | ||
) | ||
|
||
write_source_files_test( | ||
name = "write_to_source_files_test", | ||
in_files = [ | ||
":a-desired", | ||
":b-desired", | ||
], | ||
out_files = [ | ||
"a.js", | ||
"b.js", | ||
], | ||
) | ||
|
||
write_source_files( | ||
name = "macro_smoke_test", | ||
files = { | ||
"a2.js": ":a-desired", | ||
"b2.js": ":b-desired", | ||
}, | ||
) |
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 @@ | ||
console.log("a"); |
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 @@ | ||
console.log("a*") |
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 @@ | ||
console.log("b"); |
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 @@ | ||
console.log("b*") |
102 changes: 102 additions & 0 deletions
102
lib/tests/write_source_files/write_source_files_test.bzl
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,102 @@ | ||
"""Tests for write_source_files""" | ||
# Inspired by https://github.com/cgrindel/bazel-starlib/blob/main/updatesrc/private/updatesrc_update_test.bzl | ||
|
||
load("//lib/private:write_source_files.bzl", _lib = "write_source_files_lib") | ||
|
||
_write_source_files = rule( | ||
attrs = _lib.attrs, | ||
implementation = _lib.implementation, | ||
executable = True, | ||
) | ||
|
||
def _impl(ctx): | ||
test = ctx.actions.declare_file( | ||
ctx.label.name + "_test.sh", | ||
) | ||
|
||
ctx.actions.write( | ||
output = test, | ||
is_executable = True, | ||
content = """ | ||
#!/usr/bin/env bash | ||
set -o errexit -o nounset -o pipefail | ||
assert_different() { | ||
local in_file="${1}" | ||
local out_file="${2}" | ||
diff "${in_file}" "${out_file}" > /dev/null && (echo >&2 "Expected files to differ. in: ${in_file}, out: ${out_file}" && return -1) | ||
return 0 | ||
} | ||
assert_same() { | ||
local in_file="${1}" | ||
local out_file="${2}" | ||
diff "${in_file}" "${out_file}" || (echo >&2 "Expected files to be same. in: ${in_file}, out: ${out_file}" && return -1) | ||
} | ||
# Check that in and out files are different | ||
""" + "\n".join([ | ||
"assert_different {in_file} {out_file}".format( | ||
in_file = ctx.files.in_files[i].short_path, | ||
out_file = ctx.files.out_files[i].short_path, | ||
) | ||
for i in range(len(ctx.files.in_files)) | ||
]) + """ | ||
# Write to the source files | ||
{write_source_files} | ||
# Check that in and out files are the same | ||
""".format(write_source_files = ctx.file.write_source_files_target.short_path) + "\n".join([ | ||
"assert_same {in_file} {out_file}".format( | ||
in_file = ctx.files.in_files[i].short_path, | ||
out_file = ctx.files.out_files[i].short_path, | ||
) | ||
for i in range(len(ctx.files.in_files)) | ||
]), | ||
) | ||
|
||
return DefaultInfo( | ||
executable = test, | ||
runfiles = ctx.runfiles(files = [ctx.file.write_source_files_target] + ctx.files.in_files + ctx.files.out_files), | ||
) | ||
|
||
_write_source_files_test = rule( | ||
implementation = _impl, | ||
attrs = { | ||
"write_source_files_target": attr.label( | ||
allow_single_file = True, | ||
executable = True, | ||
cfg = "exec", | ||
), | ||
"out_files": attr.label_list( | ||
allow_files = True, | ||
allow_empty = False, | ||
mandatory = True, | ||
), | ||
"in_files": attr.label_list( | ||
allow_files = True, | ||
allow_empty = False, | ||
mandatory = True, | ||
), | ||
}, | ||
test = True, | ||
) | ||
|
||
def write_source_files_test(name, in_files, out_files): | ||
"""Stamp a write_source_files executable and a test to run against it""" | ||
|
||
_write_source_files( | ||
name = name + "_updater", | ||
out_files = out_files, | ||
in_files = in_files, | ||
is_windows = False, | ||
) | ||
|
||
# Note that for testing we update the source files in the sandbox, | ||
# not the actual source tree. | ||
_write_source_files_test( | ||
name = name, | ||
write_source_files_target = name + "_updater", | ||
out_files = out_files, | ||
in_files = in_files, | ||
) |
Oops, something went wrong.