Skip to content

Commit acafe5c

Browse files
committed
Ignore external git config in unit tests
1 parent 14204e4 commit acafe5c

File tree

9 files changed

+86
-9
lines changed

9 files changed

+86
-9
lines changed

lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,9 +27,11 @@
2727
import org.junit.jupiter.api.Test;
2828

2929
import com.diffplug.common.base.StringPrinter;
30+
import com.diffplug.spotless.ClearGitConfig;
3031
import com.diffplug.spotless.LineEnding;
3132
import com.diffplug.spotless.ResourceHarness;
3233

34+
@ClearGitConfig
3335
class GitAttributesTest extends ResourceHarness {
3436
private List<File> testFiles(String prefix) {
3537
List<File> result = new ArrayList<>();

lib-extra/src/test/java/com/diffplug/spotless/extra/GitRachetMergeBaseTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,8 +27,10 @@
2727
import org.eclipse.jgit.lib.RefDatabase;
2828
import org.junit.jupiter.api.Test;
2929

30+
import com.diffplug.spotless.ClearGitConfig;
3031
import com.diffplug.spotless.ResourceHarness;
3132

33+
@ClearGitConfig
3234
class GitRachetMergeBaseTest extends ResourceHarness {
3335
@Test
3436
void test() throws IllegalStateException, GitAPIException, IOException {

lib-extra/src/test/java/com/diffplug/spotless/extra/GitWorkaroundsTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,9 +27,11 @@
2727
import org.junit.jupiter.api.Nested;
2828
import org.junit.jupiter.api.Test;
2929

30+
import com.diffplug.spotless.ClearGitConfig;
3031
import com.diffplug.spotless.ResourceHarness;
3132
import com.diffplug.spotless.extra.GitWorkarounds.RepositorySpecificResolver;
3233

34+
@ClearGitConfig
3335
class GitWorkaroundsTest extends ResourceHarness {
3436
@Test
3537
void inline() throws IOException, GitAPIException {

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,9 @@
3636
import org.junit.jupiter.params.ParameterizedTest;
3737
import org.junit.jupiter.params.provider.ValueSource;
3838

39+
import com.diffplug.spotless.ClearGitConfig;
40+
41+
@ClearGitConfig
3942
class GitRatchetGradleTest extends GradleIntegrationHarness {
4043
private static final String TEST_PATH = "src/markdown/test.md";
4144

@@ -268,10 +271,9 @@ public BuildResultAssertion outcome(String taskPath, TaskOutcome expected) {
268271
private RevCommit addAndCommit(Git git) throws NoFilepatternException, GitAPIException {
269272
PersonIdent emptyPerson = new PersonIdent("jane doe", "[email protected]", new Date(0), TimeZone.getTimeZone("UTC"));
270273
git.add().addFilepattern(".").call();
271-
RevCommit commit = git.commit().setMessage("baseline")
274+
return git.commit().setMessage("baseline")
272275
.setCommitter(emptyPerson)
273276
.setAuthor(emptyPerson)
274277
.call();
275-
return commit;
276278
}
277279
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,9 @@
2121
import org.eclipse.jgit.api.Git;
2222
import org.junit.jupiter.api.Test;
2323

24+
import com.diffplug.spotless.ClearGitConfig;
25+
26+
@ClearGitConfig
2427
class LicenseHeaderTest extends GradleIntegrationHarness {
2528
private static final String NOW = String.valueOf(YearMonth.now().getYear());
2629

plugin-maven/src/test/java/com/diffplug/spotless/maven/GitRatchetMavenTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,9 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import com.diffplug.common.base.StringPrinter;
28+
import com.diffplug.spotless.ClearGitConfig;
2829

30+
@ClearGitConfig
2931
class GitRatchetMavenTest extends MavenIntegrationHarness {
3032
private static final String TEST_PATH = "src/markdown/test.md";
3133

plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderRatchetTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,10 @@
2121
import org.eclipse.jgit.api.Git;
2222
import org.junit.jupiter.api.Test;
2323

24+
import com.diffplug.spotless.ClearGitConfig;
2425
import com.diffplug.spotless.maven.MavenIntegrationHarness;
2526

27+
@ClearGitConfig
2628
class LicenseHeaderRatchetTest extends MavenIntegrationHarness {
2729
private static final String NOW = String.valueOf(YearMonth.now().getYear());
2830

testlib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies {
1717

1818
implementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
1919
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"
20+
implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
2021
implementation gradleTestKit()
2122
}
2223

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
import org.eclipse.jgit.lib.StoredConfig;
26+
import org.eclipse.jgit.util.SystemReader;
27+
import org.junit.jupiter.api.extension.AfterAllCallback;
28+
import org.junit.jupiter.api.extension.BeforeAllCallback;
29+
import org.junit.jupiter.api.extension.ExtendWith;
30+
import org.junit.jupiter.api.extension.ExtensionContext;
31+
import org.junit.jupiter.api.parallel.ResourceAccessMode;
32+
import org.junit.jupiter.api.parallel.ResourceLock;
33+
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Target({ElementType.TYPE, ElementType.METHOD})
36+
@ExtendWith(ClearGitConfig.GitConfigExtension.class)
37+
@ResourceLock(value = "GIT", mode = ResourceAccessMode.READ_WRITE)
38+
public @interface ClearGitConfig {
39+
40+
class GitConfigExtension implements BeforeAllCallback, AfterAllCallback {
41+
42+
@Override
43+
public void beforeAll(ExtensionContext extensionContext) throws Exception {
44+
for (var config : getConfigs()) {
45+
config.clear();
46+
}
47+
}
48+
49+
@Override
50+
public void afterAll(ExtensionContext extensionContext) throws Exception {
51+
for (var config : getConfigs()) {
52+
config.load();
53+
}
54+
}
55+
56+
private List<StoredConfig> getConfigs() throws Exception {
57+
var reader = SystemReader.getInstance();
58+
return Arrays.asList(reader.getUserConfig(), reader.getSystemConfig());
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)