Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .bazelignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.ci/micromamba
test/unit/external_repository
3 changes: 2 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ toolchain(
toolchain_type = ":toolchain_type",
)

# Repository root marker, used by buildifier and pylint tests
# Repository root marker, used by the buildifier, pylint, and external_repository tests
exports_files(
["MODULE.bazel"],
visibility = [
"//test/buildifier:__pkg__",
"//test/pylint:__pkg__",
"//test/unit/external_repository:__pkg__",
],
)
74 changes: 39 additions & 35 deletions test/unit/external_repository/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Ericsson AB
# Copyright 2026 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,49 +13,53 @@
# limitations under the License.

load(
"@rules_cc//cc:defs.bzl",
"cc_binary",
"cc_library",
"//test/unit/external_repository:external_test.bzl",
"external_test",
)
load(
"@rules_codechecker//:defs.bzl",
"codechecker_test",
"compile_commands",

exports_files([
"external_test_runner.py",
])

# Source files made available to the inner build via the test runfiles
filegroup(
name = "external_test_srcs",
srcs = [
"intermediate.cpp",
"intermediate.h",
"main.cc",
],
)

# This is the important bit -- not only do we have an external dependency here
# in the form of external_lib, but is also an implementation_dep. This is a
# tricky edge case we need to handle.
cc_library(
name = "intermediate_lib",
srcs = ["intermediate.cpp"],
hdrs = ["intermediate.h"],
implementation_deps = ["@external_lib//:lib"],
visibility = ["//visibility:public"],
external_test(
name = "compile_commands_isystem_test",
action = "build",
Comment thread
furtib marked this conversation as resolved.
contains = [
"-isystem external/external_lib[~+]/include",
"-isystem bazel-out/k8-fastbuild/bin/external/external_lib[~+]/include",
],
output_file = "bazel-bin/compile_commands_isystem/compile_commands.json",
target = ":compile_commands_isystem",
)

cc_binary(
name = "isystem_impl_dep",
srcs = ["main.cc"],
deps = [":intermediate_lib"],
external_test(
name = "codechecker_external_deps_test",
target = ":codechecker_external_deps",
)

compile_commands(
name = "compile_commands_isystem",
targets = [
":isystem_impl_dep",
],
external_test(
name = "per_file_external_deps_test",
target = ":per_file_external_deps",
)

codechecker_test(
name = "codechecker_external_deps",
tags = ["manual"],
targets = ["isystem_impl_dep"],
external_test(
name = "codechecker_external_include_paths_test",
extra_flags = ["--features=external_include_paths"],
target = ":codechecker_external_deps",
)

codechecker_test(
name = "per_file_external_deps",
per_file = True,
tags = ["manual"],
targets = ["isystem_impl_dep"],
external_test(
name = "per_file_external_include_paths_test",
extra_flags = ["--features=external_include_paths"],
target = ":per_file_external_deps",
)
33 changes: 0 additions & 33 deletions test/unit/external_repository/MODULE.bazel

This file was deleted.

1 change: 0 additions & 1 deletion test/unit/external_repository/WORKSPACE

This file was deleted.

102 changes: 102 additions & 0 deletions test/unit/external_repository/external_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright 2026 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Macro for external repository integration tests.

Each external_test() generates a py_test that:
1. Creates a temporary Bazel workspace with external dependencies
2. Runs a bazel command inside it
3. Asserts on exit code and optionally on output file contents

Example:
external_test(
name = "compile_commands_test",
target = ":compile_commands_isystem",
action = "build",
extra_flags = ["--features=external_include_paths"],
output_file = "bazel-bin/compile_commands_isystem/compile_commands.json",
contains = ["-isystem external/external_lib"],
tags = ["manual"],
size = "large",
)
"""

load("@rules_python//python:py_test.bzl", "py_test")

# Source files made available to the inner build via the test runfiles,
# alongside the runner. Declared as a filegroup in the BUILD file.
_SRCS = "//test/unit/external_repository:external_test_srcs"

def external_test(
name,
target,
action = "test",
extra_flags = [],
output_file = None,
contains = None,
tags = [],
size = "large",
**kwargs):
"""Generate a py_test that runs a bazel command in a temp workspace.

Args:
name: Test name.
target: Bazel target to act on (e.g. ":codechecker_external_deps").
action: Bazel action to run (default: "test").
extra_flags: Additional flags to pass to the bazel command.
output_file: Optional relative path to an output file to check.
contains: Optional list of regex patterns to find in the output file.
tags: Additional test tags.
size: Test size (default: large).
**kwargs: Forwarded to py_test.
"""
if type(contains) == "string":
contains = [contains]
if type(extra_flags) == "string":
extra_flags = [extra_flags]

python_args = [
"--repo_root",
"$(rootpath //:MODULE.bazel)",
"--action",
action,
"--target",
target,
]

# Output file assertions
if output_file:
python_args.extend(["--output_file", output_file])
if contains:
python_args.append("--contains")
python_args.extend(["'{}'".format(pat) for pat in contains])

# Extra flags go after -- to avoid argparse confusion with --flags
if extra_flags:
python_args.append("--")
python_args.extend(extra_flags)

py_test(
name = name,
srcs = ["//test/unit/external_repository:external_test_runner.py"],
main = "//test/unit/external_repository:external_test_runner.py",
args = python_args,
data = [_SRCS, "//:MODULE.bazel"],
# No other way to ensure integrated bazel can find the repository
local = True,
size = size,
tags = tags,
**kwargs
)
Loading
Loading