Apply static analysis findings in FileFunctionTest.
Apply minor cleanups as suggested by the static analysis.
PiperOrigin-RevId: 291005058
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java
index bcb0e34..ea3efbf 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java
@@ -20,7 +20,6 @@
import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult;
import static org.junit.Assert.fail;
-import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
@@ -44,8 +43,6 @@
import com.google.devtools.build.lib.analysis.ServerDirectories;
import com.google.devtools.build.lib.clock.BlazeClock;
import com.google.devtools.build.lib.cmdline.LabelConstants;
-import com.google.devtools.build.lib.cmdline.PackageIdentifier;
-import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.NullEventHandler;
import com.google.devtools.build.lib.events.StoredEventHandler;
import com.google.devtools.build.lib.packages.WorkspaceFileValue;
@@ -97,6 +94,7 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
@@ -137,7 +135,7 @@
outputBase,
ImmutableList.of(pkgRoot),
BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY);
- FileSystemUtils.createDirectoryAndParents(pkgRoot.asPath());
+ pkgRoot.asPath().createDirectoryAndParents();
}
private SequentialBuildDriver makeDriver() {
@@ -163,7 +161,7 @@
.put(
FileStateValue.FILE_STATE,
new FileStateFunction(
- new AtomicReference<TimestampGranularityMonitor>(),
+ new AtomicReference<>(),
new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS),
externalFilesHelper))
.put(
@@ -179,7 +177,7 @@
.put(
SkyFunctions.PACKAGE_LOOKUP,
new PackageLookupFunction(
- new AtomicReference<>(ImmutableSet.<PackageIdentifier>of()),
+ new AtomicReference<>(ImmutableSet.of()),
CrossRepositoryLabelViolationStrategy.ERROR,
BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY))
.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider))
@@ -198,11 +196,10 @@
differencer);
PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator);
- RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(
- differencer, ImmutableMap.<RepositoryName, PathFragment>of());
+ RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of());
PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT_SEMANTICS);
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set(
- differencer, Optional.<RootedPath>absent());
+ differencer, Optional.absent());
return new SequentialBuildDriver(evaluator);
}
@@ -214,7 +211,7 @@
return valueForPathHelper(Root.absoluteRoot(fs), path, makeDriver());
}
- private FileValue valueForPathHelper(Root root, Path path, SequentialBuildDriver driver)
+ private static FileValue valueForPathHelper(Root root, Path path, SequentialBuildDriver driver)
throws InterruptedException {
PathFragment pathFragment = root.relativize(path);
RootedPath rootedPath = RootedPath.toRootedPath(root, pathFragment);
@@ -505,7 +502,7 @@
createFsAndRoot(
new CustomInMemoryFs(manualClock) {
@Override
- protected byte[] getFastDigest(Path path) throws IOException {
+ protected byte[] getFastDigest(Path path) {
return digest;
}
});
@@ -595,7 +592,7 @@
p = symlink("file", "foo");
FileValue b = valueForPath(p);
p.delete();
- FileSystemUtils.createDirectoryAndParents(pkgRoot.getRelative("file"));
+ pkgRoot.getRelative("file").createDirectoryAndParents();
FileValue c = valueForPath(p);
assertThat(a.equals(b)).isFalse();
assertThat(b.equals(c)).isFalse();
@@ -804,16 +801,10 @@
return ImmutableSet.copyOf(
(Iterable<RootedPath>)
(Iterable)
- Iterables.transform(
- Iterables.filter(
- graph.getValues().keySet(),
- SkyFunctionName.functionIs(FileStateValue.FILE_STATE)),
- new Function<SkyKey, Object>() {
- @Override
- public Object apply(SkyKey skyKey) {
- return skyKey.argument();
- }
- }));
+ graph.getValues().keySet().stream()
+ .filter(SkyFunctionName.functionIs(FileStateValue.FILE_STATE)::apply)
+ .map(SkyKey::argument)
+ .collect(Collectors.toList()));
}
@Test
@@ -970,10 +961,10 @@
ImmutableList.of(rootedPath("c"), rootedPath("d"), rootedPath("e")))
.put(
rootedPath("d"),
- ImmutableList.<RootedPath>of(rootedPath("d"), rootedPath("e"), rootedPath("c")))
+ ImmutableList.of(rootedPath("d"), rootedPath("e"), rootedPath("c")))
.put(
rootedPath("e"),
- ImmutableList.<RootedPath>of(rootedPath("e"), rootedPath("c"), rootedPath("d")))
+ ImmutableList.of(rootedPath("e"), rootedPath("c"), rootedPath("d")))
.put(
rootedPath("a/some/descendant"),
ImmutableList.of(rootedPath("c"), rootedPath("d"), rootedPath("e")))
@@ -982,29 +973,29 @@
ImmutableList.of(rootedPath("c"), rootedPath("d"), rootedPath("e")))
.put(
rootedPath("d/some/descendant"),
- ImmutableList.<RootedPath>of(rootedPath("d"), rootedPath("e"), rootedPath("c")))
+ ImmutableList.of(rootedPath("d"), rootedPath("e"), rootedPath("c")))
.put(
rootedPath("e/some/descendant"),
- ImmutableList.<RootedPath>of(rootedPath("e"), rootedPath("c"), rootedPath("d")))
+ ImmutableList.of(rootedPath("e"), rootedPath("c"), rootedPath("d")))
.build();
Map<RootedPath, ImmutableList<RootedPath>> startToPathToCycleMap =
ImmutableMap.<RootedPath, ImmutableList<RootedPath>>builder()
.put(rootedPath("a"), ImmutableList.of(rootedPath("a"), rootedPath("b")))
.put(rootedPath("b"), ImmutableList.of(rootedPath("b")))
- .put(rootedPath("d"), ImmutableList.<RootedPath>of())
- .put(rootedPath("e"), ImmutableList.<RootedPath>of())
+ .put(rootedPath("d"), ImmutableList.of())
+ .put(rootedPath("e"), ImmutableList.of())
.put(
rootedPath("a/some/descendant"), ImmutableList.of(rootedPath("a"), rootedPath("b")))
.put(rootedPath("b/some/descendant"), ImmutableList.of(rootedPath("b")))
- .put(rootedPath("d/some/descendant"), ImmutableList.<RootedPath>of())
- .put(rootedPath("e/some/descendant"), ImmutableList.<RootedPath>of())
+ .put(rootedPath("d/some/descendant"), ImmutableList.of())
+ .put(rootedPath("e/some/descendant"), ImmutableList.of())
.build();
ImmutableList<SkyKey> keys;
if (ancestorCycle && startInCycle) {
keys = ImmutableList.of(skyKey("d/some/descendant"), skyKey("e/some/descendant"));
- } else if (ancestorCycle && !startInCycle) {
+ } else if (ancestorCycle) {
keys = ImmutableList.of(skyKey("a/some/descendant"), skyKey("b/some/descendant"));
- } else if (!ancestorCycle && startInCycle) {
+ } else if (startInCycle) {
keys = ImmutableList.of(skyKey("d"), skyKey("e"));
} else {
keys = ImmutableList.of(skyKey("a"), skyKey("b"));
@@ -1414,7 +1405,8 @@
assertThat(fileValue).isInstanceOf(SymlinkFileValueWithStoredChain.class);
assertThat(fileValue.getUnresolvedLinkTarget()).isEqualTo(PathFragment.create("b"));
assertThat(fileValue.logicalChainDuringResolution())
- .isEqualTo(ImmutableList.of(rootedPath("a"), rootedPath("b"), rootedPath("c")));
+ .containsExactly(rootedPath("a"), rootedPath("b"), rootedPath("c"))
+ .inOrder();
}
@Test
@@ -1438,7 +1430,7 @@
FileValue fileValue = valueForPath(path("a"));
assertThat(fileValue).isInstanceOf(SymlinkFileValueWithoutStoredChain.class);
assertThat(fileValue.getUnresolvedLinkTarget()).isEqualTo(PathFragment.create("b"));
- assertThrows(IllegalStateException.class, () -> fileValue.logicalChainDuringResolution());
+ assertThrows(IllegalStateException.class, fileValue::logicalChainDuringResolution);
}
@Test
@@ -1449,7 +1441,7 @@
FileValue fileValue = valueForPath(path("a/d"));
assertThat(fileValue).isInstanceOf(DifferentRealPathFileValueWithoutStoredChain.class);
assertThat(fileValue.realRootedPath()).isEqualTo(rootedPath("c/d"));
- assertThrows(IllegalStateException.class, () -> fileValue.logicalChainDuringResolution());
+ assertThrows(IllegalStateException.class, fileValue::logicalChainDuringResolution);
}
@Test
@@ -1496,16 +1488,13 @@
* Returns a callback that, when executed, deletes the given path. Not meant to be called directly
* by tests.
*/
- private Runnable makeDeletePathCallback(final Path toDelete) {
- return new Runnable() {
- @Override
- public void run() {
- try {
- toDelete.delete();
- } catch (IOException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
+ private static Runnable makeDeletePathCallback(Path toDelete) {
+ return () -> {
+ try {
+ toDelete.delete();
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
}
};
}
@@ -1514,16 +1503,13 @@
* Returns a callback that, when executed, writes the given bytes to the given file path. Not
* meant to be called directly by tests.
*/
- private Runnable makeWriteFileContentCallback(final Path toChange, final byte[] contents) {
- return new Runnable() {
- @Override
- public void run() {
- try (OutputStream outputStream = toChange.getOutputStream()) {
- outputStream.write(contents);
- } catch (IOException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
+ private static Runnable makeWriteFileContentCallback(Path toChange, byte[] contents) {
+ return () -> {
+ try (OutputStream outputStream = toChange.getOutputStream()) {
+ outputStream.write(contents);
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
}
};
}
@@ -1532,16 +1518,13 @@
* Returns a callback that, when executed, creates the given directory path. Not meant to be
* called directly by tests.
*/
- private Runnable makeCreateDirectoryCallback(final Path toCreate) {
- return new Runnable() {
- @Override
- public void run() {
- try {
- toCreate.createDirectory();
- } catch (IOException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
+ private static Runnable makeCreateDirectoryCallback(Path toCreate) {
+ return () -> {
+ try {
+ toCreate.createDirectory();
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
}
};
}
@@ -1550,22 +1533,19 @@
* Returns a callback that, when executed, makes {@code toLink} a symlink to {@code toTarget}. Not
* meant to be called directly by tests.
*/
- private Runnable makeSymlinkCallback(final Path toLink, final PathFragment toTarget) {
- return new Runnable() {
- @Override
- public void run() {
- try {
- FileSystemUtils.ensureSymbolicLink(toLink, toTarget);
- } catch (IOException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
+ private static Runnable makeSymlinkCallback(Path toLink, PathFragment toTarget) {
+ return () -> {
+ try {
+ FileSystemUtils.ensureSymbolicLink(toLink, toTarget);
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
}
};
}
/** Returns the files that would be changed/created if {@code path} were to be changed/created. */
- private ImmutableList<String> filesTouchedIfTouched(Path path) {
+ private static ImmutableList<String> filesTouchedIfTouched(Path path) {
List<String> filesToBeTouched = Lists.newArrayList();
do {
filesToBeTouched.add(path.getPathString());
@@ -1700,14 +1680,7 @@
ImmutableList<String> changedPathStrings = changeResult.first;
Runnable undoCallback = changeResult.second;
differencer.invalidate(
- Iterables.transform(
- changedPathStrings,
- new Function<String, SkyKey>() {
- @Override
- public SkyKey apply(String input) {
- return fileStateSkyKey(input);
- }
- }));
+ changedPathStrings.stream().map(this::fileStateSkyKey).collect(Collectors.toList()));
result = driver.evaluate(ImmutableList.of(key), EVALUATION_OPTIONS);
if (result.hasError()) {
@@ -1759,35 +1732,34 @@
.that(result.hasError())
.isTrue();
assertThat(
- !Iterables.isEmpty(result.getError().getCycleInfo())
- || result.getError().getException() != null)
+ !result.getError().getCycleInfo().isEmpty() || result.getError().getException() != null)
.isTrue();
return filesSeen(driver.getGraphForTesting());
}
private Path file(String fileName) throws Exception {
Path path = path(fileName);
- FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
+ path.getParentDirectory().createDirectoryAndParents();
FileSystemUtils.createEmptyFile(path);
return path;
}
private Path file(String fileName, String contents) throws Exception {
Path path = path(fileName);
- FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
+ path.getParentDirectory().createDirectoryAndParents();
FileSystemUtils.writeContentAsLatin1(path, contents);
return path;
}
private Path directory(String directoryName) throws Exception {
Path path = path(directoryName);
- FileSystemUtils.createDirectoryAndParents(path);
+ path.createDirectoryAndParents();
return path;
}
private Path symlink(String link, String target) throws Exception {
Path path = path(link);
- FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
+ path.getParentDirectory().createDirectoryAndParents();
path.createSymbolicLink(PathFragment.create(target));
return path;
}
@@ -1820,11 +1792,11 @@
private final Map<Path, IOException> stubbedStatErrors = Maps.newHashMap();
private final Map<Path, IOException> stubbedFastDigestErrors = Maps.newHashMap();
- public CustomInMemoryFs(ManualClock manualClock) {
+ CustomInMemoryFs(ManualClock manualClock) {
super(manualClock);
}
- public void stubFastDigestError(Path path, IOException error) {
+ void stubFastDigestError(Path path, IOException error) {
stubbedFastDigestErrors.put(path, error);
}
@@ -1836,11 +1808,11 @@
return fastDigest ? getDigest(path) : null;
}
- public void stubStat(Path path, @Nullable FileStatus stubbedResult) {
+ void stubStat(Path path, @Nullable FileStatus stubbedResult) {
stubbedStats.put(path, stubbedResult);
}
- public void stubStatError(Path path, IOException error) {
+ void stubStatError(Path path, IOException error) {
stubbedStatErrors.put(path, error);
}