From 28adce590d95cb9aa826fcd9f939efbb8e1eab7e Mon Sep 17 00:00:00 2001 From: janakr Date: Thu, 13 Jul 2017 19:55:32 +0200 Subject: If globbing throws an IOException, fail to construct the package instead of constructing the package with an error. Prior to this change, if a Package.Builder object was constructed, it was guaranteed that a Package (possibly with errors) would be created. This is no longer true: if an IOException is set on the Package.Builder object, it will throw a NoSuchPackageException during #build(). PiperOrigin-RevId: 161832111 --- .../lib/analysis/AnalysisWithIOExceptionsTest.java | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java (limited to 'src/test/java/com/google/devtools/build/lib/analysis') diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java new file mode 100644 index 0000000000..7610f637cb --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java @@ -0,0 +1,66 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.analysis; + +import static org.junit.Assert.fail; + +import com.google.devtools.build.lib.analysis.util.AnalysisTestCase; +import com.google.devtools.build.lib.util.BlazeClock; +import com.google.devtools.build.lib.vfs.FileStatus; +import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.Path; +import com.google.devtools.build.lib.vfs.PathFragment; +import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; +import java.io.IOException; +import java.util.function.Function; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** {@link AnalysisTestCase} with custom filesystem that can throw on stat if desired. */ +@RunWith(JUnit4.class) +public class AnalysisWithIOExceptionsTest extends AnalysisTestCase { + private static final String FS_ROOT = "/fsg"; + + private static final Function NULL_FUNCTION = (path) -> null; + + private Function crashMessage = NULL_FUNCTION; + + @Override + protected FileSystem createFileSystem() { + return new InMemoryFileSystem(BlazeClock.instance(), PathFragment.create(FS_ROOT)) { + @Override + public FileStatus stat(Path path, boolean followSymlinks) throws IOException { + String crash = crashMessage.apply(path); + if (crash != null) { + throw new IOException(crash); + } + return super.stat(path, followSymlinks); + } + }; + } + + @Test + public void testGlobIOException() throws Exception { + scratch.file("b/BUILD", "sh_library(name = 'b', deps= ['//a:a'])"); + scratch.file("a/BUILD", "sh_library(name = 'a', srcs = glob(['a.sh']))"); + crashMessage = path -> path.toString().contains("a.sh") ? "bork" : null; + reporter.removeHandler(failFastHandler); + try { + update("//b:b"); + fail("Expected failure"); + } catch (ViewCreationFailedException expected) { + } + } +} -- cgit v1.2.3