diff options
author | 2017-02-15 16:08:45 +0000 | |
---|---|---|
committer | 2017-02-15 19:19:31 +0000 | |
commit | 81f92feeb8ae8e22b62edf90f6b7d44baaec2fbd (patch) | |
tree | 31d0d3d3ce5aa6ceb919d212538b3b5a658f62fc /src/test/java/com | |
parent | 3635539af5cce8c8d3795f745c678c890b45e9b5 (diff) |
Windows: use JNI CreateJunction in Java code
Use the new CreateJunction in the Windows JNI code
every time we need to create junctions. This means
updating WindowsFileOperations and related tests.
Add test for WindowsFileSystem.createSymbolicLink.
See https://github.com/bazelbuild/bazel/issues/2238
--
Change-Id: I5827e2e70e8e147f5f102fabf95fa9a148b3bcdc
Reviewed-on: https://cr.bazel.build/8896
PiperOrigin-RevId: 147598107
MOS_MIGRATED_REVID=147598107
Diffstat (limited to 'src/test/java/com')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/vfs/WindowsFileSystemTest.java | 46 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/windows/util/WindowsTestUtil.java | 25 |
2 files changed, 49 insertions, 22 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/vfs/WindowsFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/WindowsFileSystemTest.java index 6f035c770b..ea08b22f35 100644 --- a/src/test/java/com/google/devtools/build/lib/vfs/WindowsFileSystemTest.java +++ b/src/test/java/com/google/devtools/build/lib/vfs/WindowsFileSystemTest.java @@ -17,10 +17,14 @@ package com.google.devtools.build.lib.vfs; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertSame; +import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; import com.google.devtools.build.lib.testutil.TestSpec; import com.google.devtools.build.lib.util.OS; +import com.google.devtools.build.lib.windows.WindowsFileOperations; import com.google.devtools.build.lib.windows.util.WindowsTestUtil; import java.io.File; import java.io.IOException; @@ -320,4 +324,46 @@ public class WindowsFileSystemTest { assertThat(r1).isNotEqualTo(q1); assertThat(r1).isNotSameAs(q1); } + + @Test + public void testCreateSymbolicLink() throws Exception { + // Create the `scratchRoot` directory. + assertThat(fs.getPath(scratchRoot).createDirectory()).isTrue(); + // Create symlink with directory target, relative path. + Path link1 = fs.getPath(scratchRoot).getRelative("link1"); + fs.createSymbolicLink(link1, new PathFragment("..")); + // Create symlink with directory target, absolute path. + Path link2 = fs.getPath(scratchRoot).getRelative("link2"); + fs.createSymbolicLink(link2, fs.getPath(scratchRoot).getRelative("link1").asFragment()); + // Create scratch files that'll be symlink targets. + testUtil.scratchFile("foo.txt", "hello"); + testUtil.scratchFile("bar.txt", "hello"); + // Create symlink with file target, relative path. + Path link3 = fs.getPath(scratchRoot).getRelative("link3"); + fs.createSymbolicLink(link3, new PathFragment("foo.txt")); + // Create symlink with file target, absolute path. + Path link4 = fs.getPath(scratchRoot).getRelative("link4"); + fs.createSymbolicLink(link4, fs.getPath(scratchRoot).getRelative("bar.txt").asFragment()); + // Assert that link1 and link2 are true junctions and have the right contents. + for (Path p : ImmutableList.of(link1, link2)) { + assertThat(WindowsFileOperations.isJunction(p.getPathString())).isTrue(); + assertThat(p.isSymbolicLink()).isTrue(); + assertThat( + Iterables.transform( + Arrays.asList(new File(p.getPathString()).listFiles()), + new Function<File, String>() { + @Override + public String apply(File input) { + return input.getName(); + } + })) + .containsExactly("x"); + } + // Assert that link3 and link4 are copies of files. + for (Path p : ImmutableList.of(link3, link4)) { + assertThat(WindowsFileOperations.isJunction(p.getPathString())).isFalse(); + assertThat(p.isSymbolicLink()).isFalse(); + assertThat(p.isFile()).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 77268751fe..7a73b57368 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 @@ -21,13 +21,13 @@ import com.google.common.base.Joiner; import com.google.common.base.Strings; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.build.lib.vfs.Path; +import com.google.devtools.build.lib.windows.WindowsFileOperations; import com.google.devtools.build.lib.windows.WindowsJniLoader; import com.google.devtools.build.lib.windows.WindowsRunfiles; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -55,31 +55,12 @@ public final class WindowsTestUtil { * * <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 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())); + WindowsFileOperations.createJunction( + scratchRoot + "/" + e.getKey(), scratchRoot + "/" + e.getValue()); } - runCommand(args); for (Map.Entry<String, String> e : links.entrySet()) { assertWithMessage( |