Skip to content

Commit

Permalink
ci: add integration testing (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Dec 1, 2023
1 parent 918fab8 commit d4f456e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@ concurrency:
cancel-in-progress: true

jobs:
test:
bazel-test:
uses: bazel-contrib/.github/.github/workflows/bazel.yaml@v5
with:
folders: '[".", "example"]'
# Only test with Bazel 6. And we don't try for Windows support yet.
exclude: '[{"bazelversion": "5.4.0"}, {"os": "windows-latest"}]'

integration-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup bats
uses: mig4/setup-bats@v1
with:
bats-version: "1.8.2"
- name: Setup bats helpers
uses: brokenpip3/[email protected]
with:
support-path: /usr/lib/bats/bats-support
support-version: "0.3.0"
assert-path: /usr/lib/bats/bats-assert
assert-version: "2.1.0"
- name: Integration test
working-directory: example
run: bats ./test
2 changes: 2 additions & 0 deletions example/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Don't depend on a JAVA_HOME pointing at a system JDK
# see https://github.com/bazelbuild/rules_jvm_external/issues/445
build --repo_env=JAVA_HOME=../bazel_tools/jdk

startup --host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1
32 changes: 18 additions & 14 deletions example/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This is meant to mimic the behavior of the `bazel lint` command that you'd have
# by using the Aspect CLI.
#
# To make the build fail when a linter warning is present, run with --fail-on-violation
#
# We recommend using Aspect CLI instead!
set -o errexit -o pipefail -o nounset

Expand All @@ -15,19 +17,25 @@ fi
buildevents=$(mktemp)
filter='.namedSetOfFiles | values | .files[] | ((.pathPrefix | join("/")) + "/" + .name)'

# Produce report files
# To make the command fail when there's a lint warning, you can add arguments:
# --aspects_parameters=fail_on_violation=true --keep_going
# NB: perhaps --remote_download_toplevel is needed as well with remote execution?
bazel build \
--aspects $(echo //tools:lint.bzl%{buf,eslint,flake8,pmd,ruff,shellcheck} | tr ' ' ',') \
--build_event_json_file="$buildevents" \
--output_groups=rules_lint_report \
--remote_download_regex='.*aspect_rules_lint.report' \
$@
args=(
"--aspects=$(echo //tools:lint.bzl%{buf,eslint,flake8,pmd,ruff,shellcheck} | tr ' ' ',')"
"--build_event_json_file=$buildevents"
"--output_groups=rules_lint_report"
"--remote_download_regex='.*aspect_rules_lint.report'"
)
if [ $1 == "--fail-on-violation" ]; then
args+=(
"--aspects_parameters=fail_on_violation=true"
"--keep_going"
)
shift
fi

# Produce report files
bazel build ${args[@]} $@

valid_reports=$(jq --raw-output "$filter" "$buildevents")
exit_code=0

# Show the results.
while IFS= read -r report; do
Expand All @@ -39,8 +47,4 @@ while IFS= read -r report; do
echo "From ${report}:"
cat "${report}"
echo

exit_code=1
done <<<"$valid_reports"

exit $exit_code
42 changes: 42 additions & 0 deletions example/test/lint_test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
bats_load_library "bats-support"
bats_load_library "bats-assert"

function assert_lints() {
# Shellcheck
echo <<"EOF" | assert_output --partial
In src/hello.sh line 3:
[ -z $THING ] && echo "hello world"
^----^ SC2086 (info): Double quote to prevent globbing and word splitting.
EOF

# Ruff
echo <<"EOF" | assert_output --partial
src/unused_import.py:13:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 fixable with the `--fix` option.
EOF

# Flake8
assert_output --partial "src/unused_import.py:13:1: F401 'os' imported but unused"

# PMD
assert_output --partial 'src/Foo.java:9: FinalizeOverloaded: Finalize methods should not be overloaded'

# ESLint
assert_output --partial 'src/file.ts:2:7: Type string trivially inferred from a string literal, remove type annotation [error from @typescript-eslint/no-inferrable-types]'

# Buf
assert_output --partial 'src/file.proto:1:1:Import "src/unused.proto" is unused.'
}

@test "should produce reports" {
run $BATS_TEST_DIRNAME/../lint.sh //src:all
assert_success
assert_lints
}

@test "should fail when --fail-on-violation is passed" {
run $BATS_TEST_DIRNAME/../lint.sh --fail-on-violation //src:all
assert_failure
assert_lints
}

0 comments on commit d4f456e

Please sign in to comment.