From f5614d471d6d4b8eb54dd3ef8e457317dfd26530 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Sat, 25 Aug 2018 11:33:50 +0200 Subject: Don't inline included configs on FileBasedConfig.save() Bug: 529825 Change-Id: Id23d4602aa2082d7f2dfe15ae92d7b175b1b8944 Signed-off-by: Marc Strapetz --- .../jgit/storage/file/FileBasedConfigTest.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/storage') 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 7f0d60295c..777a784c33 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.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.StringTokenizer; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.util.FS; @@ -67,16 +68,23 @@ public class FileBasedConfigTest { private static final String NAME = "name"; + private static final String EMAIL = "email"; + private static final String ALICE = "Alice"; private static final String BOB = "Bob"; + private static final String ALICE_EMAIL = "alice@home"; + private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = " + ALICE + "\n"; private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = " + BOB + "\n"; + private static final String CONTENT3 = "[" + USER + "]\n\t" + NAME + " = " + + ALICE + "\n" + "[" + USER + "]\n\t" + EMAIL + " = " + ALICE_EMAIL; + private File trash; @Before @@ -233,6 +241,50 @@ public class FileBasedConfigTest { assertEquals(ALICE, config.getString(USER, null, NAME)); } + @Test + public void testIncludeDontInlineIncludedLinesOnSave() + throws IOException, ConfigInvalidException { + // use a content with multiple sections and multiple key/value pairs + // because code for first line works different than for subsequent lines + final File includedFile = createFile(CONTENT3.getBytes(), "dir1"); + + final File file = createFile(new byte[0], "dir2"); + FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED); + config.setString("include", null, "path", + ("../" + includedFile.getParentFile().getName() + "/" + + includedFile.getName())); + + // just by setting the include.path, it won't be included + assertEquals(null, config.getString(USER, null, NAME)); + assertEquals(null, config.getString(USER, null, EMAIL)); + config.save(); + + // and it won't be included after saving + assertEquals(null, config.getString(USER, null, NAME)); + assertEquals(null, config.getString(USER, null, EMAIL)); + + final String expectedText = config.toText(); + assertEquals(2, + new StringTokenizer(expectedText, "\n", false).countTokens()); + + config = new FileBasedConfig(file, FS.DETECTED); + config.load(); + + String actualText = config.toText(); + assertEquals(expectedText, actualText); + // but it will be included after (re)loading + assertEquals(ALICE, config.getString(USER, null, NAME)); + assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL)); + + config.save(); + + actualText = config.toText(); + assertEquals(expectedText, actualText); + // and of course preserved after saving + assertEquals(ALICE, config.getString(USER, null, NAME)); + assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL)); + } + private File createFile(byte[] content) throws IOException { return createFile(content, null); } -- cgit v1.2.3