Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
Last active April 3, 2018 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sw-samuraj/2c09157b8175b5a2365ae4c843690de0 to your computer and use it in GitHub Desktop.
Save sw-samuraj/2c09157b8175b5a2365ae4c843690de0 to your computer and use it in GitHub Desktop.
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