blob: b7783a59db988da02e38f7c6b8e7f65a38a83cc6 [file] [log] [blame]
lebad3521022022-04-06 09:46:35 -07001// Copyright 2022 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.analysis;
16
17import static com.google.common.truth.Truth.assertThat;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Iterables;
21import com.google.common.collect.Maps;
22import com.google.devtools.build.lib.analysis.util.BuildViewTestBase;
23import com.google.devtools.build.lib.testutil.ManualClock;
24import com.google.devtools.build.lib.vfs.DigestHashFunction;
25import com.google.devtools.build.lib.vfs.FileSystem;
26import com.google.devtools.build.lib.vfs.Path;
27import com.google.devtools.build.lib.vfs.PathFragment;
28import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
29import java.io.IOException;
30import java.util.Map;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.junit.runners.JUnit4;
34
35/** BuildViewTest where it's possible to stub the FileSystem operations. */
36@RunWith(JUnit4.class)
37public class StubbableFSBuildViewTest extends BuildViewTestBase {
38 @Override
39 protected FileSystem createFileSystem() {
40 return new StubbableFs(new ManualClock());
41 }
42
43 private StubbableFs getStubbableFS() {
44 return (StubbableFs) fileSystem;
45 }
46
47 // Regression test for b/227641207.
48 @Test
49 public void testCatastrophicAnalysisErrorAspect_keepGoing_noCrashCatastrophicErrorReported()
50 throws Exception {
51 // We're expecting failures.
52 reporter.removeHandler(failFastHandler);
53 Path pathToBuildB = scratch.file("b/BUILD", "cc_library(name='b')");
54 scratch.file("a/BUILD", "cc_library(name='a', srcs = ['a.cc'], deps = ['//b:b'])");
55 scratch.file("a/a.cc", "");
56 scratch.file(
57 "a/aspect.bzl",
58 "def _impl(target, ctx):",
59 " print('This aspect does nothing')",
60 " return struct()",
61 "MyAspect = aspect(implementation=_impl)");
62 getStubbableFS().stubFastDigestError(pathToBuildB, new IOException("testException"));
63 AnalysisFailureRecorder recorder = new AnalysisFailureRecorder();
64 eventBus.register(recorder);
65
66 AnalysisResult result =
67 update(
68 eventBus,
69 defaultFlags().with(Flag.KEEP_GOING),
70 ImmutableList.of("a/aspect.bzl%MyAspect"),
71 "//a");
72
73 assertThat(result.hasError()).isTrue();
74 assertThat(result.getFailureDetail().getMessage())
75 .contains("command succeeded, but not all targets were analyzed");
76 assertThat(recorder.events).hasSize(1);
77 assertThat(
78 Iterables.getOnlyElement(recorder.events)
79 .getRootCauses()
80 .getSingleton()
81 .getDetailedExitCode()
82 .getFailureDetail()
83 .getMessage())
84 .contains(
85 "Inconsistent filesystem operations. 'stat' said /workspace/b/BUILD is a file but then"
86 + " we later encountered error 'testException' which indicates that"
87 + " /workspace/b/BUILD is no longer a file.");
88 }
89
90 // Regression test for b/227641207.
91 @Test
92 public void testCatastrophicAnalysisError_keepGoing_noCrashCatastrophicErrorReported()
93 throws Exception {
94 // We're expecting failures.
95 reporter.removeHandler(failFastHandler);
96 Path pathToBuildB = scratch.file("b/BUILD", "cc_library(name='b')");
97 scratch.file("a/BUILD", "cc_library(name='a', srcs = ['a.cc'], deps = ['//b:b'])");
98 scratch.file("a/a.cc", "");
99 getStubbableFS().stubFastDigestError(pathToBuildB, new IOException("testExeception"));
100 AnalysisFailureRecorder recorder = new AnalysisFailureRecorder();
101 eventBus.register(recorder);
102
103 AnalysisResult result = update(eventBus, defaultFlags().with(Flag.KEEP_GOING), "//a");
104
105 assertThat(result.hasError()).isTrue();
106 assertThat(result.getFailureDetail().getMessage())
107 .contains("command succeeded, but not all targets were analyzed");
108 assertThat(recorder.events).hasSize(1);
109 assertThat(
110 Iterables.getOnlyElement(recorder.events)
111 .getRootCauses()
112 .getSingleton()
113 .getDetailedExitCode()
114 .getFailureDetail()
115 .getMessage())
116 .contains(
117 "Inconsistent filesystem operations. 'stat' said /workspace/b/BUILD is a file but then"
118 + " we later encountered error 'testExeception' which indicates that"
119 + " /workspace/b/BUILD is no longer a file.");
120 }
121
122 private static class StubbableFs extends InMemoryFileSystem {
123
124 private final Map<PathFragment, IOException> stubbedFastDigestErrors = Maps.newHashMap();
125
126 StubbableFs(ManualClock manualClock) {
127 super(manualClock, DigestHashFunction.SHA256);
128 }
129
130 void stubFastDigestError(Path path, IOException error) {
131 stubbedFastDigestErrors.put(path.asFragment(), error);
132 }
133
134 @Override
135 @SuppressWarnings("UnsynchronizedOverridesSynchronized")
136 protected byte[] getFastDigest(PathFragment path) throws IOException {
137 if (stubbedFastDigestErrors.containsKey(path)) {
138 throw stubbedFastDigestErrors.get(path);
139 }
140 return getDigest(path);
141 }
142 }
143}