diff --git a/CHANGES.md b/CHANGES.md index adf6d109e7..1151dd8430 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) ## [2.45.0] - 2024-01-23 ### Added diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java index a7cde9e58f..8af0478543 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,9 +27,11 @@ import org.junit.jupiter.api.Test; import com.diffplug.common.base.StringPrinter; +import com.diffplug.spotless.ClearGitConfig; import com.diffplug.spotless.LineEnding; import com.diffplug.spotless.ResourceHarness; +@ClearGitConfig class GitAttributesTest extends ResourceHarness { private List testFiles(String prefix) { List result = new ArrayList<>(); diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitRachetMergeBaseTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitRachetMergeBaseTest.java index 02447a36bd..7f2ad99b54 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitRachetMergeBaseTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitRachetMergeBaseTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 DiffPlug + * Copyright 2020-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,8 +27,10 @@ import org.eclipse.jgit.lib.RefDatabase; import org.junit.jupiter.api.Test; +import com.diffplug.spotless.ClearGitConfig; import com.diffplug.spotless.ResourceHarness; +@ClearGitConfig class GitRachetMergeBaseTest extends ResourceHarness { @Test void test() throws IllegalStateException, GitAPIException, IOException { diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitWorkaroundsTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitWorkaroundsTest.java index a90716f5de..b8a1bf8c89 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitWorkaroundsTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitWorkaroundsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,9 +27,11 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import com.diffplug.spotless.ClearGitConfig; import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.extra.GitWorkarounds.RepositorySpecificResolver; +@ClearGitConfig class GitWorkaroundsTest extends ResourceHarness { @Test void inline() throws IOException, GitAPIException { diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index d4ce3b410e..5e7d8823b9 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) ## [6.25.0] - 2024-01-23 ### Added diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java index 0398809f90..388bbbb8d1 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import com.diffplug.spotless.ClearGitConfig; + +@ClearGitConfig class GitRatchetGradleTest extends GradleIntegrationHarness { private static final String TEST_PATH = "src/markdown/test.md"; @@ -268,10 +271,9 @@ public BuildResultAssertion outcome(String taskPath, TaskOutcome expected) { private RevCommit addAndCommit(Git git) throws NoFilepatternException, GitAPIException { PersonIdent emptyPerson = new PersonIdent("jane doe", "jane@doe.com", new Date(0), TimeZone.getTimeZone("UTC")); git.add().addFilepattern(".").call(); - RevCommit commit = git.commit().setMessage("baseline") + return git.commit().setMessage("baseline") .setCommitter(emptyPerson) .setAuthor(emptyPerson) .call(); - return commit; } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java index 4b0784832f..d4c01cdc6d 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,9 @@ import org.eclipse.jgit.api.Git; import org.junit.jupiter.api.Test; +import com.diffplug.spotless.ClearGitConfig; + +@ClearGitConfig class LicenseHeaderTest extends GradleIntegrationHarness { private static final String NOW = String.valueOf(YearMonth.now().getYear()); diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 56cd5a5104..37cc1de12e 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) ## [2.43.0] - 2024-01-23 ### Added diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/GitRatchetMavenTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/GitRatchetMavenTest.java index 7d2b26ee89..c1cb0f78d7 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/GitRatchetMavenTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/GitRatchetMavenTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 DiffPlug + * Copyright 2020-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,9 @@ import org.junit.jupiter.api.Test; import com.diffplug.common.base.StringPrinter; +import com.diffplug.spotless.ClearGitConfig; +@ClearGitConfig class GitRatchetMavenTest extends MavenIntegrationHarness { private static final String TEST_PATH = "src/markdown/test.md"; diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderRatchetTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderRatchetTest.java index cb6d54a1e4..24200020f2 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderRatchetTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderRatchetTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 DiffPlug + * Copyright 2020-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,10 @@ import org.eclipse.jgit.api.Git; import org.junit.jupiter.api.Test; +import com.diffplug.spotless.ClearGitConfig; import com.diffplug.spotless.maven.MavenIntegrationHarness; +@ClearGitConfig class LicenseHeaderRatchetTest extends MavenIntegrationHarness { private static final String NOW = String.valueOf(YearMonth.now().getYear()); diff --git a/testlib/build.gradle b/testlib/build.gradle index ce36b83adb..38e598369e 100644 --- a/testlib/build.gradle +++ b/testlib/build.gradle @@ -17,6 +17,7 @@ dependencies { implementation "com.diffplug.durian:durian-io:${VER_DURIAN}" implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}" + implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}" implementation gradleTestKit() } diff --git a/testlib/src/main/java/com/diffplug/spotless/ClearGitConfig.java b/testlib/src/main/java/com/diffplug/spotless/ClearGitConfig.java new file mode 100644 index 0000000000..1655845831 --- /dev/null +++ b/testlib/src/main/java/com/diffplug/spotless/ClearGitConfig.java @@ -0,0 +1,60 @@ +/* + * Copyright 2024 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.List; + +import org.eclipse.jgit.lib.StoredConfig; +import org.eclipse.jgit.util.SystemReader; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.parallel.ResourceAccessMode; +import org.junit.jupiter.api.parallel.ResourceLock; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@ExtendWith(ClearGitConfig.GitConfigExtension.class) +@ResourceLock(value = "GIT", mode = ResourceAccessMode.READ_WRITE) +public @interface ClearGitConfig { + + class GitConfigExtension implements BeforeEachCallback, AfterEachCallback { + + @Override + public void beforeEach(ExtensionContext extensionContext) throws Exception { + for (var config : getConfigs()) { + config.clear(); + } + } + + @Override + public void afterEach(ExtensionContext extensionContext) throws Exception { + for (var config : getConfigs()) { + config.load(); + } + } + + private static List getConfigs() throws Exception { + var reader = SystemReader.getInstance(); + return List.of(reader.getUserConfig(), reader.getSystemConfig()); + } + } +}