Skip to content

Commit

Permalink
test: Add an example module (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk authored Apr 10, 2024
1 parent 9368e27 commit 1da6be6
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 18 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.cache
examples
16 changes: 14 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copied from the original workspace
.bazelrc
.bazeliskrc
.bazelignore

# Bazel
/bazel-*
MODULE.bazel.lock

/.cache
Empty file added examples/BUILD.bazel
Empty file.
27 changes: 27 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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")
8 changes: 8 additions & 0 deletions examples/cc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"),
)
4 changes: 4 additions & 0 deletions examples/cc/compile_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main()
{
static_assert(false, "Example compile error message");
}
File renamed without changes.
6 changes: 5 additions & 1 deletion style_check.bash → scripts/style_check.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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}
30 changes: 30 additions & 0 deletions scripts/validate_example.bash
Original file line number Diff line number Diff line change
@@ -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 //...
2 changes: 1 addition & 1 deletion tests/cc/c_compile_error/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/c_compile_error_with_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/c_link_error/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/c_link_error_with_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/cpp_compile_error/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/cpp_compile_error_with_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/cpp_compile_error_with_local_defines/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/cpp_link_error/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/cc/cpp_link_error_with_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 1da6be6

Please sign in to comment.