Skip to content

Commit

Permalink
ci: add test coverage for format binary
Browse files Browse the repository at this point in the history
This is a beginning, if the pattern works I'll apply it to remaining languages.
Should be able to catch most regressions this way.
  • Loading branch information
alexeagle committed Jan 27, 2024
1 parent 5760fcb commit 4518100
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
support-version: "0.3.0"
assert-path: /usr/lib/bats/bats-assert
assert-version: "2.1.0"
- name: Integration test
- name: "Integration test: example"
working-directory: example
run: bats ./test
- name: "Integration test: format"
working-directory: format
run: bats ./test
2 changes: 2 additions & 0 deletions format/private/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function on_exit {

trap on_exit EXIT

set -o noglob

# ls-files <language> [<file>...]
function ls-files {
language="$1" && shift;
Expand Down
20 changes: 12 additions & 8 deletions format/private/formatter_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")

# Per the formatter design, each language can only have a single formatter binary
_TOOLS = {
TOOLS = {
"javascript": "prettier",
"markdown": "prettier-md",
"python": "ruff",
Expand All @@ -22,16 +22,20 @@ _TOOLS = {
}

def _formatter_binary_impl(ctx):
# We need to fill in the rlocation paths in the shell script
substitutions = {
"{{BASH_RLOCATION_FUNCTION}}": BASH_RLOCATION_FUNCTION,
"{{fix_target}}": str(ctx.label),
}
tools = {v: getattr(ctx.attr, k) for k, v in _TOOLS.items()}
substitutions = {}
tools = {v: getattr(ctx.attr, k) for k, v in TOOLS.items()}
for tool, attr in tools.items():
if attr:
substitutions["{{%s}}" % tool] = to_rlocation_path(ctx, attr.files_to_run.executable)
if len(substitutions) == 0:
fail("multi_formatter_binary should have at least one language attribute set to a formatter tool")

substitutions.update({
# We need to fill in the rlocation paths in the shell script
"{{BASH_RLOCATION_FUNCTION}}": BASH_RLOCATION_FUNCTION,
# Support helpful error reporting
"{{fix_target}}": str(ctx.label),
})
bin = ctx.actions.declare_file("format.sh")
ctx.actions.expand_template(
template = ctx.file._bin,
Expand Down Expand Up @@ -66,7 +70,7 @@ formatter_binary_lib = struct(
implementation = _formatter_binary_impl,
attrs = dict({
k: attr.label(doc = "a binary target that runs {} (or another tool with compatible CLI arguments)".format(v), executable = True, cfg = "exec", allow_files = True)
for k, v in _TOOLS.items()
for k, v in TOOLS.items()
}, **{
"_bin": attr.label(default = "//format/private:format.sh", allow_single_file = True),
"_runfiles_lib": attr.label(default = "@bazel_tools//tools/bash/runfiles", allow_single_file = True),
Expand Down
36 changes: 36 additions & 0 deletions format/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//format:defs.bzl", "multi_formatter_binary")
load("//format/private:formatter_binary.bzl", "TOOLS")

# Avoid depending on a bunch of actual tools in the root module.
# That's the job of the example/ submodule.
# Instead, just provide a tool that acts like any of the formatters we support.
[
write_file(
name = "fake_{}_sh".format(t),
out = "fake_{}.sh".format(t),
content = [
"#!/usr/bin/env bash",
"echo + {} $*".format(t),
],
)
for t in TOOLS.values()
]

[
sh_binary(
name = "fake_" + t,
srcs = ["fake_{}.sh".format(t)],
)
for t in TOOLS.values()
]

multi_formatter_binary(
name = "format_javascript",
javascript = ":fake_prettier",
)

multi_formatter_binary(
name = "format_starlark",
starlark = ":fake_buildifier",
)
29 changes: 29 additions & 0 deletions format/test/format_test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
bats_load_library "bats-support"
bats_load_library "bats-assert"

@test "should run prettier on javascript" {
run bazel run //format/test:format_javascript
assert_success

echo <<"EOF" | assert_output --partial
Formatting JavaScript with Prettier...
+ prettier --write example/.eslintrc.cjs
EOF
echo <<"EOF" | assert_output --partial
Formatting TypeScript with Prettier...
+ prettier --write example/src/file.ts example/test/no_violations.ts
EOF
echo <<"EOF" | assert_output --partial
Formatting TSX with Prettier...
+ prettier --write example/src/hello.tsx
EOF
}

@test "should run buildozer on starlark" {
run bazel run //format/test:format_starlark
assert_success
assert_output --partial "Formatting Starlark with Buildifier..."
assert_output --partial "+ buildifier -mode=fix BUILD.bazel"
assert_output --partial "format/private/BUILD.bazel"

}

0 comments on commit 4518100

Please sign in to comment.