From c28167e1036fe132f8a66db7943d54ee8d869d34 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Sat, 10 Aug 2019 23:44:39 +0200 Subject: [PATCH] Ensure LocalDiskRepositoryTestCase#setup fully uses MockSystemReader FS#getFileStoreAttributes used the real userConfig and not the mocked one. This led to test errors when running tests with Bazel since it sandboxes tests which prevents they can write to ~/.gitconfig. Fix this by first preparing the MockedSystemReader and the mocked config before calling FS#getFileStoreAttributes. Also fix ConfigTest which broke due to this change since it inherits from LocalDiskRepositoryTestCase and calls its setup method which was changed here. We can no longer assert by comparing plain text since FS adds FileStoreAttributes to the mocked userConfig. Also the default options seen by this test changed since we now use a mocked config. Change-Id: I76bc7c94953fe979266147d3b309a68dda9d4dfe Signed-off-by: Matthias Sohn --- .../junit/LocalDiskRepositoryTestCase.java | 12 ++--- .../tst/org/eclipse/jgit/pgm/ConfigTest.java | 45 ++++++++++++------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java index af23ad1e3..cce83c00b 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java @@ -128,11 +128,8 @@ public void setUp() throws Exception { if (!tmp.delete() || !tmp.mkdir()) throw new IOException("Cannot create " + tmp); - // measure timer resolution before the test to avoid time critical tests - // are affected by time needed for measurement - FS.getFileStoreAttributes(tmp.toPath().getParent()); - mockSystemReader = new MockSystemReader(); + SystemReader.setInstance(mockSystemReader); mockSystemReader.userGitConfig = new FileBasedConfig(new File(tmp, "usergitconfig"), FS.DETECTED); // We have to set autoDetach to false for tests, because tests expect to be able @@ -142,7 +139,12 @@ public void setUp() throws Exception { null, ConfigConstants.CONFIG_KEY_AUTODETACH, false); mockSystemReader.userGitConfig.save(); ceilTestDirectories(getCeilings()); - SystemReader.setInstance(mockSystemReader); + + // Measure timer resolution before the test to avoid time critical tests + // are affected by time needed for measurement. + // The MockSystemReader must be configured first since we need to use + // the same one here + FS.getFileStoreAttributes(tmp.toPath().getParent()); author = new PersonIdent("J. Author", "jauthor@example.com"); committer = new PersonIdent("J. Committer", "jcommitter@example.com"); diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java index 0ce645139..1ce86d15e 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java @@ -43,14 +43,14 @@ package org.eclipse.jgit.pgm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.CLIRepositoryTestCase; -import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.SystemReader; import org.junit.Before; import org.junit.Test; @@ -65,6 +65,7 @@ public void setUp() throws Exception { } } + @SuppressWarnings("boxing") @Test public void testListConfig() throws Exception { boolean isWindows = SystemReader.getInstance().getProperty("os.name") @@ -73,19 +74,31 @@ public void testListConfig() throws Exception { .equals("Mac OS X"); String[] output = execute("git config --list"); - List expect = new ArrayList<>(); - expect.add("gc.autoDetach=false"); - expect.add("core.filemode=" + !isWindows); - expect.add("core.logallrefupdates=true"); - if (isMac) - expect.add("core.precomposeunicode=true"); - expect.add("core.repositoryformatversion=0"); - if (!FS.DETECTED.supportsSymlinks()) - expect.add("core.symlinks=false"); - expect.add(""); // ends with LF (last line empty) - assertEquals("expected default configuration", - Arrays.asList(expect.toArray()).toString(), - Arrays.asList(output).toString()); + + Map options = parseOptions(output); + + assertEquals(!isWindows, Boolean.valueOf(options.get("core.filemode"))); + assertTrue((Boolean.valueOf(options.get("core.logallrefupdates")))); + if (isMac) { + assertTrue( + (Boolean.valueOf(options.get("core.precomposeunicode")))); + } + assertEquals(Integer.valueOf(0), + Integer.valueOf(options.get("core.repositoryformatversion"))); + } + + private Map parseOptions(String[] output) { + Map options = new HashMap<>(); + Arrays.stream(output).forEachOrdered(s -> { + int p = s.indexOf('='); + if (p == -1) { + return; + } + String key = s.substring(0, p); + String value = s.substring(p + 1); + options.put(key, value); + }); + return options; } }