Skip to content

Commit 6979328

Browse files
Add tests for custom mnemonics
Adds tests to verify custom mnemonics set on `copy_file`, `copy_directory` and `run_binary`.
1 parent 65c8f18 commit 6979328

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

tests/copy_directory/BUILD.bazel

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This package aids testing the 'copy_directory' rule.
22

33
load("//rules:copy_directory.bzl", "copy_directory")
4+
load("//tests/copy_directory:mnemonic_test.bzl", "copy_directory_mnemonic_test")
45
load(":empty_directory.bzl", "empty_directory")
56

67
licenses(["notice"])
@@ -41,3 +42,16 @@ sh_test(
4142
],
4243
deps = ["@bazel_tools//tools/bash/runfiles"],
4344
)
45+
46+
copy_directory(
47+
name = "copy_directory_mnemonic_test_target",
48+
src = "dir_with_subdir",
49+
out = "copied_dir_with_subdir",
50+
mnemonic = "FooBar",
51+
tags = ["manual"],
52+
)
53+
54+
copy_directory_mnemonic_test(
55+
name = "copy_directory_mnemonic_test",
56+
target_under_test = ":copy_directory_mnemonic_test_target",
57+
)
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Custom mnemonic tests for copy_directory.bzl"""
16+
17+
load("//lib:unittest.bzl", "analysistest", "asserts")
18+
19+
def _copy_directory_mnemonic_test_impl(ctx):
20+
env = analysistest.begin(ctx)
21+
22+
expected_mmemonic = "FooBar"
23+
actual_mnemonic = analysistest.target_actions(env)[0].mnemonic
24+
25+
asserts.equals(env, expected_mmemonic, actual_mnemonic)
26+
27+
return analysistest.end(env)
28+
29+
copy_directory_mnemonic_test = analysistest.make(_copy_directory_mnemonic_test_impl)

tests/copy_file/BUILD

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# of it, so we assert that that field contains the output file of the rule
3333

3434
load("//rules:copy_file.bzl", "copy_file")
35+
load("//tests/copy_file:mnemonic_test.bzl", "copy_file_mnemonic_test")
3536

3637
licenses(["notice"])
3738

@@ -171,3 +172,16 @@ genrule(
171172
outs = ["b.txt"],
172173
cmd = "echo -e '#!/usr/bin/env bash\necho potato' > $@",
173174
)
175+
176+
copy_file(
177+
name = "copy_file_mnemonic_test_target",
178+
src = "foo.txt",
179+
out = "bar.txt",
180+
mnemonic = "FooBar",
181+
tags = ["manual"],
182+
)
183+
184+
copy_file_mnemonic_test(
185+
name = "copy_file_mnemonic_test",
186+
target_under_test = ":copy_file_mnemonic_test_target",
187+
)

tests/copy_file/mnemonic_test.bzl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Custom mnemonic tests for copy_file.bzl"""
16+
17+
load("//lib:unittest.bzl", "analysistest", "asserts")
18+
19+
def _copy_file_mnemonic_test_impl(ctx):
20+
env = analysistest.begin(ctx)
21+
22+
expected_mmemonic = "FooBar"
23+
actual_mnemonic = analysistest.target_actions(env)[0].mnemonic
24+
25+
asserts.equals(env, expected_mmemonic, actual_mnemonic)
26+
27+
return analysistest.end(env)
28+
29+
copy_file_mnemonic_test = analysistest.make(_copy_file_mnemonic_test_impl)

tests/run_binary/BUILD

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
22
load("//rules:diff_test.bzl", "diff_test")
33
load("//rules:run_binary.bzl", "run_binary")
44
load("//rules:write_file.bzl", "write_file")
5+
load("//tests/run_binary:mnemonic_test.bzl", "run_binary_mnemonic_test")
56

67
package(
78
default_testonly = 1,
@@ -165,3 +166,21 @@ cc_binary(
165166
name = "printargs",
166167
srcs = ["printargs.cc"],
167168
)
169+
170+
cc_binary(
171+
name = "hello",
172+
srcs = ["hello.cc"],
173+
)
174+
175+
run_binary(
176+
name = "run_binary_test_target",
177+
outs = ["hello.out"],
178+
mnemonic = "FooBar",
179+
tags = ["manual"],
180+
tool = ":hello",
181+
)
182+
183+
run_binary_mnemonic_test(
184+
name = "run_binary_mnemonic_test",
185+
target_under_test = ":run_binary_test_target",
186+
)

tests/run_binary/hello.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2019 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <stdio.h>
16+
17+
int main() {
18+
printf("Hello, world!");
19+
return 0;
20+
}

tests/run_binary/mnemonic_test.bzl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Custom mnemonic tests for run_binary.bzl"""
16+
17+
load("//lib:unittest.bzl", "analysistest", "asserts")
18+
19+
def _run_binary_mnemonic_test_impl(ctx):
20+
env = analysistest.begin(ctx)
21+
22+
expected_mmemonic = "FooBar"
23+
actual_mnemonic = analysistest.target_actions(env)[0].mnemonic
24+
25+
asserts.equals(env, expected_mmemonic, actual_mnemonic)
26+
27+
return analysistest.end(env)
28+
29+
run_binary_mnemonic_test = analysistest.make(_run_binary_mnemonic_test_impl)

0 commit comments

Comments
 (0)