From 1da6be6c057d964c1247c310e6294571e78e6a85 Mon Sep 17 00:00:00 2001 From: yuyawk <46620009+yuyawk@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:51:38 +0900 Subject: [PATCH] test: Add an example module (#13) --- .bazelignore | 1 + .github/workflows/tests.yml | 16 ++++++++-- MODULE.bazel | 13 ++++++-- README.md | 4 +-- examples/.gitignore | 10 +++++++ examples/BUILD.bazel | 0 examples/MODULE.bazel | 27 +++++++++++++++++ examples/cc/BUILD.bazel | 8 +++++ examples/cc/compile_error.cpp | 4 +++ .../execute_tests.bash | 0 style_check.bash => scripts/style_check.bash | 6 +++- scripts/validate_example.bash | 30 +++++++++++++++++++ tests/cc/c_compile_error/BUILD.bazel | 2 +- .../cc/c_compile_error_with_deps/BUILD.bazel | 2 +- tests/cc/c_link_error/BUILD.bazel | 2 +- tests/cc/c_link_error_with_deps/BUILD.bazel | 2 +- tests/cc/cpp_compile_error/BUILD.bazel | 2 +- .../BUILD.bazel | 2 +- .../cpp_compile_error_with_deps/BUILD.bazel | 2 +- .../BUILD.bazel | 2 +- tests/cc/cpp_link_error/BUILD.bazel | 2 +- tests/cc/cpp_link_error_with_deps/BUILD.bazel | 2 +- 22 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 examples/.gitignore create mode 100644 examples/BUILD.bazel create mode 100644 examples/MODULE.bazel create mode 100644 examples/cc/BUILD.bazel create mode 100644 examples/cc/compile_error.cpp rename execute_tests.bash => scripts/execute_tests.bash (100%) rename style_check.bash => scripts/style_check.bash (71%) create mode 100755 scripts/validate_example.bash diff --git a/.bazelignore b/.bazelignore index 16d3c4d..4ae507f 100644 --- a/.bazelignore +++ b/.bazelignore @@ -1 +1,2 @@ .cache +examples diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8152d43..e09f25d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,7 +21,19 @@ jobs: - name: Set up uses: ./.github/actions/setup - name: Run unit tests - run: ./execute_tests.bash + run: ./scripts/execute_tests.bash + + example: + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + - name: Set up + uses: ./.github/actions/setup + - name: Validate example + run: ./scripts/validate_example.bash style-check: runs-on: ubuntu-22.04 @@ -33,7 +45,7 @@ jobs: - name: Set up uses: ./.github/actions/setup - name: Run formatters and linters - run: ./style_check.bash + run: ./scripts/style_check.bash - name: Fail if some files are changed run: | if ! git diff --exit-code; then diff --git a/MODULE.bazel b/MODULE.bazel index 2340c44..ecaadd1 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -3,18 +3,25 @@ module( name = "rules_build_error", - version = "0.1.1", + version = "0.0.0", ) bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True) bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True) -llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm") +llvm = use_extension( + "@toolchains_llvm//toolchain/extensions:llvm.bzl", + "llvm", + dev_dependency = True, +) llvm.toolchain( llvm_version = "17.0.6", ) use_repo(llvm, "llvm_toolchain") -register_toolchains("@llvm_toolchain//:all") +register_toolchains( + "@llvm_toolchain//:all", + dev_dependency = True, +) bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True) diff --git a/README.md b/README.md index 0f3d440..e559c70 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,9 @@ In order to specify how to validate the error message, a struct `matcher` is ava ### How to test -Execute [`execute_tests.bash`](execute_tests.bash) after installing [`bazelisk`](https://github.com/bazelbuild/bazelisk). It executes `bazelisk test` and `bazelisk build` commands under the hood. +Execute [`scripts/execute_tests.bash`](scripts/execute_tests.bash) after installing [`bazelisk`](https://github.com/bazelbuild/bazelisk). It executes `bazelisk test` and `bazelisk build` commands under the hood. -When writing tests, in principle, use `tags = ["manual"]` if a test case target must fail with `bazelisk test`. In such a test case, confirm its failure in [`execute_tests.bash`](execute_tests.bash) one by one. +When writing tests, in principle, use `tags = ["manual"]` if a test case target must fail with `bazelisk test`. In such a test case, confirm its failure in [`scripts/execute_tests.bash`](scripts/execute_tests.bash) one by one. ## CI diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..6713d5e --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,10 @@ +# Copied from the original workspace +.bazelrc +.bazeliskrc +.bazelignore + +# Bazel +/bazel-* +MODULE.bazel.lock + +/.cache diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel new file mode 100644 index 0000000..1dd6828 --- /dev/null +++ b/examples/MODULE.bazel @@ -0,0 +1,27 @@ +"""Test module for `rules_build_error`. +""" + +module( + name = "rules_build_error_examples", + version = "0.0.0", +) + +bazel_dep(name = "rules_build_error", version = "0.0.0") +local_path_override( + module_name = "rules_build_error", + path = "..", +) + +# Register LLVM toolchain for C/C++ +bazel_dep(name = "toolchains_llvm", version = "1.0.0") + +llvm = use_extension( + "@toolchains_llvm//toolchain/extensions:llvm.bzl", + "llvm", +) +llvm.toolchain( + llvm_version = "17.0.6", +) +use_repo(llvm, "llvm_toolchain") + +register_toolchains("@llvm_toolchain//:all") diff --git a/examples/cc/BUILD.bazel b/examples/cc/BUILD.bazel new file mode 100644 index 0000000..48cc19d --- /dev/null +++ b/examples/cc/BUILD.bazel @@ -0,0 +1,8 @@ +load("@rules_build_error//lang/cc:defs.bzl", "cc_build_error") +load("@rules_build_error//matcher:defs.bzl", "matcher") + +cc_build_error( + name = "example", + src = "compile_error.cpp", + compile_stderr = matcher.has_substr("Example compile error"), +) diff --git a/examples/cc/compile_error.cpp b/examples/cc/compile_error.cpp new file mode 100644 index 0000000..a134c02 --- /dev/null +++ b/examples/cc/compile_error.cpp @@ -0,0 +1,4 @@ +int main() +{ + static_assert(false, "Example compile error message"); +} diff --git a/execute_tests.bash b/scripts/execute_tests.bash similarity index 100% rename from execute_tests.bash rename to scripts/execute_tests.bash diff --git a/style_check.bash b/scripts/style_check.bash similarity index 71% rename from style_check.bash rename to scripts/style_check.bash index 10b24c2..4602f77 100755 --- a/style_check.bash +++ b/scripts/style_check.bash @@ -15,6 +15,10 @@ BAZEL_EXECUTABLE=( bazelisk ) -buildifier_targets="$(git ls-files *.bzl *.bazel BUILD WORKSPACE MODULE | xargs -I{} echo "$(pwd)/{}")" +buildifier_targets="$( + git ls-files | \ + grep -E '.bzl$|.bazel$|BUILD$|WORKSPACE$|WORKSPACE.bzlmod$' | \ + xargs -I{} echo "$(pwd)/{}" +)" "${BAZEL_EXECUTABLE[@]}" run -- @buildifier_prebuilt//:buildifier -lint=fix ${buildifier_targets} diff --git a/scripts/validate_example.bash b/scripts/validate_example.bash new file mode 100755 index 0000000..e406767 --- /dev/null +++ b/scripts/validate_example.bash @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +# Validate example directory + +set -euo pipefail + +# Bazel executable with some arguments +BAZEL_EXECUTABLE=( + "env" + "-i" + BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 + BAZELISK_HOME=../.cache/bazelisk + "HOME=${HOME}" + "PATH=${PATH}" + bazelisk +) + +copied_files=( + ".bazelignore" + ".bazeliskrc" + ".bazelrc" +) + +for copied_file in "${copied_files[@]}"; do + cp "${copied_file}" examples +done + +cd examples + +"${BAZEL_EXECUTABLE[@]}" build //... diff --git a/tests/cc/c_compile_error/BUILD.bazel b/tests/cc/c_compile_error/BUILD.bazel index 9185521..9845695 100644 --- a/tests/cc/c_compile_error/BUILD.bazel +++ b/tests/cc/c_compile_error/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_build_error( name = "plain", diff --git a/tests/cc/c_compile_error_with_deps/BUILD.bazel b/tests/cc/c_compile_error_with_deps/BUILD.bazel index f684e8a..852831d 100644 --- a/tests/cc/c_compile_error_with_deps/BUILD.bazel +++ b/tests/cc/c_compile_error_with_deps/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_library( name = "library", diff --git a/tests/cc/c_link_error/BUILD.bazel b/tests/cc/c_link_error/BUILD.bazel index 911d165..d5b1352 100644 --- a/tests/cc/c_link_error/BUILD.bazel +++ b/tests/cc/c_link_error/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_build_error( name = "plain", diff --git a/tests/cc/c_link_error_with_deps/BUILD.bazel b/tests/cc/c_link_error_with_deps/BUILD.bazel index 397f721..3e7496b 100644 --- a/tests/cc/c_link_error_with_deps/BUILD.bazel +++ b/tests/cc/c_link_error_with_deps/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_library( name = "library", diff --git a/tests/cc/cpp_compile_error/BUILD.bazel b/tests/cc/cpp_compile_error/BUILD.bazel index 560b33e..c90054e 100644 --- a/tests/cc/cpp_compile_error/BUILD.bazel +++ b/tests/cc/cpp_compile_error/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_build_error( name = "plain", diff --git a/tests/cc/cpp_compile_error_defines_is_transitive/BUILD.bazel b/tests/cc/cpp_compile_error_defines_is_transitive/BUILD.bazel index 4ade5b5..c925ba3 100644 --- a/tests/cc/cpp_compile_error_defines_is_transitive/BUILD.bazel +++ b/tests/cc/cpp_compile_error_defines_is_transitive/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_library( name = "library", diff --git a/tests/cc/cpp_compile_error_with_deps/BUILD.bazel b/tests/cc/cpp_compile_error_with_deps/BUILD.bazel index 41ac6ef..29b4c12 100644 --- a/tests/cc/cpp_compile_error_with_deps/BUILD.bazel +++ b/tests/cc/cpp_compile_error_with_deps/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_library( name = "library", diff --git a/tests/cc/cpp_compile_error_with_local_defines/BUILD.bazel b/tests/cc/cpp_compile_error_with_local_defines/BUILD.bazel index eaabd2f..5c2fcfb 100644 --- a/tests/cc/cpp_compile_error_with_local_defines/BUILD.bazel +++ b/tests/cc/cpp_compile_error_with_local_defines/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_build_error( name = "plain", diff --git a/tests/cc/cpp_link_error/BUILD.bazel b/tests/cc/cpp_link_error/BUILD.bazel index 7adeb57..d494cc1 100644 --- a/tests/cc/cpp_link_error/BUILD.bazel +++ b/tests/cc/cpp_link_error/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_build_error( name = "plain", diff --git a/tests/cc/cpp_link_error_with_deps/BUILD.bazel b/tests/cc/cpp_link_error_with_deps/BUILD.bazel index 8443099..de3badd 100644 --- a/tests/cc/cpp_link_error_with_deps/BUILD.bazel +++ b/tests/cc/cpp_link_error_with_deps/BUILD.bazel @@ -1,6 +1,6 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//lang/cc:defs.bzl", "cc_build_error") load("//matcher:defs.bzl", "matcher") -load("@bazel_skylib//rules:build_test.bzl", "build_test") cc_library( name = "library",