diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2016-09-07 10:22:06 +0000 |
---|---|---|
committer | Yue Gan <yueg@google.com> | 2016-09-07 10:44:35 +0000 |
commit | b65d7547a0aba0f94ffd1cff01bbe33965e2c5a2 (patch) | |
tree | 478e6637c517705534f4a3c5b8e47ffee82e8622 /src/test | |
parent | 27bb97805da8bc43c54ad80a916e0d20b370373e (diff) |
Windows, tests: more refactoring in tests
Move more logic from FilesFileOperationsTest into
WindowsTestUtil, and further separate test files
in the BUILD file.
--
MOS_MIGRATED_REVID=132417714
Diffstat (limited to 'src/test')
3 files changed, 81 insertions, 44 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index a06be8ed17..7adaa54b95 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -2,12 +2,22 @@ package( default_visibility = ["//src/test/java/com/google/devtools/build/lib:__subpackages__"], ) -WINDOWS_TEST_FILES = [ +# Tests for Windows-specific functionality that can run cross-platform. +CROSS_PLATFORM_WINDOWS_TESTS = [ "util/DependencySetWindowsTest.java", "vfs/PathFragmentWindowsTest.java", "vfs/PathWindowsTest.java", ] +# Tests for Windows-specific functionality that run on Windows. +WINDOWS_ON_WINDOWS_TESTS = glob( + ["windows/*.java"], + exclude = ["windows/MockSubprocess.java"], +) + +# All Windows-specific tests. Use this to exclude Windows tests from globs. +ALL_WINDOWS_TESTS = CROSS_PLATFORM_WINDOWS_TESTS + WINDOWS_ON_WINDOWS_TESTS + filegroup( name = "srcs", srcs = glob(["**"]) + [ @@ -118,7 +128,7 @@ java_test( # qualified paths. exclude = [ "util/ResourceFileLoaderTest.java", - ] + WINDOWS_TEST_FILES, + ] + ALL_WINDOWS_TESTS, ), data = glob(["vfs/*.zip"]), tags = [ @@ -155,7 +165,7 @@ java_test( # systems java_test( name = "windows_test", - srcs = WINDOWS_TEST_FILES, + srcs = CROSS_PLATFORM_WINDOWS_TESTS, jvm_flags = ["-Dblaze.os=Windows"], test_class = "com.google.devtools.build.lib.AllTests", deps = [ @@ -193,10 +203,7 @@ java_library( # Tests that need to run on Windows java_test( name = "windows-tests", - srcs = glob( - ["windows/*.java"], - exclude = ["windows/MockSubprocess.java"], - ), + srcs = WINDOWS_ON_WINDOWS_TESTS, data = [ ":MockSubprocess_deploy.jar", ] + select({ diff --git a/src/test/java/com/google/devtools/build/lib/windows/WindowsFileOperationsTest.java b/src/test/java/com/google/devtools/build/lib/windows/WindowsFileOperationsTest.java index 94d9d268b2..44cd07b2e8 100644 --- a/src/test/java/com/google/devtools/build/lib/windows/WindowsFileOperationsTest.java +++ b/src/test/java/com/google/devtools/build/lib/windows/WindowsFileOperationsTest.java @@ -22,10 +22,7 @@ import com.google.devtools.build.lib.testutil.TestSpec; import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.windows.util.WindowsTestUtil; import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -41,39 +38,26 @@ import org.junit.runners.JUnit4; public class WindowsFileOperationsTest { private String scratchRoot; + private WindowsTestUtil testUtil; @Before public void loadJni() throws Exception { WindowsTestUtil.loadJni(); scratchRoot = new File(System.getenv("TEST_TMPDIR")).getAbsolutePath() + "/x"; + testUtil = new WindowsTestUtil(scratchRoot); cleanupScratchDir(); } @After public void cleanupScratchDir() throws Exception { - WindowsTestUtil.deleteAllUnder(scratchRoot); - } - - private Path scratchDir(String path) throws IOException { - return Files.createDirectories(new File(scratchRoot + "/" + path).toPath()); - } - - private void scratchFile(String path, String... contents) throws IOException { - File fd = new File(scratchRoot + "/" + path); - Files.createDirectories(fd.toPath().getParent()); - try (FileWriter w = new FileWriter(fd)) { - for (String line : contents) { - w.write(line); - w.write('\n'); - } - } + testUtil.deleteAllUnder(""); } @Test public void testMockJunctionCreation() throws Exception { - String root = scratchDir("dir").getParent().toString(); - scratchFile("dir/file.txt", "hello"); - WindowsTestUtil.createJunctions(scratchRoot, ImmutableMap.of("junc", "dir")); + String root = testUtil.scratchDir("dir").getParent().toString(); + testUtil.scratchFile("dir/file.txt", "hello"); + testUtil.createJunctions(ImmutableMap.of("junc", "dir")); String[] children = new File(root + "/junc").list(); assertThat(children).isNotNull(); assertThat(children).hasLength(1); @@ -93,17 +77,17 @@ public class WindowsFileOperationsTest { junctions.put("abbrev~1/b", "longtargetpath"); junctions.put("abbrev~1/c", "longta~1"); - String root = scratchDir("shrtpath").getParent().toAbsolutePath().toString(); - scratchDir("longlinkpath"); - scratchDir("abbreviated"); - scratchDir("control/a"); - scratchDir("control/b"); - scratchDir("control/c"); + String root = testUtil.scratchDir("shrtpath").getParent().toAbsolutePath().toString(); + testUtil.scratchDir("longlinkpath"); + testUtil.scratchDir("abbreviated"); + testUtil.scratchDir("control/a"); + testUtil.scratchDir("control/b"); + testUtil.scratchDir("control/c"); - scratchFile("shrttrgt/file1.txt", "hello"); - scratchFile("longtargetpath/file2.txt", "hello"); + testUtil.scratchFile("shrttrgt/file1.txt", "hello"); + testUtil.scratchFile("longtargetpath/file2.txt", "hello"); - WindowsTestUtil.createJunctions(scratchRoot, junctions); + testUtil.createJunctions(junctions); assertThat(WindowsFileOperations.isJunction(root + "/shrtpath/a")).isTrue(); assertThat(WindowsFileOperations.isJunction(root + "/shrtpath/b")).isTrue(); diff --git a/src/test/java/com/google/devtools/build/lib/windows/util/WindowsTestUtil.java b/src/test/java/com/google/devtools/build/lib/windows/util/WindowsTestUtil.java index 23171afe93..0a5d5436b5 100644 --- a/src/test/java/com/google/devtools/build/lib/windows/util/WindowsTestUtil.java +++ b/src/test/java/com/google/devtools/build/lib/windows/util/WindowsTestUtil.java @@ -18,14 +18,18 @@ import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; import com.google.common.base.Joiner; +import com.google.common.base.Strings; import com.google.devtools.build.lib.windows.WindowsJniLoader; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -34,19 +38,36 @@ import java.util.concurrent.TimeUnit; /** Utilities for running Java tests on Windows. */ public final class WindowsTestUtil { - private WindowsTestUtil() {} private static Map<String, String> runfiles; + /** A path where temp files can be created. It is NOT owned by this class. */ + private final String scratchRoot; + + public WindowsTestUtil(String scratchRoot) { + this.scratchRoot = scratchRoot; + } + + /** Ensure the actual JNI DLL is loaded. */ public static void loadJni() throws Exception { String jniDllPath = WindowsTestUtil.getRunfile("io_bazel/src/main/native/windows_jni.dll"); WindowsJniLoader.loadJniForTesting(jniDllPath); } + /** + * Create directory junctions. + * + * <p>Each key in the map is a junction path, relative to {@link #scratchRoot}. These are the link + * names. + * + * <p>Each value in the map is a directory or junction path, also relative to + * {@link #scratchRoot}. These are the link targets. + * + * <p>This method creates all junctions in one invocation to "cmd.exe". + */ // Do not use WindowsFileSystem.createDirectoryJunction but reimplement junction creation here. // If that method were buggy, using it here would compromise the test. - public static void createJunctions(String scratchRoot, Map<String, String> links) - throws Exception { + public void createJunctions(Map<String, String> links) throws Exception { List<String> args = new ArrayList<>(); boolean first = true; @@ -68,17 +89,42 @@ public final class WindowsTestUtil { runCommand(args); } - public static void deleteAllUnder(String path) throws IOException { + /** Delete everything under {@link #scratchRoot}/path. */ + public void deleteAllUnder(String path) throws IOException { + if (Strings.isNullOrEmpty(path)) { + path = scratchRoot; + } else { + path = scratchRoot + "\\" + path; + } if (new File(path).exists()) { runCommand("cmd.exe /c rd /s /q \"" + path + "\""); } } - private static void runCommand(List<String> args) throws IOException { + /** Create a directory under `path`, relative to {@link #scratchRoot}. */ + public Path scratchDir(String path) throws IOException { + return Files.createDirectories(new File(scratchRoot, path).toPath()); + } + + /** Create a file with the given contents under `path`, relative to {@link #scratchRoot}. */ + public void scratchFile(String path, String... contents) throws IOException { + File fd = new File(scratchRoot, path); + Files.createDirectories(fd.toPath().getParent()); + try (FileWriter w = new FileWriter(fd)) { + for (String line : contents) { + w.write(line); + w.write('\n'); + } + } + } + + /** Run a Command Prompt command. */ + public static void runCommand(List<String> args) throws IOException { runCommand(Joiner.on(' ').join(args)); } - private static void runCommand(String cmd) throws IOException { + /** Run a Command Prompt command. */ + public static void runCommand(String cmd) throws IOException { Process p = Runtime.getRuntime().exec(cmd); try { // Wait no more than 5 seconds to create all junctions. |