Googler | 3774f00 | 2023-07-26 14:18:40 -0700 | [diff] [blame] | 1 | // Copyright 2023 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.skyframe; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.devtools.build.lib.analysis.RuleContext; |
| 23 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
| 24 | import com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileFunction; |
| 25 | import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleResolutionFunction; |
| 26 | import com.google.devtools.build.lib.bazel.bzlmod.FakeRegistry; |
| 27 | import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction; |
| 28 | import com.google.devtools.build.lib.bazel.bzlmod.YankedVersionsUtil; |
| 29 | import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.BazelCompatibilityMode; |
| 30 | import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.CheckDirectDepsMode; |
| 31 | import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.LockfileMode; |
| 32 | import com.google.devtools.build.lib.packages.Type; |
| 33 | import com.google.devtools.build.lib.skyframe.PrecomputedValue.Injected; |
| 34 | import com.google.devtools.build.lib.vfs.Path; |
| 35 | import java.io.IOException; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.junit.runners.JUnit4; |
| 39 | |
| 40 | @RunWith(JUnit4.class) |
| 41 | public class RepoFileFunctionTest extends BuildViewTestCase { |
| 42 | |
| 43 | private Path moduleRoot; |
| 44 | private FakeRegistry registry; |
| 45 | |
| 46 | @Override |
| 47 | protected ImmutableList<Injected> extraPrecomputedValues() { |
| 48 | try { |
| 49 | moduleRoot = scratch.dir("modules"); |
| 50 | } catch (IOException e) { |
| 51 | throw new IllegalStateException(e); |
| 52 | } |
| 53 | registry = FakeRegistry.DEFAULT_FACTORY.newFakeRegistry(moduleRoot.getPathString()); |
| 54 | return ImmutableList.of( |
| 55 | PrecomputedValue.injected( |
| 56 | ModuleFileFunction.REGISTRIES, ImmutableList.of(registry.getUrl())), |
| 57 | PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), |
| 58 | PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), |
| 59 | PrecomputedValue.injected(YankedVersionsUtil.ALLOWED_YANKED_VERSIONS, ImmutableList.of()), |
| 60 | PrecomputedValue.injected( |
| 61 | BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES, CheckDirectDepsMode.WARNING), |
| 62 | PrecomputedValue.injected( |
| 63 | BazelModuleResolutionFunction.BAZEL_COMPATIBILITY_MODE, BazelCompatibilityMode.ERROR), |
| 64 | PrecomputedValue.injected(BazelLockFileFunction.LOCKFILE_MODE, LockfileMode.UPDATE)); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void repoFileInTheMainRepo() throws Exception { |
| 69 | scratch.overwriteFile("REPO.bazel", "repo(default_deprecation='EVERYTHING IS DEPRECATED')"); |
| 70 | scratch.overwriteFile("abc/def/BUILD", "filegroup(name='what')"); |
| 71 | assertThat( |
| 72 | getRuleContext(getConfiguredTarget("//abc/def:what")) |
| 73 | .attributes() |
| 74 | .get("deprecation", Type.STRING)) |
| 75 | .isEqualTo("EVERYTHING IS DEPRECATED"); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void repoFileInAnExternalRepo() throws Exception { |
| 80 | setBuildLanguageOptions("--enable_bzlmod"); |
| 81 | scratch.overwriteFile("MODULE.bazel", "bazel_dep(name='foo',version='1.0')"); |
| 82 | scratch.overwriteFile("abc/def/BUILD", "filegroup(name='what')"); |
| 83 | registry.addModule(createModuleKey("foo", "1.0"), "module(name='foo',version='1.0')"); |
| 84 | scratch.overwriteFile(moduleRoot.getRelative("foo~1.0/WORKSPACE.bazel").getPathString()); |
| 85 | scratch.overwriteFile( |
| 86 | moduleRoot.getRelative("foo~1.0/REPO.bazel").getPathString(), |
| 87 | "repo(default_deprecation='EVERYTHING IS DEPRECATED')"); |
| 88 | scratch.overwriteFile( |
| 89 | moduleRoot.getRelative("foo~1.0/abc/def/BUILD").getPathString(), "filegroup(name='what')"); |
| 90 | |
| 91 | assertThat( |
| 92 | getRuleContext(getConfiguredTarget("//abc/def:what")) |
| 93 | .attributes() |
| 94 | .get("deprecation", Type.STRING)) |
| 95 | .isNull(); |
| 96 | assertThat( |
| 97 | getRuleContext(getConfiguredTarget("@@foo~1.0//abc/def:what")) |
| 98 | .attributes() |
| 99 | .get("deprecation", Type.STRING)) |
| 100 | .isEqualTo("EVERYTHING IS DEPRECATED"); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void cantCallRepoTwice() throws Exception { |
| 105 | scratch.overwriteFile( |
| 106 | "REPO.bazel", |
| 107 | "repo(default_deprecation='EVERYTHING IS DEPRECATED')", |
| 108 | "repo(features=['abc'])"); |
| 109 | scratch.overwriteFile("abc/def/BUILD", "filegroup(name='what')"); |
| 110 | reporter.removeHandler(failFastHandler); |
| 111 | assertTargetError("//abc/def:what", "'repo' can only be called once"); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void featureMerger() throws Exception { |
| 116 | scratch.overwriteFile("REPO.bazel", "repo(features=['a', 'b', 'c', '-d'])"); |
| 117 | scratch.overwriteFile( |
| 118 | "abc/def/BUILD", |
| 119 | "package(features=['-a','-b','d'])", |
| 120 | "filegroup(name='what', features=['b'])"); |
| 121 | RuleContext ruleContext = getRuleContext(getConfiguredTarget("//abc/def:what")); |
| 122 | assertThat(ruleContext.getFeatures()).containsExactly("b", "c", "d"); |
| 123 | assertThat(ruleContext.getDisabledFeatures()).containsExactly("a"); |
| 124 | } |
Googler | 3774f00 | 2023-07-26 14:18:40 -0700 | [diff] [blame] | 125 | } |