blob: 4ca8b15395e6f2fb7caf461b5d354f1690ad167c [file] [log] [blame]
Dmitry Lomov430d8dc2015-11-18 17:42:25 +00001// Copyright 2015 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
15package com.google.devtools.build.lib.packages;
16
lberkiaea56b32017-05-30 12:35:33 +020017import static com.google.common.truth.Truth.assertThat;
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000018
adonovan0cf75b92020-09-29 14:55:42 -070019import com.google.common.collect.ImmutableSet;
20import com.google.devtools.build.lib.events.EventKind;
21import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase;
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000022import org.junit.Test;
23import org.junit.runner.RunWith;
24import org.junit.runners.JUnit4;
25
adonovan0cf75b92020-09-29 14:55:42 -070026/** Tests for {@link Rule}. */
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000027@RunWith(JUnit4.class)
adonovan0cf75b92020-09-29 14:55:42 -070028public class RuleTest extends PackageLoadingTestCase {
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000029
30 @Test
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000031 public void testOutputNameError() throws Exception {
adonovan0cf75b92020-09-29 14:55:42 -070032 reporter.removeHandler(failFastHandler);
33 scratch.file(
34 "namecollide/BUILD",
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000035 "genrule(name = 'hello_world',",
adonovan0cf75b92020-09-29 14:55:42 -070036 "srcs = ['ignore_me.txt'],",
37 "outs = ['message.txt', 'hello_world'],",
38 "cmd = 'echo \"Hello, world.\" >$(location message.txt)')");
39 Rule genRule = (Rule) getTarget("//namecollide:hello_world");
lberkiaea56b32017-05-30 12:35:33 +020040 assertThat(genRule.containsErrors()).isFalse(); // TODO: assertTrue
adonovan0cf75b92020-09-29 14:55:42 -070041 assertContainsEvent(
42 "target 'hello_world' is both a rule and a file; please choose another name for the rule",
43 ImmutableSet.of(EventKind.WARNING));
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000044 }
45
46 @Test
47 public void testIsLocalTestRuleForLocalEquals1() throws Exception {
adonovan0cf75b92020-09-29 14:55:42 -070048 scratch.file(
49 "x/BUILD",
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000050 "cc_test(name = 'y',",
51 " srcs = ['a'],",
52 " local = 0)",
53 "cc_test(name = 'z',",
54 " srcs = ['a'],",
55 " local = 1)");
adonovan0cf75b92020-09-29 14:55:42 -070056 Rule y = (Rule) getTarget("//x:y");
lberkiaea56b32017-05-30 12:35:33 +020057 assertThat(TargetUtils.isLocalTestRule(y)).isFalse();
adonovan0cf75b92020-09-29 14:55:42 -070058 Rule z = (Rule) getTarget("//x:z");
lberkiaea56b32017-05-30 12:35:33 +020059 assertThat(TargetUtils.isLocalTestRule(z)).isTrue();
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000060 }
61
62 @Test
63 public void testDeprecation() throws Exception {
adonovan0cf75b92020-09-29 14:55:42 -070064 scratch.file("x/BUILD", "cc_test(name = 'y')", "cc_test(name = 'z', deprecation = 'Foo')");
65 Rule y = (Rule) getTarget("//x:y");
lberkiaea56b32017-05-30 12:35:33 +020066 assertThat(TargetUtils.getDeprecation(y)).isNull();
adonovan0cf75b92020-09-29 14:55:42 -070067 Rule z = (Rule) getTarget("//x:z");
lberkiaea56b32017-05-30 12:35:33 +020068 assertThat(TargetUtils.getDeprecation(z)).isEqualTo("Foo");
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000069 }
70
71 @Test
72 public void testVisibilityValid() throws Exception {
adonovan0cf75b92020-09-29 14:55:42 -070073 scratch.file(
74 "x/BUILD",
75 "cc_binary(name = 'pr', visibility = ['//visibility:private'])",
76 "cc_binary(name = 'pu', visibility = ['//visibility:public'])",
77 "cc_binary(name = 'cu', visibility = ['//a:b'])");
78 Package pkg = getTarget("//x:BUILD").getPackage();
lberkiaea56b32017-05-30 12:35:33 +020079 assertThat(pkg.getRule("pu").getVisibility()).isEqualTo(ConstantRuleVisibility.PUBLIC);
80 assertThat(pkg.getRule("pr").getVisibility()).isEqualTo(ConstantRuleVisibility.PRIVATE);
Dmitry Lomov430d8dc2015-11-18 17:42:25 +000081 }
82}