diff options
author | Ulf Adams <ulfjack@google.com> | 2015-04-24 12:58:31 +0000 |
---|---|---|
committer | Han-Wen Nienhuys <hanwen@google.com> | 2015-04-27 18:46:28 +0000 |
commit | 47bf88e8b4827b9fedf034900fc03b8e50a8bbc3 (patch) | |
tree | 2a302c1e0b74b9eba8f6cb7ce7bcb88de4059969 /src/test | |
parent | 18629fb2667bcc668f8f149df31e665f5b564ef0 (diff) |
Extend the Scratch class to have a working directory.
--
MOS_MIGRATED_REVID=91976263
Diffstat (limited to 'src/test')
3 files changed, 53 insertions, 57 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/FoundationTestCase.java b/src/test/java/com/google/devtools/build/lib/testutil/FoundationTestCase.java index 1b802f7a7b..262f9da433 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/FoundationTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/FoundationTestCase.java @@ -72,7 +72,7 @@ public abstract class FoundationTestCase extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - scratch = new Scratch(createFileSystem()); + scratch = new Scratch(createFileSystem(), "/workspace"); outputBase = scratch.dir("/usr/local/google/_blaze_jrluser/FAKEMD5/"); rootDirectory = scratch.dir("/workspace"); scratchFile(rootDirectory.getRelative("WORKSPACE").getPathString(), @@ -140,20 +140,13 @@ public abstract class FoundationTestCase extends TestCase { return scratch.getFileSystem(); } - private String workspaceRelative(String pathName) { - if (!pathName.startsWith("/")) { - return "/workspace/" + pathName; - } - return pathName; - } - /** * Create a scratch file in the scratch filesystem, with the given pathName, * consisting of a set of lines. The method returns a Path instance for the * scratch file. */ protected Path scratchFile(String pathName, String... lines) throws IOException { - return scratch.file(workspaceRelative(pathName), lines); + return scratch.file(pathName, lines); } /** @@ -161,7 +154,7 @@ public abstract class FoundationTestCase extends TestCase { * returns a Path instance for the scratch directory. */ protected Path scratchDir(String pathName) throws IOException { - return scratch.dir(workspaceRelative(pathName)); + return scratch.dir(pathName); } /** @@ -169,17 +162,7 @@ public abstract class FoundationTestCase extends TestCase { * exists. */ protected Path overwriteScratchFile(String pathName, String... lines) throws IOException { - return scratch.overwriteFile(workspaceRelative(pathName), lines); - } - - /** - * Create a scratch file in the given filesystem, with the given pathName, - * consisting of a set of lines. The method returns a Path instance for the - * scratch file. - */ - protected Path scratchFile(FileSystem fs, String pathName, String... lines) - throws IOException { - return scratch.file(fs, workspaceRelative(pathName), lines); + return scratch.overwriteFile(pathName, lines); } /** @@ -187,9 +170,9 @@ public abstract class FoundationTestCase extends TestCase { * consisting of a set of lines. The method returns a Path instance for the * scratch file. */ - protected Path scratchFile(FileSystem fs, String pathName, byte[] content) + protected Path scratchFile(String pathName, byte[] content) throws IOException { - return scratch.file(fs, workspaceRelative(pathName), content); + return scratch.file(pathName, content); } /** @@ -198,7 +181,7 @@ public abstract class FoundationTestCase extends TestCase { * scratch file. */ protected void deleteFile(String pathName) throws IOException { - scratch.deleteFile(workspaceRelative(pathName)); + scratch.deleteFile(pathName); } // Mix-in assertions: diff --git a/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java b/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java index 229d2a7a75..ac4d8389b6 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java @@ -28,19 +28,35 @@ import java.io.IOException; public final class Scratch { private final FileSystem fileSystem; + private Path workingDir = null; /** * Create a new ScratchFileSystem using the {@link InMemoryFileSystem} */ public Scratch() { - this(new InMemoryFileSystem(BlazeClock.instance())); + this(new InMemoryFileSystem(BlazeClock.instance()), "/"); + } + + /** + * Create a new ScratchFileSystem using the {@link InMemoryFileSystem} + */ + public Scratch(String workingDir) { + this(new InMemoryFileSystem(BlazeClock.instance()), workingDir); } /** * Create a new ScratchFileSystem using the supplied FileSystem. */ public Scratch(FileSystem fileSystem) { + this(fileSystem, "/"); + } + + /** + * Create a new ScratchFileSystem using the supplied FileSystem. + */ + public Scratch(FileSystem fileSystem, String workingDir) { this.fileSystem = fileSystem; + this.workingDir = fileSystem.getPath(workingDir); } /** @@ -50,11 +66,24 @@ public final class Scratch { return fileSystem; } + public void setWorkingDir(String workingDir) { + this.workingDir = fileSystem.getPath(workingDir); + } + + /** + * Resolves {@code pathName} relative to the working directory. Note that this will not create any + * entity in the filesystem; i.e., the file that the object is describing may not exist in the + * filesystem. + */ + public Path resolve(String pathName) { + return workingDir.getRelative(pathName); + } + /** * Create a directory in the scratch filesystem, with the given path name. */ public Path dir(String pathName) throws IOException { - Path dir = getFileSystem().getPath(pathName); + Path dir = resolve(pathName); if (!dir.exists()) { FileSystemUtils.createDirectoryAndParents(dir); } @@ -69,11 +98,11 @@ public final class Scratch { * consisting of a set of lines. The method returns a Path instance for the * scratch file. */ - public Path file(String pathName, String... lines) - throws IOException { - Path newFile = file(getFileSystem(), pathName, lines); - newFile.setLastModifiedTime(-1L); - return newFile; + public Path file(String pathName, String... lines) throws IOException { + Path file = newFile(pathName); + FileSystemUtils.writeContentAsLatin1(file, linesAsString(lines)); + file.setLastModifiedTime(-1L); + return file; } /** @@ -81,10 +110,10 @@ public final class Scratch { * exists. */ public Path overwriteFile(String pathName, String... lines) throws IOException { - Path oldFile = getFileSystem().getPath(pathName); + Path oldFile = resolve(pathName); long newMTime = oldFile.exists() ? oldFile.getLastModifiedTime() + 1 : -1; oldFile.delete(); - Path newFile = file(getFileSystem(), pathName, lines); + Path newFile = file(pathName, lines); newFile.setLastModifiedTime(newMTime); return newFile; } @@ -93,20 +122,7 @@ public final class Scratch { * Deletes the specified scratch file, using the same specification as {@link Path#delete}. */ public boolean deleteFile(String pathName) throws IOException { - Path file = getFileSystem().getPath(pathName); - return file.delete(); - } - - /** - * Create a scratch file in the given filesystem, with the given pathName, - * consisting of a set of lines. The method returns a Path instance for the - * scratch file. - */ - public Path file(FileSystem fs, String pathName, String... lines) - throws IOException { - Path file = newScratchFile(fs, pathName); - FileSystemUtils.writeContentAsLatin1(file, linesAsString(lines)); - return file; + return resolve(pathName).delete(); } /** @@ -114,16 +130,15 @@ public final class Scratch { * consisting of a set of lines. The method returns a Path instance for the * scratch file. */ - public Path file(FileSystem fs, String pathName, byte[] content) - throws IOException { - Path file = newScratchFile(fs, pathName); + public Path file(String pathName, byte[] content) throws IOException { + Path file = newFile(pathName); FileSystemUtils.writeContent(file, content); return file; } /** Creates a new scratch file, ensuring parents exist. */ - private Path newScratchFile(FileSystem fs, String pathName) throws IOException { - Path file = fs.getPath(pathName); + private Path newFile(String pathName) throws IOException { + Path file = resolve(pathName); Path parentDir = file.getParentDirectory(); if (!parentDir.exists()) { FileSystemUtils.createDirectoryAndParents(parentDir); 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 5d93351a8d..08d3158062 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 @@ -126,11 +126,9 @@ public class FsApparatus { } /** - * Initializes this apparatus (if it hasn't been initialized yet), and returns - * a path object describing a file, directory, or symlink pointed at by - * {@code pathName}. Note that this will not create any entity in the - * filesystem; i.e., the file that the object is describing may not exist in - * the filesystem. + * Resolves {@code pathName} relative to the working directory. Note that this will not create any + * entity in the filesystem; i.e., the file that the object is describing may not exist in the + * filesystem. */ public Path path(String pathName) { return workingDir.getRelative(pathName); |