diff --git a/.bazelignore b/.bazelignore new file mode 100644 index 0000000..2b29f27 --- /dev/null +++ b/.bazelignore @@ -0,0 +1 @@ +tests diff --git a/.github/workflows/licence-checker.yml b/.github/workflows/licence-checker.yml index ee7d545..f9a9f6f 100644 --- a/.github/workflows/licence-checker.yml +++ b/.github/workflows/licence-checker.yml @@ -25,3 +25,7 @@ jobs: run: bazel run :format.check - name: Run Licence Checker run: bazel run //:licence-check + - name: Test Licence Checker + run: | + cd tests + bazel test :licence_check diff --git a/licence-checker/BUILD b/licence-checker/BUILD index 4ced674..a85c7f3 100644 --- a/licence-checker/BUILD +++ b/licence-checker/BUILD @@ -5,6 +5,7 @@ package(default_visibility = ["//visibility:public"]) load("@lowrisc_misc_linters_pip//:requirements.bzl", "requirement") +load("@rules_python//python:py_binary.bzl", "py_binary") py_binary( name = "licence-checker", diff --git a/rules/licence-checker-runner.template.sh b/rules/licence-checker-runner.template.sh index 015a5c4..a62ddd6 100644 --- a/rules/licence-checker-runner.template.sh +++ b/rules/licence-checker-runner.template.sh @@ -3,6 +3,10 @@ # Licensed under the Apache License, Version 2.0, see LICENSE for details. # SPDX-License-Identifier: Apache-2.0 +# Remember the runfiles dir since this is what paths like "@@LICENCE_CHECKER@@" +# and "@@CONFIG@@" are relative to once we `cd` elsewhere. +RUNFILES_DIR="$PWD" + WORKSPACE="@@WORKSPACE@@" if [[ ! -z "${WORKSPACE}" ]]; then @@ -16,4 +20,4 @@ else exit 1 fi -"@@LICENCE_CHECKER@@" --config="@@CONFIG@@" "$@" +"${RUNFILES_DIR}/@@LICENCE_CHECKER@@" --config="${RUNFILES_DIR}/@@CONFIG@@" "$@" diff --git a/rules/rules.bzl b/rules/rules.bzl index 39858b7..f87c557 100644 --- a/rules/rules.bzl +++ b/rules/rules.bzl @@ -18,34 +18,20 @@ def _licence_check_impl(ctx): }, ) - # Hack to make Bazel build the checker correctly. - # - # Bazel py_binaries require a .runfiles directory to be present, but for - # some reason or another it does not provide a good way to extract those - # for building as a dependency from a PyInfo provider. - # - # https://github.com/bazelbuild/bazel/issues/7357 - checker = ctx.actions.declare_file(ctx.label.name + ".checker-witness") - ctx.actions.run_shell( - tools = [ctx.executable.licence_check], - outputs = [checker], - command = 'touch "{}"'.format(checker.path), - ) - workspace = ctx.file.workspace.path if ctx.file.workspace else "" script = ctx.actions.declare_file(ctx.label.name + ".bash") ctx.actions.expand_template( template = ctx.file._runner, output = script, substitutions = { - "@@LICENCE_CHECKER@@": ctx.executable.licence_check.path, - "@@CONFIG@@": config.path, + "@@LICENCE_CHECKER@@": ctx.executable.licence_check.short_path, + "@@CONFIG@@": config.short_path, "@@WORKSPACE@@": workspace, }, is_executable = True, ) - files = [config, checker] + files = [config] if ctx.file.workspace: files.append(ctx.file.workspace) diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..648fc0e --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +MODULE.bazel.lock diff --git a/tests/BUILD b/tests/BUILD new file mode 100644 index 0000000..bbb5512 --- /dev/null +++ b/tests/BUILD @@ -0,0 +1,16 @@ +# Copyright lowRISC contributors. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +load("@lowrisc_misc_linters//rules:rules.bzl", "licence_test") + +licence_test( + name = "licence_check", + exclude_patterns = ["file_without_licence.txt"], + licence = """ + Copyright lowRISC contributors. + Licensed under the Apache License, Version 2.0, see LICENSE for details. + SPDX-License-Identifier: Apache-2.0 + """, + workspace = ":MODULE.bazel", +) diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel new file mode 100644 index 0000000..076dc60 --- /dev/null +++ b/tests/MODULE.bazel @@ -0,0 +1,13 @@ +# Copyright lowRISC contributors. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +module(name = "lowrisc_misc_linters_tests") + +bazel_dep(name = "lowrisc_misc_linters") +local_path_override(module_name = "lowrisc_misc_linters", path = "../") + +bazel_dep(name = "rules_python", version = "0.34.0") +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain(python_version = "3.9") + diff --git a/tests/WORKSPACE b/tests/WORKSPACE new file mode 100644 index 0000000..9d4e96b --- /dev/null +++ b/tests/WORKSPACE @@ -0,0 +1,17 @@ +# Copyright lowRISC contributors. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +local_repository( + name = "lowrisc_misc_linters", + path = "..", +) + +load("@lowrisc_misc_linters//rules:repos.bzl", "lowrisc_misc_linters_repos") +lowrisc_misc_linters_repos() +load("@lowrisc_misc_linters//rules:deps.bzl", "lowrisc_misc_linters_dependencies") +lowrisc_misc_linters_dependencies() +load("@lowrisc_misc_linters//rules:pip.bzl", "lowrisc_misc_linters_pip_dependencies") +lowrisc_misc_linters_pip_dependencies() +load("@lowrisc_misc_linters_pip//:requirements.bzl", "install_deps") +install_deps() diff --git a/tests/file_with_licence.txt b/tests/file_with_licence.txt new file mode 100644 index 0000000..4ade057 --- /dev/null +++ b/tests/file_with_licence.txt @@ -0,0 +1,5 @@ +# Copyright lowRISC contributors. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +This file has a licence! diff --git a/tests/file_without_licence.txt b/tests/file_without_licence.txt new file mode 100644 index 0000000..2769225 --- /dev/null +++ b/tests/file_without_licence.txt @@ -0,0 +1 @@ +This file intentionally has no licence.