From fb18f39b6c7fc1832280a99aa8983efcd59798a8 Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Wed, 29 Dec 2021 21:45:46 +0100 Subject: [PATCH 1/5] Support custom index files for incremental up-to-date checking --- plugin-maven/CHANGES.md | 3 ++ plugin-maven/README.md | 4 +- .../spotless/maven/AbstractSpotlessMojo.java | 12 +++-- .../maven/incremental/FileIndexConfig.java | 15 +++--- .../maven/incremental/IndexBasedChecker.java | 4 +- .../maven/incremental/NoopChecker.java | 4 +- .../maven/incremental/UpToDateChecker.java | 8 +-- .../maven/incremental/UpToDateChecking.java | 9 ++++ .../incremental/FileIndexConfigTest.java | 17 +++++-- .../maven/incremental/NoopCheckerTest.java | 14 ++++-- .../incremental/UpToDateCheckingTest.java | 49 +++++++++++++++++++ 11 files changed, 110 insertions(+), 29 deletions(-) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 800df98dd3..26615ca78d 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Changed +* Support custom index files for incremental up-to-date checking ([#1055](https://github.com/diffplug/spotless/pull/1055)). + ### Fixed * Remove Java files from default Maven Groovy formatting ([#1051](https://github.com/diffplug/spotless/pull/1051)). * Before this release, the default target of groovy was diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 316291d38a..fd379c8fa8 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -936,6 +936,7 @@ enable incremental up-to-date checking with the following configuration: true + ${project.basedir}/custom-index-file @@ -946,7 +947,7 @@ The index file contains source file paths and corresponding last modified timest It allows Spotless to skip already formatted files that have not changed. **Note:** the index file is located in the `target` directory. Executing `mvn clean` will delete -the index file, and Spotless will need to check/format all the source files. +the index file, and Spotless will need to check/format all the source files. You can override the default index file location with the `indexFile` configuration parameter. Spotless will remove the index file when up-to-date checking is explicitly turned off with the following configuration: @@ -955,6 +956,7 @@ following configuration: false + ${project.basedir}/custom-index-file diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index c28b4e7186..fda30d28c8 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -71,7 +72,7 @@ import com.diffplug.spotless.maven.typescript.Typescript; public abstract class AbstractSpotlessMojo extends AbstractMojo { - + private static final String DEFAULT_INDEX_FILE_NAME = "spotless-index"; private static final String DEFAULT_ENCODING = "UTF-8"; private static final String DEFAULT_LINE_ENDINGS = "GIT_ATTRIBUTES"; static final String GOAL_CHECK = "check"; @@ -324,10 +325,15 @@ private List getFormatterStepFactories() { } private UpToDateChecker createUpToDateChecker(Iterable formatters) { + Path indexFile = upToDateChecking == null ? null : upToDateChecking.getIndexFile(); + if (indexFile == null) { + Path targetDir = project.getBasedir().toPath().resolve(project.getBuild().getDirectory()); + indexFile = targetDir.resolve(DEFAULT_INDEX_FILE_NAME); + } if (upToDateChecking != null && upToDateChecking.isEnabled()) { getLog().info("Up-to-date checking enabled"); - return UpToDateChecker.forProject(project, formatters, getLog()); + return UpToDateChecker.forProject(project, indexFile, formatters, getLog()); } - return UpToDateChecker.noop(project, getLog()); + return UpToDateChecker.noop(project, indexFile, getLog()); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java index 5ea1dcb8b5..99eb1b937d 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java @@ -20,18 +20,18 @@ import org.apache.maven.project.MavenProject; class FileIndexConfig { - - private static final String INDEX_FILE_NAME = "spotless-index"; - private final MavenProject project; private final PluginFingerprint pluginFingerprint; - FileIndexConfig(MavenProject project) { - this(project, PluginFingerprint.empty()); + private final Path indexFile; + + FileIndexConfig(MavenProject project, Path indexFile) { + this(project, indexFile, PluginFingerprint.empty()); } - FileIndexConfig(MavenProject project, PluginFingerprint pluginFingerprint) { + FileIndexConfig(MavenProject project, Path indexFile, PluginFingerprint pluginFingerprint) { this.project = project; + this.indexFile = indexFile; this.pluginFingerprint = pluginFingerprint; } @@ -40,8 +40,7 @@ Path getProjectDir() { } Path getIndexFile() { - Path targetDir = getProjectDir().resolve(project.getBuild().getDirectory()); - return targetDir.resolve(INDEX_FILE_NAME); + return indexFile; } PluginFingerprint getPluginFingerprint() { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java index e58c479f2e..905e15364b 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java @@ -39,9 +39,9 @@ class IndexBasedChecker implements UpToDateChecker { this.log = log; } - static IndexBasedChecker create(MavenProject project, Iterable formatters, Log log) { + static IndexBasedChecker create(MavenProject project, Path indexFile, Iterable formatters, Log log) { PluginFingerprint pluginFingerprint = PluginFingerprint.from(project, formatters); - FileIndexConfig indexConfig = new FileIndexConfig(project, pluginFingerprint); + FileIndexConfig indexConfig = new FileIndexConfig(project, indexFile, pluginFingerprint); FileIndex fileIndex = FileIndex.read(indexConfig, log); return new IndexBasedChecker(fileIndex, log); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java index 0814938f5f..7c22fcfb1e 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java @@ -24,8 +24,8 @@ class NoopChecker implements UpToDateChecker { private NoopChecker() {} - static NoopChecker create(MavenProject project, Log log) { - FileIndexConfig indexConfig = new FileIndexConfig(project); + static NoopChecker create(MavenProject project, Path indexFile, Log log) { + FileIndexConfig indexConfig = new FileIndexConfig(project, indexFile); FileIndex.delete(indexConfig, log); return new NoopChecker(); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index 0851679707..6151936d3c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -30,11 +30,11 @@ public interface UpToDateChecker extends AutoCloseable { void close(); - static UpToDateChecker noop(MavenProject project, Log log) { - return NoopChecker.create(project, log); + static UpToDateChecker noop(MavenProject project, Path indexFile, Log log) { + return NoopChecker.create(project, indexFile, log); } - static UpToDateChecker forProject(MavenProject project, Iterable formatters, Log log) { - return IndexBasedChecker.create(project, formatters, log); + static UpToDateChecker forProject(MavenProject project, Path indexFile, Iterable formatters, Log log) { + return IndexBasedChecker.create(project, indexFile, formatters, log); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java index 098d24fb81..75f6f9e525 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java @@ -15,6 +15,9 @@ */ package com.diffplug.spotless.maven.incremental; +import java.io.File; +import java.nio.file.Path; + import org.apache.maven.plugins.annotations.Parameter; public class UpToDateChecking { @@ -22,7 +25,13 @@ public class UpToDateChecking { @Parameter private boolean enabled; + private String indexFile; + public boolean isEnabled() { return enabled; } + + public Path getIndexFile() { + return indexFile == null ? null : new File(indexFile).toPath(); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java index 17b3a338ba..b35a6b5932 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.nio.file.Path; import java.nio.file.Paths; import org.apache.maven.model.Build; @@ -28,14 +29,16 @@ class FileIndexConfigTest { @Test void returnsCorrectProjectDir() { - FileIndexConfig config = new FileIndexConfig(mavenProject(), PluginFingerprint.from("foo")); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project), PluginFingerprint.from("foo")); assertThat(config.getProjectDir()).isEqualTo(Paths.get("projectDir")); } @Test void returnsCorrectIndexFile() { - FileIndexConfig config = new FileIndexConfig(mavenProject(), PluginFingerprint.from("foo")); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project), PluginFingerprint.from("foo")); assertThat(config.getIndexFile()) .isEqualTo(Paths.get("projectDir", "target", "spotless-index")); @@ -43,14 +46,16 @@ void returnsCorrectIndexFile() { @Test void returnsCorrectPluginFingerprint() { - FileIndexConfig config = new FileIndexConfig(mavenProject(), PluginFingerprint.from("foo")); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project), PluginFingerprint.from("foo")); assertThat(config.getPluginFingerprint()).isEqualTo(PluginFingerprint.from("foo")); } @Test void returnsEmptyPluginFingerprint() { - FileIndexConfig config = new FileIndexConfig(mavenProject()); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project)); assertThat(config.getPluginFingerprint()).isEqualTo(PluginFingerprint.from("")); } @@ -63,4 +68,8 @@ private static MavenProject mavenProject() { project.setBuild(build); return project; } + + private static Path getIndexFile(MavenProject project) { + return project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index"); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java index ae719273e1..0adf6d1d63 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java @@ -49,10 +49,14 @@ class NoopCheckerTest extends ResourceHarness { private Path existingSourceFile; private Path nonExistingSourceFile; + private Path getIndexFile() { + return project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index"); + } + @BeforeEach void beforeEach() throws Exception { project = buildMavenProject(); - indexFile = new FileIndexConfig(project).getIndexFile(); + indexFile = new FileIndexConfig(project, getIndexFile()).getIndexFile(); existingSourceFile = project.getBasedir().toPath().resolve("existing.txt"); Files.write(existingSourceFile, "foo".getBytes(UTF_8), CREATE_NEW); nonExistingSourceFile = project.getBasedir().toPath().resolve("non-existing.txt"); @@ -61,12 +65,12 @@ void beforeEach() throws Exception { @Test void deletesExistingIndexFileWhenCreated() { Log log = mock(Log.class); - try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, singletonList(dummyFormatter()), log)) { + try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, getIndexFile(), singletonList(dummyFormatter()), log)) { realChecker.setUpToDate(existingSourceFile); } assertThat(indexFile).exists(); - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, log)) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), log)) { assertThat(noopChecker).isNotNull(); } assertThat(indexFile).doesNotExist(); @@ -78,7 +82,7 @@ void doesNothingWhenIndexFileDoesNotExist() { assertThat(indexFile).doesNotExist(); Log log = mock(Log.class); - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, log)) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), log)) { assertThat(noopChecker).isNotNull(); } assertThat(indexFile).doesNotExist(); @@ -87,7 +91,7 @@ void doesNothingWhenIndexFileDoesNotExist() { @Test void neverUpToDate() { - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, mock(Log.class))) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), mock(Log.class))) { assertThat(noopChecker.isUpToDate(existingSourceFile)).isFalse(); assertThat(noopChecker.isUpToDate(nonExistingSourceFile)).isFalse(); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java index ffcd87c1fe..f5bd3bdb16 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java @@ -19,6 +19,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -65,6 +67,42 @@ void disableUpToDateChecking() throws Exception { assertFormatted(files); } + @Test + void enableUpToDateCheckingCustomIndexFile() throws Exception { + Path tempDirectory = Files.createTempDirectory("index-files"); + Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index"); + assertThat(indexFile.getParent()).doesNotExist(); + assertThat(indexFile).doesNotExist(); + writePomWithUpToDateCheckingEnabledIndexFile(true, tempDirectory + "/${project.groupId}/${project.artifactId}.index"); + + List files = writeUnformattedFiles(1); + String output = runSpotlessApply(); + + assertThat(output).contains("Up-to-date checking enabled"); + assertFormatted(files); + assertThat(indexFile.getParent()).exists(); + assertThat(indexFile).exists(); + } + + @Test + void disableUpToDateCheckingCustomIndexFile() throws Exception { + Path tempDirectory = Files.createTempDirectory("index-files"); + Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index"); + indexFile.toFile().getParentFile().mkdirs(); + indexFile.toFile().createNewFile(); + assertThat(indexFile.getParent()).exists(); + assertThat(indexFile).exists(); + writePomWithUpToDateCheckingEnabledIndexFile(false, tempDirectory + "/${project.groupId}/${project.artifactId}.index"); + + List files = writeUnformattedFiles(1); + String output = runSpotlessApply(); + + assertThat(output).doesNotContain("Up-to-date checking enabled"); + assertFormatted(files); + assertThat(indexFile.getParent()).exists(); + assertThat(indexFile).doesNotExist(); + } + @Test void spotlessApplyRecordsCorrectlyFormattedFiles() throws Exception { writePomWithUpToDateCheckingEnabled(true); @@ -141,6 +179,17 @@ private void writePomWithUpToDateCheckingEnabled(boolean enabled) throws IOExcep ""); } + private void writePomWithUpToDateCheckingEnabledIndexFile(boolean enabled, String indexFile) throws IOException { + writePom( + "", + " ", + "", + "", + " " + enabled + "", + " " + indexFile + "", + ""); + } + private List writeFormattedFiles(int count) throws IOException { return writeFiles("java/googlejavaformat/JavaCodeFormatted18.test", "formatted", count); } From a43d46b4a34ebf439c24a54528f53360057b3768 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 5 Jan 2022 13:39:12 -0800 Subject: [PATCH 2/5] Fix maven changelog. --- plugin-maven/CHANGES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index f212f69fa4..4347c8117e 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,9 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] -### Changed +### Added * Support custom index files for incremental up-to-date checking ([#1055](https://github.com/diffplug/spotless/pull/1055)). - ### Fixed * Remove Java files from default Maven Groovy formatting ([#1051](https://github.com/diffplug/spotless/pull/1051)). * Before this release, the default target of groovy was From 9570efe089908ffb54f5b6507b10d7a9a300ec13 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 5 Jan 2022 14:08:04 -0800 Subject: [PATCH 3/5] Update copyrights to 2022. --- .../java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java | 2 +- .../diffplug/spotless/maven/incremental/FileIndexConfig.java | 2 +- .../diffplug/spotless/maven/incremental/IndexBasedChecker.java | 2 +- .../com/diffplug/spotless/maven/incremental/NoopChecker.java | 2 +- .../diffplug/spotless/maven/incremental/UpToDateChecker.java | 2 +- .../diffplug/spotless/maven/incremental/UpToDateChecking.java | 2 +- .../spotless/maven/incremental/FileIndexConfigTest.java | 2 +- .../diffplug/spotless/maven/incremental/NoopCheckerTest.java | 2 +- .../spotless/maven/incremental/UpToDateCheckingTest.java | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index fda30d28c8..1b5abe9f0c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java index 99eb1b937d..03cddb8982 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java index 905e15364b..1ae32a4b60 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java index 7c22fcfb1e..8d57c0be4a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index 6151936d3c..2fd2f3a1ea 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java index 75f6f9e525..a69448d653 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java index b35a6b5932..4d167bc18f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java index 0adf6d1d63..3bcb1c111c 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java index f5bd3bdb16..4243abceb4 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 15bddf0f3f51d35bcde0dc7d9049c71e37257952 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 5 Jan 2022 14:15:35 -0800 Subject: [PATCH 4/5] `String indexFile` should be marked `@Parameter`. --- .../diffplug/spotless/maven/incremental/UpToDateChecking.java | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java index a69448d653..f4d915c712 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java @@ -25,6 +25,7 @@ public class UpToDateChecking { @Parameter private boolean enabled; + @Parameter private String indexFile; public boolean isEnabled() { From 0a94634887ef2d01ee3063f4587b1e75c3f7169b Mon Sep 17 00:00:00 2001 From: Kostiantyn Liutovych Date: Thu, 6 Jan 2022 11:58:33 +0100 Subject: [PATCH 5/5] Minor refactorings * use java.nio APIs for file manipulations in UpToDateCheckingTest * simplify index file creation in NoopCheckerTest * annotate getter's return type with `@Nullable` --- .../maven/incremental/FileIndexConfig.java | 1 - .../maven/incremental/UpToDateChecking.java | 3 +++ .../maven/incremental/NoopCheckerTest.java | 14 +++++--------- .../maven/incremental/UpToDateCheckingTest.java | 8 ++++---- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java index 03cddb8982..9875602fb2 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java @@ -22,7 +22,6 @@ class FileIndexConfig { private final MavenProject project; private final PluginFingerprint pluginFingerprint; - private final Path indexFile; FileIndexConfig(MavenProject project, Path indexFile) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java index f4d915c712..d5eb6aa941 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java @@ -18,6 +18,8 @@ import java.io.File; import java.nio.file.Path; +import javax.annotation.Nullable; + import org.apache.maven.plugins.annotations.Parameter; public class UpToDateChecking { @@ -32,6 +34,7 @@ public boolean isEnabled() { return enabled; } + @Nullable public Path getIndexFile() { return indexFile == null ? null : new File(indexFile).toPath(); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java index 3bcb1c111c..97482f6f9f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java @@ -49,14 +49,10 @@ class NoopCheckerTest extends ResourceHarness { private Path existingSourceFile; private Path nonExistingSourceFile; - private Path getIndexFile() { - return project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index"); - } - @BeforeEach void beforeEach() throws Exception { project = buildMavenProject(); - indexFile = new FileIndexConfig(project, getIndexFile()).getIndexFile(); + indexFile = project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index"); existingSourceFile = project.getBasedir().toPath().resolve("existing.txt"); Files.write(existingSourceFile, "foo".getBytes(UTF_8), CREATE_NEW); nonExistingSourceFile = project.getBasedir().toPath().resolve("non-existing.txt"); @@ -65,12 +61,12 @@ void beforeEach() throws Exception { @Test void deletesExistingIndexFileWhenCreated() { Log log = mock(Log.class); - try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, getIndexFile(), singletonList(dummyFormatter()), log)) { + try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, indexFile, singletonList(dummyFormatter()), log)) { realChecker.setUpToDate(existingSourceFile); } assertThat(indexFile).exists(); - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), log)) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, log)) { assertThat(noopChecker).isNotNull(); } assertThat(indexFile).doesNotExist(); @@ -82,7 +78,7 @@ void doesNothingWhenIndexFileDoesNotExist() { assertThat(indexFile).doesNotExist(); Log log = mock(Log.class); - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), log)) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, log)) { assertThat(noopChecker).isNotNull(); } assertThat(indexFile).doesNotExist(); @@ -91,7 +87,7 @@ void doesNothingWhenIndexFileDoesNotExist() { @Test void neverUpToDate() { - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), mock(Log.class))) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, mock(Log.class))) { assertThat(noopChecker.isUpToDate(existingSourceFile)).isFalse(); assertThat(noopChecker.isUpToDate(nonExistingSourceFile)).isFalse(); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java index 4243abceb4..c0e4ac2495 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java @@ -69,7 +69,7 @@ void disableUpToDateChecking() throws Exception { @Test void enableUpToDateCheckingCustomIndexFile() throws Exception { - Path tempDirectory = Files.createTempDirectory("index-files"); + Path tempDirectory = newFolder("index-files").toPath(); Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index"); assertThat(indexFile.getParent()).doesNotExist(); assertThat(indexFile).doesNotExist(); @@ -86,10 +86,10 @@ void enableUpToDateCheckingCustomIndexFile() throws Exception { @Test void disableUpToDateCheckingCustomIndexFile() throws Exception { - Path tempDirectory = Files.createTempDirectory("index-files"); + Path tempDirectory = newFolder("index-files").toPath(); Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index"); - indexFile.toFile().getParentFile().mkdirs(); - indexFile.toFile().createNewFile(); + Files.createDirectories(indexFile.getParent()); + Files.createFile(indexFile); assertThat(indexFile.getParent()).exists(); assertThat(indexFile).exists(); writePomWithUpToDateCheckingEnabledIndexFile(false, tempDirectory + "/${project.groupId}/${project.artifactId}.index");