summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse
diff options
context:
space:
mode:
authorMarc Strapetz <marc.strapetz@syntevo.com>2017-11-18 17:50:30 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2017-12-04 23:38:24 +0100
commit26d78902f8843f24556ba152a38b5f89b21af107 (patch)
tree5673079bcc64a44e1c315b6bf58afceb75490b1c /org.eclipse.jgit.test/tst/org/eclipse
parentb8f257747287df472fe2907c45144ab0dbbc9fcc (diff)
downloadjgit-26d78902f8843f24556ba152a38b5f89b21af107.tar.gz
jgit-26d78902f8843f24556ba152a38b5f89b21af107.zip
FileBasedConfig: support for relative includes
Relative include.path are now resolved against the config's parent directory. include.path starting with ~/ are resolved against the user's home directory Change-Id: I91911ef404126618b1ddd3589294824a0ad919e6 Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java2
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java85
2 files changed, 84 insertions, 3 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
index 7902bb5303..13d546f2b7 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
@@ -855,7 +855,7 @@ public class ConfigTest {
assertEquals("bar", parsed.getString("other", null, "more"));
}
- private static String pathToString(File file) {
+ public static String pathToString(File file) {
final String path = file.getPath();
if (SystemReader.getInstance().isWindows()) {
return path.replace('\\', '/');
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java
index ee845c5325..2134e1b8dc 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java
@@ -52,6 +52,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.ConfigTest;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.IO;
@@ -157,9 +158,89 @@ public class FileBasedConfigTest {
assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
}
+ @Test
+ public void testIncludeAbsolute()
+ throws IOException, ConfigInvalidException {
+ final File includedFile = createFile(CONTENT1.getBytes());
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write("[include]\npath=".getBytes());
+ bos.write(ConfigTest.pathToString(includedFile).getBytes());
+
+ final File file = createFile(bos.toByteArray());
+ final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
+ config.load();
+ assertEquals(ALICE, config.getString(USER, null, NAME));
+ }
+
+ @Test
+ public void testIncludeRelativeDot()
+ throws IOException, ConfigInvalidException {
+ final File includedFile = createFile(CONTENT1.getBytes(), "dir1");
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write("[include]\npath=".getBytes());
+ bos.write(("./" + includedFile.getName()).getBytes());
+
+ final File file = createFile(bos.toByteArray(), "dir1");
+ final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
+ config.load();
+ assertEquals(ALICE, config.getString(USER, null, NAME));
+ }
+
+ @Test
+ public void testIncludeRelativeDotDot()
+ throws IOException, ConfigInvalidException {
+ final File includedFile = createFile(CONTENT1.getBytes(), "dir1");
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write("[include]\npath=".getBytes());
+ bos.write(("../" + includedFile.getParentFile().getName() + "/"
+ + includedFile.getName()).getBytes());
+
+ final File file = createFile(bos.toByteArray(), "dir2");
+ final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
+ config.load();
+ assertEquals(ALICE, config.getString(USER, null, NAME));
+ }
+
+ @Test
+ public void testIncludeRelativeDotDotNotFound()
+ throws IOException, ConfigInvalidException {
+ final File includedFile = createFile(CONTENT1.getBytes());
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write("[include]\npath=".getBytes());
+ bos.write(("../" + includedFile.getName()).getBytes());
+
+ final File file = createFile(bos.toByteArray());
+ final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
+ config.load();
+ assertEquals(null, config.getString(USER, null, NAME));
+ }
+
+ @Test
+ public void testIncludeWithTilde()
+ throws IOException, ConfigInvalidException {
+ final File includedFile = createFile(CONTENT1.getBytes(), "home");
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos.write("[include]\npath=".getBytes());
+ bos.write(("~/" + includedFile.getName()).getBytes());
+
+ final File file = createFile(bos.toByteArray(), "repo");
+ final FS fs = FS.DETECTED.newInstance();
+ fs.setUserHome(includedFile.getParentFile());
+
+ final FileBasedConfig config = new FileBasedConfig(file, fs);
+ config.load();
+ assertEquals(ALICE, config.getString(USER, null, NAME));
+ }
+
private File createFile(byte[] content) throws IOException {
- trash.mkdirs();
- File f = File.createTempFile(getClass().getName(), null, trash);
+ return createFile(content, null);
+ }
+
+ private File createFile(byte[] content, String subdir) throws IOException {
+ File dir = subdir != null ? new File(trash, subdir) : trash;
+ dir.mkdirs();
+
+ File f = File.createTempFile(getClass().getName(), null, dir);
FileOutputStream os = new FileOutputStream(f, true);
try {
os.write(content);