Skip to content

Commit b3c4893

Browse files
authored
Ignore external git config when running unit tests (#2021 fixes #1990)
2 parents 7770835 + 647f7e8 commit b3c4893

File tree

12 files changed

+91
-9
lines changed

12 files changed

+91
-9
lines changed

Diff for: CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
1315

1416
## [2.45.0] - 2024-01-23
1517
### Added

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

+3-1
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<>();

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

+3-1
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 {

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

+3-1
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 {

Diff for: plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
68

79
## [6.25.0] - 2024-01-23
810
### Added

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

+5-3
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
}

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

+4-1
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

Diff for: plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
68

79
## [2.43.0] - 2024-01-23
810
### Added

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

+3-1
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

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

+3-1
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

Diff for: testlib/build.gradle

+1
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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.List;
23+
24+
import org.eclipse.jgit.lib.StoredConfig;
25+
import org.eclipse.jgit.util.SystemReader;
26+
import org.junit.jupiter.api.extension.AfterEachCallback;
27+
import org.junit.jupiter.api.extension.BeforeEachCallback;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.junit.jupiter.api.extension.ExtensionContext;
30+
import org.junit.jupiter.api.parallel.ResourceAccessMode;
31+
import org.junit.jupiter.api.parallel.ResourceLock;
32+
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ElementType.TYPE, ElementType.METHOD})
35+
@ExtendWith(ClearGitConfig.GitConfigExtension.class)
36+
@ResourceLock(value = "GIT", mode = ResourceAccessMode.READ_WRITE)
37+
public @interface ClearGitConfig {
38+
39+
class GitConfigExtension implements BeforeEachCallback, AfterEachCallback {
40+
41+
@Override
42+
public void beforeEach(ExtensionContext extensionContext) throws Exception {
43+
for (var config : getConfigs()) {
44+
config.clear();
45+
}
46+
}
47+
48+
@Override
49+
public void afterEach(ExtensionContext extensionContext) throws Exception {
50+
for (var config : getConfigs()) {
51+
config.load();
52+
}
53+
}
54+
55+
private static List<StoredConfig> getConfigs() throws Exception {
56+
var reader = SystemReader.getInstance();
57+
return List.of(reader.getUserConfig(), reader.getSystemConfig());
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)