Browse Source

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 <matthias.sohn@sap.com>
stable-5.1
Matthias Sohn 5 years ago
parent
commit
c28167e103
  1. 12
      org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java
  2. 45
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ConfigTest.java

12
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java

@ -128,11 +128,8 @@ public abstract class LocalDiskRepositoryTestCase {
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 abstract class LocalDiskRepositoryTestCase {
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");

45
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 class ConfigTest extends CLIRepositoryTestCase {
}
}
@SuppressWarnings("boxing")
@Test
public void testListConfig() throws Exception {
boolean isWindows = SystemReader.getInstance().getProperty("os.name")
@ -73,19 +74,31 @@ public class ConfigTest extends CLIRepositoryTestCase {
.equals("Mac OS X");
String[] output = execute("git config --list");
List<String> 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<String, String> 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<String, String> parseOptions(String[] output) {
Map<String, String> 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;
}
}

Loading…
Cancel
Save