Last active
April 3, 2018 09:38
-
-
Save sw-samuraj/2c09157b8175b5a2365ae4c843690de0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.junit.jupiter.api.AfterEach; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.nio.file.Files; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
class SymlinkTest { | |
private final File baseDir = new File("/home/guido/work"); | |
private final File linkDir = new File(baseDir, "path/to"); | |
private final File link = new File(linkDir, "symlink"); | |
@BeforeEach | |
void setUp() { | |
if (!linkDir.exists()) { | |
linkDir.mkdirs(); | |
} | |
} | |
@AfterEach | |
void tearDown() { | |
link.delete(); | |
linkDir.delete(); | |
new File(baseDir, "path").delete(); | |
} | |
@Test | |
void runCommandInSymlinkDir() throws IOException { | |
Files.createSymbolicLink(link.toPath(), baseDir.toPath()); | |
Process process = new ProcessBuilder() | |
.command("pwd") | |
.directory(link) | |
.start(); | |
String output = getOutput(process); | |
assertEquals(link.getAbsolutePath(), output); | |
} | |
@Test | |
void runCommandInShell() throws IOException { | |
Files.createSymbolicLink(link.toPath(), baseDir.toPath()); | |
String[] cmd = {"/bin/sh", "-c", "cd " + link + " && pwd"}; | |
Process process = Runtime.getRuntime().exec(cmd); | |
String output = getOutput(process); | |
assertEquals(link.getAbsolutePath(), output); | |
} | |
private String getOutput(Process process) throws IOException { | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(process.getInputStream())); | |
return reader.readLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment