diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ed5bc714..d4154172 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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/setup-bats-libs@0.0.3 + 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 diff --git a/example/.bazelrc b/example/.bazelrc index 477691a9..613f69b4 100644 --- a/example/.bazelrc +++ b/example/.bazelrc @@ -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 diff --git a/example/lint.sh b/example/lint.sh index 72ce8cc5..76c1f447 100755 --- a/example/lint.sh +++ b/example/lint.sh @@ -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 @@ -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 @@ -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 diff --git a/example/test/lint_test.bats b/example/test/lint_test.bats new file mode 100644 index 00000000..62952965 --- /dev/null +++ b/example/test/lint_test.bats @@ -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 +}