Skip to content

Commit

Permalink
Add tests for custom mnemonics
Browse files Browse the repository at this point in the history
Adds tests to verify custom mnemonics set on `copy_file`,
`copy_directory` and `run_binary`.
  • Loading branch information
joshua-k-harris committed Mar 14, 2024
1 parent 65c8f18 commit 73b8807
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/copy_directory/BUILD.bazel
@@ -1,6 +1,7 @@
# This package aids testing the 'copy_directory' rule.

load("//rules:copy_directory.bzl", "copy_directory")
load("//tests/copy_directory:mnemonic_test.bzl", "copy_directory_mnemonic_test")
load(":empty_directory.bzl", "empty_directory")

licenses(["notice"])
Expand Down Expand Up @@ -41,3 +42,16 @@ sh_test(
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

copy_directory(
name = "copy_directory_mnemonic_test_target",
src = "dir_with_subdir",
out = "copied_dir_with_subdir",
mnemonic = "FooBar",
tags = ["manual"],
)

copy_directory_mnemonic_test(
name = "copy_directory_mnemonic_test",
target_under_test = ":copy_directory_mnemonic_test_target",
)
30 changes: 30 additions & 0 deletions tests/copy_directory/mnemonic_test.bzl
@@ -0,0 +1,30 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# 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.

"""Custom mnemonic tests for copy_directory.bzl"""

load("//lib:unittest.bzl", "analysistest", "asserts")
load("//rules:copy_directory.bzl", "copy_directory")

def _copy_directory_mnemonic_test_impl(ctx):
env = analysistest.begin(ctx)

expected_mmemonic = "FooBar"
actual_mnemonic = analysistest.target_actions(env)[0].mnemonic

asserts.equals(env, expected_mmemonic, actual_mnemonic)

return analysistest.end(env)

copy_directory_mnemonic_test = analysistest.make(_copy_directory_mnemonic_test_impl)
14 changes: 14 additions & 0 deletions tests/copy_file/BUILD
Expand Up @@ -32,6 +32,7 @@
# of it, so we assert that that field contains the output file of the rule

load("//rules:copy_file.bzl", "copy_file")
load("//tests/copy_file:mnemonic_test.bzl", "copy_file_mnemonic_test")

licenses(["notice"])

Expand Down Expand Up @@ -171,3 +172,16 @@ genrule(
outs = ["b.txt"],
cmd = "echo -e '#!/usr/bin/env bash\necho potato' > $@",
)

copy_file(
name = "copy_file_mnemonic_test_target",
src = "foo.txt",
out = "bar.txt",
mnemonic = "FooBar",
tags = ["manual"],
)

copy_file_mnemonic_test(
name = "copy_file_mnemonic_test",
target_under_test = ":copy_file_mnemonic_test_target",
)
30 changes: 30 additions & 0 deletions tests/copy_file/mnemonic_test.bzl
@@ -0,0 +1,30 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# 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.

"""Custom mnemonic tests for copy_file.bzl"""

load("//lib:unittest.bzl", "analysistest", "asserts")
load("//rules:copy_file.bzl", "copy_file")

def _copy_file_mnemonic_test_impl(ctx):
env = analysistest.begin(ctx)

expected_mmemonic = "FooBar"
actual_mnemonic = analysistest.target_actions(env)[0].mnemonic

asserts.equals(env, expected_mmemonic, actual_mnemonic)

return analysistest.end(env)

copy_file_mnemonic_test = analysistest.make(_copy_file_mnemonic_test_impl)
19 changes: 19 additions & 0 deletions tests/run_binary/BUILD
Expand Up @@ -2,6 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//rules:diff_test.bzl", "diff_test")
load("//rules:run_binary.bzl", "run_binary")
load("//rules:write_file.bzl", "write_file")
load("//tests/run_binary:mnemonic_test.bzl", "run_binary_mnemonic_test")

package(
default_testonly = 1,
Expand Down Expand Up @@ -165,3 +166,21 @@ cc_binary(
name = "printargs",
srcs = ["printargs.cc"],
)

cc_binary(
name = "hello",
srcs = ["hello.cc"],
)

run_binary(
name = "run_binary_test_target",
outs = ["hello.out"],
mnemonic = "FooBar",
tags = ["manual"],
tool = ":hello",
)

run_binary_mnemonic_test(
name = "run_binary_mnemonic_test",
target_under_test = ":run_binary_test_target",
)
20 changes: 20 additions & 0 deletions tests/run_binary/hello.cc
@@ -0,0 +1,20 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// 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.

#include <stdio.h>

int main() {
printf("Hello, world!");
return 0;
}
30 changes: 30 additions & 0 deletions tests/run_binary/mnemonic_test.bzl
@@ -0,0 +1,30 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# 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.

"""Custom mnemonic tests for run_binary.bzl"""

load("//lib:unittest.bzl", "analysistest", "asserts")
load("//rules:run_binary.bzl", "run_binary")

def _run_binary_mnemonic_test_impl(ctx):
env = analysistest.begin(ctx)

expected_mmemonic = "FooBar"
actual_mnemonic = analysistest.target_actions(env)[0].mnemonic

asserts.equals(env, expected_mmemonic, actual_mnemonic)

return analysistest.end(env)

run_binary_mnemonic_test = analysistest.make(_run_binary_mnemonic_test_impl)

0 comments on commit 73b8807

Please sign in to comment.