diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2016-09-07 08:06:15 +0000 |
---|---|---|
committer | Yue Gan <yueg@google.com> | 2016-09-07 10:42:50 +0000 |
commit | a2da311441fc682629c38435038d7d3be855507f (patch) | |
tree | 281a5eadc6d0b4a26d363816b79f7d8e86ccc99a /src/test/java | |
parent | fde49114ca129ce3ac77753fcde90075b6571778 (diff) |
Windows, tests: move code into WindowsTestUtil
Additionally:
- clean up the corresponding BUILD file a bit
- add a comment to Path
A subsequent change will add tests for
WindowsFileSystem, then fix a bug there.
--
MOS_MIGRATED_REVID=132408212
Diffstat (limited to 'src/test/java')
3 files changed, 92 insertions, 74 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index bc8ed7a550..a06be8ed17 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -2,6 +2,12 @@ package( default_visibility = ["//src/test/java/com/google/devtools/build/lib:__subpackages__"], ) +WINDOWS_TEST_FILES = [ + "util/DependencySetWindowsTest.java", + "vfs/PathFragmentWindowsTest.java", + "vfs/PathWindowsTest.java", +] + filegroup( name = "srcs", srcs = glob(["**"]) + [ @@ -111,11 +117,8 @@ java_test( # java_rules_skylark doesn't support resource loading with # qualified paths. exclude = [ - "util/DependencySetWindowsTest.java", "util/ResourceFileLoaderTest.java", - "vfs/PathFragmentWindowsTest.java", - "vfs/PathWindowsTest.java", - ], + ] + WINDOWS_TEST_FILES, ), data = glob(["vfs/*.zip"]), tags = [ @@ -152,11 +155,7 @@ java_test( # systems java_test( name = "windows_test", - srcs = [ - "util/DependencySetWindowsTest.java", - "vfs/PathFragmentWindowsTest.java", - "vfs/PathWindowsTest.java", - ], + srcs = WINDOWS_TEST_FILES, jvm_flags = ["-Dblaze.os=Windows"], test_class = "com.google.devtools.build.lib.AllTests", deps = [ @@ -182,6 +181,13 @@ java_library( srcs = [ "windows/util/WindowsTestUtil.java", ], + deps = [ + "//src/main/java/com/google/devtools/build/lib:windows", + "//third_party:guava", + "//third_party:guava-testlib", + "//third_party:junit4", + "//third_party:truth", + ], ) # Tests that need to run on Windows 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 7de28febc5..94d9d268b2 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 @@ -15,10 +15,8 @@ package com.google.devtools.build.lib.windows; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.testutil.TestSpec; import com.google.devtools.build.lib.util.OS; @@ -28,12 +26,9 @@ import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -49,60 +44,14 @@ public class WindowsFileOperationsTest { @Before public void loadJni() throws Exception { - String jniDllPath = WindowsTestUtil.getRunfile("io_bazel/src/main/native/windows_jni.dll"); - WindowsJniLoader.loadJniForTesting(jniDllPath); + WindowsTestUtil.loadJni(); scratchRoot = new File(System.getenv("TEST_TMPDIR")).getAbsolutePath() + "/x"; - deleteAllUnder(scratchRoot); + cleanupScratchDir(); } @After public void cleanupScratchDir() throws Exception { - deleteAllUnder(scratchRoot); - } - - private void deleteAllUnder(String path) throws IOException { - if (new File(scratchRoot).exists()) { - runCommand("cmd.exe /c rd /s /q \"" + scratchRoot + "\""); - } - } - - // Do not use WindowsFileSystem.createDirectoryJunction but reimplement junction creation here. - // If that method were buggy, using it here would compromise the test. - private void createJunctions(Map<String, String> links) throws Exception { - List<String> args = new ArrayList<>(); - boolean first = true; - - // Shell out to cmd.exe to create all junctions in one go. - // Running "cmd.exe /c command1 arg1 arg2 && command2 arg1 ... argN && ..." will run all - // commands within one cmd.exe invocation. - for (Map.Entry<String, String> e : links.entrySet()) { - if (first) { - args.add("cmd.exe /c"); - first = false; - } else { - args.add("&&"); - } - - args.add( - String.format( - "mklink /j \"%s/%s\" \"%s/%s\"", scratchRoot, e.getKey(), scratchRoot, e.getValue())); - } - runCommand(args); - } - - private void runCommand(List<String> args) throws IOException { - runCommand(Joiner.on(' ').join(args)); - } - - private void runCommand(String cmd) throws IOException { - Process p = Runtime.getRuntime().exec(cmd); - try { - // Wait no more than 5 seconds to create all junctions. - p.waitFor(5, TimeUnit.SECONDS); - } catch (InterruptedException e) { - fail("Failed to execute command; cmd: " + cmd); - } - assertWithMessage("Command failed: " + cmd).that(p.exitValue()).isEqualTo(0); + WindowsTestUtil.deleteAllUnder(scratchRoot); } private Path scratchDir(String path) throws IOException { @@ -124,7 +73,7 @@ public class WindowsFileOperationsTest { public void testMockJunctionCreation() throws Exception { String root = scratchDir("dir").getParent().toString(); scratchFile("dir/file.txt", "hello"); - createJunctions(ImmutableMap.of("junc", "dir")); + WindowsTestUtil.createJunctions(scratchRoot, ImmutableMap.of("junc", "dir")); String[] children = new File(root + "/junc").list(); assertThat(children).isNotNull(); assertThat(children).hasLength(1); @@ -154,7 +103,7 @@ public class WindowsFileOperationsTest { scratchFile("shrttrgt/file1.txt", "hello"); scratchFile("longtargetpath/file2.txt", "hello"); - createJunctions(junctions); + WindowsTestUtil.createJunctions(scratchRoot, 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 45d7720656..23171afe93 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 @@ -14,19 +14,81 @@ package com.google.devtools.build.lib.windows.util; +import static com.google.common.truth.Truth.assertWithMessage; +import static org.junit.Assert.fail; + +import com.google.common.base.Joiner; +import com.google.devtools.build.lib.windows.WindowsJniLoader; import java.io.BufferedReader; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** Utilities for running Java tests on Windows. */ -public class WindowsTestUtil { +public final class WindowsTestUtil { + private WindowsTestUtil() {} + private static Map<String, String> runfiles; + public static void loadJni() throws Exception { + String jniDllPath = WindowsTestUtil.getRunfile("io_bazel/src/main/native/windows_jni.dll"); + WindowsJniLoader.loadJniForTesting(jniDllPath); + } + + // 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 { + List<String> args = new ArrayList<>(); + boolean first = true; + + // Shell out to cmd.exe to create all junctions in one go. + // Running "cmd.exe /c command1 arg1 arg2 && command2 arg1 ... argN && ..." will run all + // commands within one cmd.exe invocation. + for (Map.Entry<String, String> e : links.entrySet()) { + if (first) { + args.add("cmd.exe /c"); + first = false; + } else { + args.add("&&"); + } + + args.add( + String.format( + "mklink /j \"%s/%s\" \"%s/%s\"", scratchRoot, e.getKey(), scratchRoot, e.getValue())); + } + runCommand(args); + } + + public static void deleteAllUnder(String path) throws IOException { + if (new File(path).exists()) { + runCommand("cmd.exe /c rd /s /q \"" + path + "\""); + } + } + + private static void runCommand(List<String> args) throws IOException { + runCommand(Joiner.on(' ').join(args)); + } + + private static void runCommand(String cmd) throws IOException { + Process p = Runtime.getRuntime().exec(cmd); + try { + // Wait no more than 5 seconds to create all junctions. + p.waitFor(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + fail("Failed to execute command; cmd: " + cmd); + } + assertWithMessage("Command failed: " + cmd).that(p.exitValue()).isEqualTo(0); + } + public static String getRunfile(String runfilesPath) throws IOException { ensureRunfilesParsed(); return runfiles.get(runfilesPath); @@ -40,15 +102,16 @@ public class WindowsTestUtil { runfiles = new HashMap<>(); InputStream fis = new FileInputStream(System.getenv("RUNFILES_MANIFEST_FILE")); InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); - BufferedReader br = new BufferedReader(isr); - String line; - while ((line = br.readLine()) != null) { - String[] splitLine = line.split(" "); // This is buggy when the path contains spaces - if (splitLine.length != 2) { - continue; - } + try (BufferedReader br = new BufferedReader(isr)) { + String line; + while ((line = br.readLine()) != null) { + String[] splitLine = line.split(" "); // This is buggy when the path contains spaces + if (splitLine.length != 2) { + continue; + } - runfiles.put(splitLine[0], splitLine[1]); + runfiles.put(splitLine[0], splitLine[1]); + } } } } |