Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.packages; |
| 16 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 18 | |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.devtools.build.lib.events.EventKind; |
| 21 | import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase; |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.junit.runner.RunWith; |
| 24 | import org.junit.runners.JUnit4; |
| 25 | |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 26 | /** Tests for {@link Rule}. */ |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 27 | @RunWith(JUnit4.class) |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 28 | public class RuleTest extends PackageLoadingTestCase { |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 29 | |
| 30 | @Test |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 31 | public void testOutputNameError() throws Exception { |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 32 | reporter.removeHandler(failFastHandler); |
| 33 | scratch.file( |
| 34 | "namecollide/BUILD", |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 35 | "genrule(name = 'hello_world',", |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 36 | "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"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 40 | assertThat(genRule.containsErrors()).isFalse(); // TODO: assertTrue |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 41 | 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 Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testIsLocalTestRuleForLocalEquals1() throws Exception { |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 48 | scratch.file( |
| 49 | "x/BUILD", |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 50 | "cc_test(name = 'y',", |
| 51 | " srcs = ['a'],", |
| 52 | " local = 0)", |
| 53 | "cc_test(name = 'z',", |
| 54 | " srcs = ['a'],", |
| 55 | " local = 1)"); |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 56 | Rule y = (Rule) getTarget("//x:y"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 57 | assertThat(TargetUtils.isLocalTestRule(y)).isFalse(); |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 58 | Rule z = (Rule) getTarget("//x:z"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 59 | assertThat(TargetUtils.isLocalTestRule(z)).isTrue(); |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testDeprecation() throws Exception { |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 64 | scratch.file("x/BUILD", "cc_test(name = 'y')", "cc_test(name = 'z', deprecation = 'Foo')"); |
| 65 | Rule y = (Rule) getTarget("//x:y"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 66 | assertThat(TargetUtils.getDeprecation(y)).isNull(); |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 67 | Rule z = (Rule) getTarget("//x:z"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 68 | assertThat(TargetUtils.getDeprecation(z)).isEqualTo("Foo"); |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testVisibilityValid() throws Exception { |
adonovan | 0cf75b9 | 2020-09-29 14:55:42 -0700 | [diff] [blame] | 73 | 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(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 79 | assertThat(pkg.getRule("pu").getVisibility()).isEqualTo(ConstantRuleVisibility.PUBLIC); |
| 80 | assertThat(pkg.getRule("pr").getVisibility()).isEqualTo(ConstantRuleVisibility.PRIVATE); |
Dmitry Lomov | 430d8dc | 2015-11-18 17:42:25 +0000 | [diff] [blame] | 81 | } |
| 82 | } |