diff options
10 files changed, 97 insertions, 88 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategyTestCase.java b/src/test/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategyTestCase.java index 9e26afaa9c..771162fe0e 100644 --- a/src/test/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategyTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategyTestCase.java @@ -119,7 +119,7 @@ public class LinuxSandboxedStrategyTestCase { } private Path createTestRoot() throws IOException { - fileSystem = FileSystems.initDefaultAsNative(); + fileSystem = FileSystems.getNativeFileSystem(); Path testRoot = fileSystem.getPath(TestUtils.tmpDir()); try { FileSystemUtils.deleteTreesBelow(testRoot); 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 69c68f5a8d..be334649f4 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 @@ -53,8 +53,8 @@ import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.lib.vfs.RootedPath; -import com.google.devtools.build.lib.vfs.UnixFileSystem; import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; +import com.google.devtools.build.lib.vfs.util.FileSystems; import com.google.devtools.build.skyframe.ErrorInfo; import com.google.devtools.build.skyframe.EvaluationResult; import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; @@ -88,9 +88,7 @@ import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nullable; -/** - * Tests for {@link FileFunction}. - */ +/** Tests for {@link FileFunction}. */ @RunWith(JUnit4.class) public class FileFunctionTest { private CustomInMemoryFs fs; @@ -102,7 +100,7 @@ public class FileFunctionTest { private RecordingDifferencer differencer; @Before - public final void createFsAndRoot() throws Exception { + public final void createFsAndRoot() throws Exception { fastMd5 = true; manualClock = new ManualClock(); createFsAndRoot(new CustomInMemoryFs(manualClock)); @@ -122,16 +120,18 @@ public class FileFunctionTest { private SequentialBuildDriver makeDriver(boolean errorOnExternalFiles) { AtomicReference<PathPackageLocator> pkgLocatorRef = new AtomicReference<>(pkgLocator); - BlazeDirectories directories = new BlazeDirectories(pkgRoot, outputBase, pkgRoot, - TestConstants.PRODUCT_NAME); + BlazeDirectories directories = + new BlazeDirectories(pkgRoot, outputBase, pkgRoot, TestConstants.PRODUCT_NAME); ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(pkgLocatorRef, errorOnExternalFiles, directories); differencer = new RecordingDifferencer(); MemoizingEvaluator evaluator = new InMemoryMemoizingEvaluator( ImmutableMap.<SkyFunctionName, SkyFunction>builder() - .put(SkyFunctions.FILE_STATE, new FileStateFunction( - new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper)) + .put( + SkyFunctions.FILE_STATE, + new FileStateFunction( + new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper)) .put( SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction()) @@ -470,8 +470,8 @@ public class FileFunctionTest { @Test public void testUnreadableFileWithFastDigest() throws Exception { - final byte[] expectedDigest = MessageDigest.getInstance("md5").digest( - "blah".getBytes(StandardCharsets.UTF_8)); + final byte[] expectedDigest = + MessageDigest.getInstance("md5").digest("blah".getBytes(StandardCharsets.UTF_8)); createFsAndRoot( new CustomInMemoryFs(manualClock) { @@ -1121,7 +1121,8 @@ public class FileFunctionTest { FileSystem oldFileSystem = Path.getFileSystemForSerialization(); try { - FileSystem fs = new UnixFileSystem(); // InMemoryFS is not supported for serialization. + // InMemoryFS is not supported for serialization. + FileSystem fs = FileSystems.getJavaIoFileSystem(); Path.setFileSystemForSerialization(fs); pkgRoot = fs.getRootDirectory(); @@ -1311,8 +1312,8 @@ public class FileFunctionTest { } /** - * Returns a callback that, when executed, deletes the given path. - * Not meant to be called directly by tests. + * 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() { @@ -1329,8 +1330,8 @@ public class FileFunctionTest { } /** - * Returns a callback that, when executed, writes the given bytes to the given file path. - * Not meant to be called directly by tests. + * 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() { @@ -1350,8 +1351,8 @@ public class FileFunctionTest { } /** - * Returns a callback that, when executed, creates the given directory path. - * Not meant to be called directly by tests. + * 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() { @@ -1368,8 +1369,8 @@ public class FileFunctionTest { } /** - * Returns a callback that, when executed, makes {@code toLink} a symlink to {@code toTarget}. - * Not meant to be called directly by tests. + * 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() { @@ -1385,9 +1386,7 @@ public class FileFunctionTest { }; } - /** - * Returns the files that would be changed/created if {@code path} were to be changed/created. - */ + /** Returns the files that would be changed/created if {@code path} were to be changed/created. */ private ImmutableList<String> filesTouchedIfTouched(Path path) { List<String> filesToBeTouched = Lists.newArrayList(); do { @@ -1399,12 +1398,13 @@ public class FileFunctionTest { /** * Changes the contents of the FileValue for the given file in some way e.g. + * * <ul> - * <li> If it's a regular file, the contents will be changed. - * <li> If it's a non-existent file, it will be created. - * <ul> - * and then returns the file(s) changed paired with a callback to undo the change. Not meant to - * be called directly by tests. + * <li> If it's a regular file, the contents will be changed. + * <li> If it's a non-existent file, it will be created. + * <ul> + * and then returns the file(s) changed paired with a callback to undo the change. Not meant + * to be called directly by tests. */ private Pair<ImmutableList<String>, Runnable> changeFile(String fileStringToChange) throws Exception { @@ -1426,12 +1426,13 @@ public class FileFunctionTest { /** * Changes the contents of the FileValue for the given directory in some way e.g. + * * <ul> - * <li> If it exists, the directory will be deleted. - * <li> If it doesn't exist, the directory will be created. - * <ul> - * and then returns the file(s) changed paired with a callback to undo the change. Not meant to - * be called directly by tests. + * <li> If it exists, the directory will be deleted. + * <li> If it doesn't exist, the directory will be created. + * <ul> + * and then returns the file(s) changed paired with a callback to undo the change. Not meant + * to be called directly by tests. */ private Pair<ImmutableList<String>, Runnable> changeDirectory(String directoryStringToChange) throws Exception { @@ -1449,14 +1450,13 @@ public class FileFunctionTest { } /** - * Performs filesystem operations to change the file or directory denoted by - * {@code changedPathString} and returns the file(s) changed paired with a callback to undo the - * change. + * Performs filesystem operations to change the file or directory denoted by {@code + * changedPathString} and returns the file(s) changed paired with a callback to undo the change. * Not meant to be called directly by tests. * - * @param isSupposedToBeFile whether the path denoted by the given string is supposed to be a - * file or a directory. This is needed is the path doesn't exist yet, - * and so the filesystem doesn't know. + * @param isSupposedToBeFile whether the path denoted by the given string is supposed to be a file + * or a directory. This is needed is the path doesn't exist yet, and so the filesystem doesn't + * know. */ private Pair<ImmutableList<String>, Runnable> change( String changedPathString, boolean isSupposedToBeFile) throws Exception { @@ -1503,8 +1503,8 @@ public class FileFunctionTest { /** * Asserts that if the contents of {@code changedPathString} changes, then the FileValue - * corresponding to {@code pathString} will change. Returns the paths of all files seen. - * Not meant to be called directly by tests. + * corresponding to {@code pathString} will change. Returns the paths of all files seen. Not meant + * to be called directly by tests. */ private Set<RootedPath> assertChangesIfChanges( String changedPathString, boolean isFile, boolean changes, String pathString) @@ -1544,10 +1544,7 @@ public class FileFunctionTest { assertTrue( String.format( "Changing the contents of %s %s should%s change the value for file %s.", - isFile ? "file" : "directory", - changedPathString, - changes ? "" : " not", - pathString), + isFile ? "file" : "directory", changedPathString, changes ? "" : " not", pathString), changes != newValue.equals(oldValue)); // Restore the original file. diff --git a/src/test/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategyTest.java b/src/test/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategyTest.java index 733c4adf5f..580bff9ba5 100644 --- a/src/test/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategyTest.java +++ b/src/test/java/com/google/devtools/build/lib/standalone/StandaloneSpawnStrategyTest.java @@ -68,7 +68,7 @@ public class StandaloneSpawnStrategyTest { private FileSystem fileSystem; private Path createTestRoot() throws IOException { - fileSystem = FileSystems.initDefaultAsNative(); + fileSystem = FileSystems.getNativeFileSystem(); Path testRoot = fileSystem.getPath(TestUtils.tmpDir()); try { FileSystemUtils.deleteTreesBelow(testRoot); diff --git a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemConcurrencyTest.java b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemConcurrencyTest.java index 0ae1a25dbd..2d6308ffbf 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemConcurrencyTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemConcurrencyTest.java @@ -36,7 +36,7 @@ public class FileSystemConcurrencyTest { @Before public final void initializeFileSystem() throws Exception { - FileSystem testFS = FileSystems.initDefaultAsNative(); + FileSystem testFS = FileSystems.getNativeFileSystem(); // Resolve symbolic links in the temp dir: workingDir = testFS.getPath(new File(TestUtils.tmpDir()).getCanonicalPath()); diff --git a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemsTest.java b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemsTest.java index 5e10a3eda0..1e250a01ab 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/FileSystemsTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/FileSystemsTest.java @@ -30,19 +30,19 @@ public class FileSystemsTest { @Test public void testFileSystemsCreatesOnlyOneDefaultNative() { - assertSame(FileSystems.initDefaultAsNative(), - FileSystems.initDefaultAsNative()); + assertSame(FileSystems.getNativeFileSystem(), + FileSystems.getNativeFileSystem()); } @Test public void testFileSystemsCreatesOnlyOneDefaultJavaIo() { - assertSame(FileSystems.initDefaultAsJavaIo(), - FileSystems.initDefaultAsJavaIo()); + assertSame(FileSystems.getJavaIoFileSystem(), + FileSystems.getJavaIoFileSystem()); } @Test public void testFileSystemsCanSwitchDefaults() { - assertNotSame(FileSystems.initDefaultAsNative(), - FileSystems.initDefaultAsJavaIo()); + assertNotSame(FileSystems.getNativeFileSystem(), + FileSystems.getJavaIoFileSystem()); } } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/UnixPathGetParentTest.java b/src/test/java/com/google/devtools/build/lib/vfs/UnixPathGetParentTest.java index db0cad3242..d4719f6da6 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/UnixPathGetParentTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/UnixPathGetParentTest.java @@ -38,7 +38,7 @@ public class UnixPathGetParentTest { @Before public final void createTestRoot() throws Exception { - unixFs = FileSystems.initDefaultAsNative(); + unixFs = FileSystems.getNativeFileSystem(); testRoot = unixFs.getPath(TestUtils.tmpDir()).getRelative("UnixPathGetParentTest"); FileSystemUtils.createDirectoryAndParents(testRoot); } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/UnixPathTest.java b/src/test/java/com/google/devtools/build/lib/vfs/UnixPathTest.java index cd174c62f8..2be505548a 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/UnixPathTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/UnixPathTest.java @@ -53,7 +53,7 @@ public class UnixPathTest { private File tmpDir; protected FileSystem getUnixFileSystem() { - return FileSystems.initDefaultAsNative(); + return FileSystems.getNativeFileSystem(); } @Before diff --git a/src/test/java/com/google/devtools/build/lib/vfs/ZipFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/ZipFileSystemTest.java index 8ff07bc537..bbe4129967 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/ZipFileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/ZipFileSystemTest.java @@ -60,7 +60,7 @@ public class ZipFileSystemTest { @Before public final void initializeFileSystems() throws Exception { - FileSystem unixFs = FileSystems.initDefaultAsNative(); + FileSystem unixFs = FileSystems.getNativeFileSystem(); Path testdataDir = unixFs.getPath(BlazeTestUtils.runfilesDir()).getRelative( TestConstants.JAVATESTS_ROOT + "/com/google/devtools/build/lib/vfs"); Path zPath1 = testdataDir.getChild("sample_with_dirs.zip"); diff --git a/src/test/java/com/google/devtools/build/lib/vfs/util/FileSystems.java b/src/test/java/com/google/devtools/build/lib/vfs/util/FileSystems.java index 38ad5f41fc..9329a8b853 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/util/FileSystems.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/util/FileSystems.java @@ -13,13 +13,16 @@ // limitations under the License. package com.google.devtools.build.lib.vfs.util; +import com.google.common.base.Verify; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; +import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.build.lib.vfs.JavaIoFileSystem; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.lib.vfs.UnionFileSystem; import com.google.devtools.build.lib.vfs.UnixFileSystem; +import com.google.devtools.build.lib.vfs.WindowsFileSystem; import com.google.devtools.build.lib.vfs.ZipFileSystem; import java.io.IOException; @@ -34,60 +37,69 @@ public final class FileSystems { private FileSystems() {} - private static FileSystem defaultFileSystem; + private static FileSystem defaultNativeFileSystem; + private static FileSystem defaultJavaIoFileSystem; + private static FileSystem defaultUnionFileSystem; /** - * Initializes the default {@link FileSystem} instance as a platform native - * (Unix) file system, creating one iff needed, and returns the instance. - * - * <p>This method is idempotent as long as the initialization is of the same - * type (Native/JavaIo/Union). + * Initializes the default native {@link FileSystem} instance as a platform native + * (Unix or Windows) file system. If it's not initialized, then initialize it, + * otherwise verify if the type of the instance is correct. */ - public static synchronized FileSystem initDefaultAsNative() { - if (!(defaultFileSystem instanceof UnixFileSystem)) { - defaultFileSystem = new UnixFileSystem(); + public static synchronized FileSystem getNativeFileSystem() { + if (OS.getCurrent() == OS.WINDOWS) { + if (defaultNativeFileSystem == null) { + defaultNativeFileSystem = new WindowsFileSystem(); + } else { + Verify.verify(defaultNativeFileSystem instanceof WindowsFileSystem); + } + } else { + if (defaultNativeFileSystem == null) { + defaultNativeFileSystem = new UnixFileSystem(); + } else { + Verify.verify(defaultNativeFileSystem instanceof UnixFileSystem); + } } - return defaultFileSystem; + return defaultNativeFileSystem; } /** - * Initializes the default {@link FileSystem} instance as a java.io.File - * file system, creating one iff needed, and returns the instance. - * - * <p>This method is idempotent as long as the initialization is of the same - * type (Native/JavaIo/Union). + * Initializes the default java {@link FileSystem} instance as a java.io.File + * file system. If it's not initialized, then initialize it, + * otherwise verify if the type of the instance is correct. */ - public static synchronized FileSystem initDefaultAsJavaIo() { - if (!(defaultFileSystem instanceof JavaIoFileSystem)) { - defaultFileSystem = new JavaIoFileSystem(); + public static synchronized FileSystem getJavaIoFileSystem() { + if (defaultJavaIoFileSystem == null) { + defaultJavaIoFileSystem = new JavaIoFileSystem(); + } else { + Verify.verify(defaultJavaIoFileSystem instanceof JavaIoFileSystem); } - return defaultFileSystem; + return defaultJavaIoFileSystem; } /** - * Initializes the default {@link FileSystem} instance as a - * {@link UnionFileSystem}, creating one iff needed, - * and returns the instance. - * - * <p>This method is idempotent as long as the initialization is of the same - * type (Native/JavaIo/Union). + * Initializes the default union {@link FileSystem} instance as a + * {@link UnionFileSystem}. If it's not initialized, then initialize it, + * otherwise verify if the type of the instance is correct. * * @param prefixMapping the desired mapping of path prefixes to delegate file systems * @param rootFileSystem the default file system for paths that don't match any prefix map */ - public static synchronized FileSystem initDefaultAsUnion( + public static synchronized FileSystem getUnionFileSystem( Map<PathFragment, FileSystem> prefixMapping, FileSystem rootFileSystem) { - if (!(defaultFileSystem instanceof UnionFileSystem)) { - defaultFileSystem = new UnionFileSystem(prefixMapping, rootFileSystem); + if (defaultUnionFileSystem == null) { + defaultUnionFileSystem = new UnionFileSystem(prefixMapping, rootFileSystem); + } else { + Verify.verify(defaultUnionFileSystem instanceof UnionFileSystem); } - return defaultFileSystem; + return defaultUnionFileSystem; } /** * Returns a new instance of a simple {@link FileSystem} implementation that * presents the contents of a zip file as a read-only file system view. */ - public static FileSystem newZipFileSystem(Path zipFile) throws IOException { + public static FileSystem getZipFileSystem(Path zipFile) throws IOException { return new ZipFileSystem(zipFile); } } diff --git a/src/test/java/com/google/devtools/build/lib/vfs/util/FsApparatus.java b/src/test/java/com/google/devtools/build/lib/vfs/util/FsApparatus.java index 101061f59c..fc88272517 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/util/FsApparatus.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/util/FsApparatus.java @@ -56,7 +56,7 @@ public class FsApparatus { * does not hold with our usage of Unix filesystems. */ public static FsApparatus newNative() { - FileSystem fs = FileSystems.initDefaultAsNative(); + FileSystem fs = FileSystems.getNativeFileSystem(); Path wd = fs.getPath(TMP_DIR); try { |