blob: 289b420eac8c1d7903b72561794b7c4c69fc19ed [file] [log] [blame]
Kristina Chodorow22b7dc42016-05-05 19:06:12 +00001// Copyright 2016 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.skyframe;
16
17import static com.google.common.truth.Truth.assertThat;
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000018
19import com.google.common.collect.ImmutableList;
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000020import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
21import com.google.devtools.build.lib.cmdline.Label;
22import com.google.devtools.build.lib.cmdline.PackageIdentifier;
23import com.google.devtools.build.lib.packages.NoSuchPackageException;
24import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
25import com.google.devtools.build.lib.syntax.SkylarkImport;
Luis Fernando Pino Duquebe102182016-05-23 14:03:55 +000026import com.google.devtools.build.lib.testutil.TestConstants;
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000027import com.google.devtools.build.lib.vfs.FileStatus;
28import com.google.devtools.build.lib.vfs.FileSystem;
tomlu82e68b72017-12-14 12:51:10 -080029import com.google.devtools.build.lib.vfs.LocalPath;
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000030import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
31import com.google.devtools.build.skyframe.ErrorInfo;
32import com.google.devtools.build.skyframe.EvaluationResult;
33import com.google.devtools.build.skyframe.SkyKey;
ulfjack8989e192017-04-20 13:45:25 +020034import java.io.IOException;
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000035import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.junit.runners.JUnit4;
38
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000039/**
40 * Unit tests of specific functionality of ASTFileLookupFunction.
41 */
42@RunWith(JUnit4.class)
43public class ASTFileLookupFunctionTest extends BuildViewTestCase {
44
45 private static class MockFileSystem extends InMemoryFileSystem {
46
47 boolean statThrowsIoException;
48
49 @Override
tomlu82e68b72017-12-14 12:51:10 -080050 public FileStatus stat(LocalPath path, boolean followSymlinks) throws IOException {
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000051 if (statThrowsIoException
tomlu82e68b72017-12-14 12:51:10 -080052 && path.getPathString()
Luis Fernando Pino Duquebe102182016-05-23 14:03:55 +000053 .equals("/workspace/tools/build_rules/prelude_" + TestConstants.PRODUCT_NAME)) {
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000054 throw new IOException("bork");
55 }
56 return super.stat(path, followSymlinks);
57 }
58 }
59
60 private MockFileSystem mockFS;
61
62 @Override
63 protected FileSystem createFileSystem() {
64 mockFS = new MockFileSystem();
65 return mockFS;
66 }
67
68 @Test
69 public void testPreludeASTFileIsNotMandatory() throws Exception {
70 reporter.removeHandler(failFastHandler);
71 scratch.file(
72 "foo/BUILD", "genrule(name = 'foo',", " outs = ['out.txt'],", " cmd = 'echo hello >@')");
73 scratch.deleteFile("tools/build_rules/prelude_blaze");
74 invalidatePackages();
75
76 SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo"));
77 EvaluationResult<PackageValue> result =
78 SkyframeExecutorTestUtils.evaluate(
79 getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter);
lberkiaea56b32017-05-30 12:35:33 +020080 assertThat(result.hasError()).isFalse();
81 assertThat(result.get(skyKey).getPackage().containsErrors()).isFalse();
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000082 }
83
84 @Test
85 public void testIOExceptionOccursDuringReading() throws Exception {
86 reporter.removeHandler(failFastHandler);
87 scratch.file("/workspace/tools/build_rules/BUILD");
88 scratch.file(
89 "foo/BUILD", "genrule(name = 'foo',", " outs = ['out.txt'],", " cmd = 'echo hello >@')");
90 mockFS.statThrowsIoException = true;
Greg Estren81a89992016-06-20 21:24:42 +000091 invalidatePackages(/*alsoConfigs=*/false); // We don't want to fail early on config creation.
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000092
93 SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo"));
94 EvaluationResult<PackageValue> result =
95 SkyframeExecutorTestUtils.evaluate(
96 getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter);
lberkiaea56b32017-05-30 12:35:33 +020097 assertThat(result.hasError()).isTrue();
Kristina Chodorow22b7dc42016-05-05 19:06:12 +000098 ErrorInfo errorInfo = result.getError(skyKey);
99 Throwable e = errorInfo.getException();
lberkiaea56b32017-05-30 12:35:33 +0200100 assertThat(errorInfo.getRootCauseOfException()).isEqualTo(skyKey);
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000101 assertThat(e).isInstanceOf(NoSuchPackageException.class);
lberkiaea56b32017-05-30 12:35:33 +0200102 assertThat(e).hasMessageThat().contains("bork");
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000103 }
104
105 @Test
106 public void testLoadFromBuildFileInRemoteRepo() throws Exception {
107 scratch.deleteFile("tools/build_rules/prelude_blaze");
108 scratch.overwriteFile("WORKSPACE",
109 "local_repository(",
110 " name = 'a_remote_repo',",
111 " path = '/a_remote_repo'",
112 ")");
John Catere6843922017-04-20 16:10:11 +0200113 scratch.file("/a_remote_repo/WORKSPACE");
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000114 scratch.file("/a_remote_repo/remote_pkg/BUILD",
115 "load(':ext.bzl', 'CONST')");
116 scratch.file("/a_remote_repo/remote_pkg/ext.bzl",
117 "CONST = 17");
118
Greg Estren81a89992016-06-20 21:24:42 +0000119 invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchains.
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000120 SkyKey skyKey =
121 ASTFileLookupValue.key(Label.parseAbsoluteUnchecked("@a_remote_repo//remote_pkg:BUILD"));
122 EvaluationResult<ASTFileLookupValue> result =
123 SkyframeExecutorTestUtils.evaluate(
124 getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter);
125 ImmutableList<SkylarkImport> imports = result.get(skyKey).getAST().getImports();
126 assertThat(imports).hasSize(1);
127 assertThat(imports.get(0).getImportString()).isEqualTo(":ext.bzl");
128 }
129
130 @Test
131 public void testLoadFromSkylarkFileInRemoteRepo() throws Exception {
132 scratch.deleteFile("tools/build_rules/prelude_blaze");
133 scratch.overwriteFile("WORKSPACE",
134 "local_repository(",
135 " name = 'a_remote_repo',",
136 " path = '/a_remote_repo'",
137 ")");
John Catere6843922017-04-20 16:10:11 +0200138 scratch.file("/a_remote_repo/WORKSPACE");
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000139 scratch.file("/a_remote_repo/remote_pkg/BUILD");
140 scratch.file("/a_remote_repo/remote_pkg/ext1.bzl",
141 "load(':ext2.bzl', 'CONST')");
142 scratch.file("/a_remote_repo/remote_pkg/ext2.bzl",
143 "CONST = 17");
144
Greg Estren81a89992016-06-20 21:24:42 +0000145 invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchains.
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000146 SkyKey skyKey =
147 ASTFileLookupValue.key(Label.parseAbsoluteUnchecked("@a_remote_repo//remote_pkg:ext1.bzl"));
148 EvaluationResult<ASTFileLookupValue> result =
149 SkyframeExecutorTestUtils.evaluate(
150 getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter);
151 ImmutableList<SkylarkImport> imports = result.get(skyKey).getAST().getImports();
152 assertThat(imports).hasSize(1);
153 assertThat(imports.get(0).getImportString()).isEqualTo(":ext2.bzl");
154 }
155
156 @Test
157 public void testLoadWithNonExistentBuildFile() throws Exception {
158 invalidatePackages();
159 SkyKey skyKey =
160 ASTFileLookupValue.key(Label.parseAbsoluteUnchecked("@a_remote_repo//remote_pkg:BUILD"));
161 EvaluationResult<ASTFileLookupValue> result =
162 SkyframeExecutorTestUtils.evaluate(
163 getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter);
ulfjack8989e192017-04-20 13:45:25 +0200164 assertThat(result.get(skyKey).lookupSuccessful()).isFalse();
165 assertThat(result.get(skyKey).getErrorMsg())
166 .contains("Unable to load package for '@a_remote_repo//remote_pkg:BUILD'");
167 assertThat(result.get(skyKey).getErrorMsg())
168 .contains("The repository could not be resolved");
Kristina Chodorow22b7dc42016-05-05 19:06:12 +0000169 }
170}