blob: 7df9c43e00211512f346fe4c83997616dac1e5a0 [file] [log] [blame]
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +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.
14package com.google.devtools.build.lib.skyframe;
15
jcater83130f42019-04-30 14:29:28 -070016import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Florian Weikert92b22362015-12-03 10:17:18 +000017
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000018import com.google.common.collect.ImmutableSet;
Florian Weikertcca703a2015-12-07 09:56:38 +000019import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
Kristina Chodorowec5c07a2016-01-25 17:12:29 +000020import com.google.devtools.build.lib.cmdline.RepositoryName;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000021import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080022import com.google.devtools.build.lib.vfs.Root;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000023import com.google.devtools.build.lib.vfs.RootedPath;
24import com.google.devtools.build.skyframe.SkyKey;
Florian Weikert92b22362015-12-03 10:17:18 +000025import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000029/** Tests for {@link RecursivePkgKey}. */
Florian Weikert92b22362015-12-03 10:17:18 +000030@RunWith(JUnit4.class)
Florian Weikertcca703a2015-12-07 09:56:38 +000031public class RecursivePkgKeyTest extends BuildViewTestCase {
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000032
33 private SkyKey buildRecursivePkgKey(
34 RepositoryName repository,
35 PathFragment rootRelativePath,
36 ImmutableSet<PathFragment> excludedPaths) {
tomluee6a6862018-01-17 14:36:26 -080037 RootedPath rootedPath = RootedPath.toRootedPath(Root.fromPath(rootDirectory), rootRelativePath);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000038 return RecursivePkgValue.key(repository, rootedPath, excludedPaths);
39 }
40
41 private void invalidHelper(
42 PathFragment rootRelativePath, ImmutableSet<PathFragment> excludedPaths) {
jcater83130f42019-04-30 14:29:28 -070043 assertThrows(
44 IllegalArgumentException.class,
45 () -> buildRecursivePkgKey(RepositoryName.MAIN, rootRelativePath, excludedPaths));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000046 }
47
Florian Weikert92b22362015-12-03 10:17:18 +000048 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000049 public void testValidRecursivePkgKeys() throws Exception {
50 buildRecursivePkgKey(
Kristina Chodorowa1a31ff2016-07-27 16:34:27 +000051 RepositoryName.MAIN,
nharmatab4060b62017-04-04 17:11:39 +000052 PathFragment.create(""),
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000053 ImmutableSet.<PathFragment>of());
54 buildRecursivePkgKey(
Kristina Chodorowa1a31ff2016-07-27 16:34:27 +000055 RepositoryName.MAIN,
nharmatab4060b62017-04-04 17:11:39 +000056 PathFragment.create(""),
57 ImmutableSet.of(PathFragment.create("a")));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000058
59 buildRecursivePkgKey(
Kristina Chodorowa1a31ff2016-07-27 16:34:27 +000060 RepositoryName.MAIN,
nharmatab4060b62017-04-04 17:11:39 +000061 PathFragment.create("a"),
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000062 ImmutableSet.<PathFragment>of());
63 buildRecursivePkgKey(
Kristina Chodorowa1a31ff2016-07-27 16:34:27 +000064 RepositoryName.MAIN,
nharmatab4060b62017-04-04 17:11:39 +000065 PathFragment.create("a"),
66 ImmutableSet.of(PathFragment.create("a/b")));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000067
68 buildRecursivePkgKey(
Kristina Chodorowa1a31ff2016-07-27 16:34:27 +000069 RepositoryName.MAIN,
nharmatab4060b62017-04-04 17:11:39 +000070 PathFragment.create("a/b"),
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000071 ImmutableSet.<PathFragment>of());
72 buildRecursivePkgKey(
Kristina Chodorowa1a31ff2016-07-27 16:34:27 +000073 RepositoryName.MAIN,
nharmatab4060b62017-04-04 17:11:39 +000074 PathFragment.create("a/b"),
75 ImmutableSet.of(PathFragment.create("a/b/c")));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000076 }
77
Florian Weikert92b22362015-12-03 10:17:18 +000078 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000079 public void testInvalidRecursivePkgKeys() throws Exception {
nharmatab4060b62017-04-04 17:11:39 +000080 invalidHelper(PathFragment.create(""), ImmutableSet.of(PathFragment.create("")));
81 invalidHelper(PathFragment.create("a"), ImmutableSet.of(PathFragment.create("a")));
82 invalidHelper(PathFragment.create("a"), ImmutableSet.of(PathFragment.create("b")));
83 invalidHelper(PathFragment.create("a/b"), ImmutableSet.of(PathFragment.create("a")));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000084 }
85}