Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
| 21 | import com.google.devtools.build.lib.cmdline.Label; |
| 22 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 23 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 24 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
| 25 | import com.google.devtools.build.lib.syntax.SkylarkImport; |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.testutil.TestConstants; |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 28 | import com.google.devtools.build.lib.vfs.FileSystem; |
tomlu | 82e68b7 | 2017-12-14 12:51:10 -0800 | [diff] [blame^] | 29 | import com.google.devtools.build.lib.vfs.LocalPath; |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 31 | import com.google.devtools.build.skyframe.ErrorInfo; |
| 32 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 33 | import com.google.devtools.build.skyframe.SkyKey; |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 34 | import java.io.IOException; |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.junit.runners.JUnit4; |
| 38 | |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Unit tests of specific functionality of ASTFileLookupFunction. |
| 41 | */ |
| 42 | @RunWith(JUnit4.class) |
| 43 | public class ASTFileLookupFunctionTest extends BuildViewTestCase { |
| 44 | |
| 45 | private static class MockFileSystem extends InMemoryFileSystem { |
| 46 | |
| 47 | boolean statThrowsIoException; |
| 48 | |
| 49 | @Override |
tomlu | 82e68b7 | 2017-12-14 12:51:10 -0800 | [diff] [blame^] | 50 | public FileStatus stat(LocalPath path, boolean followSymlinks) throws IOException { |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 51 | if (statThrowsIoException |
tomlu | 82e68b7 | 2017-12-14 12:51:10 -0800 | [diff] [blame^] | 52 | && path.getPathString() |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 53 | .equals("/workspace/tools/build_rules/prelude_" + TestConstants.PRODUCT_NAME)) { |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 54 | 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); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 80 | assertThat(result.hasError()).isFalse(); |
| 81 | assertThat(result.get(skyKey).getPackage().containsErrors()).isFalse(); |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 82 | } |
| 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 Estren | 81a8999 | 2016-06-20 21:24:42 +0000 | [diff] [blame] | 91 | invalidatePackages(/*alsoConfigs=*/false); // We don't want to fail early on config creation. |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 92 | |
| 93 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 94 | EvaluationResult<PackageValue> result = |
| 95 | SkyframeExecutorTestUtils.evaluate( |
| 96 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 97 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 98 | ErrorInfo errorInfo = result.getError(skyKey); |
| 99 | Throwable e = errorInfo.getException(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 100 | assertThat(errorInfo.getRootCauseOfException()).isEqualTo(skyKey); |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 101 | assertThat(e).isInstanceOf(NoSuchPackageException.class); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 102 | assertThat(e).hasMessageThat().contains("bork"); |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 103 | } |
| 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 Cater | e684392 | 2017-04-20 16:10:11 +0200 | [diff] [blame] | 113 | scratch.file("/a_remote_repo/WORKSPACE"); |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 114 | 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 Estren | 81a8999 | 2016-06-20 21:24:42 +0000 | [diff] [blame] | 119 | invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchains. |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 120 | 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 Cater | e684392 | 2017-04-20 16:10:11 +0200 | [diff] [blame] | 138 | scratch.file("/a_remote_repo/WORKSPACE"); |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 139 | 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 Estren | 81a8999 | 2016-06-20 21:24:42 +0000 | [diff] [blame] | 145 | invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchains. |
Kristina Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 146 | 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); |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 164 | 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 Chodorow | 22b7dc4 | 2016-05-05 19:06:12 +0000 | [diff] [blame] | 169 | } |
| 170 | } |