Skip to content

Commit

Permalink
Merge pull request #323 from larsgrefer
Browse files Browse the repository at this point in the history
* gh-323:
  Polish "Support up-to-date checking of Format task"
  Support up-to-date checking of Format task

Closes gh-323
  • Loading branch information
wilkinsona committed May 24, 2022
2 parents c63853c + ae64820 commit 837a166
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,11 +25,8 @@
import java.util.stream.Collectors;

import org.gradle.api.GradleException;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import io.spring.javaformat.formatter.FileEdit;
Expand Down Expand Up @@ -72,12 +69,6 @@ public void checkFormatting() throws IOException, InterruptedException {
}
}

@Override
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}

@OutputFile
public File getReportLocation() {
return this.reportLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,8 @@
import java.io.IOException;

import org.gradle.api.GradleException;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.OutputFiles;
import org.gradle.api.tasks.TaskAction;

import io.spring.javaformat.formatter.FileEdit;
Expand Down Expand Up @@ -51,4 +53,9 @@ public void format() throws IOException, InterruptedException {
}
}

@OutputFiles
public FileTree getOutputFiles() {
return super.getSource();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import java.nio.charset.Charset;
import java.util.stream.Stream;

import org.gradle.api.file.FileTree;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SourceTask;

import io.spring.javaformat.config.IndentationStyle;
Expand Down Expand Up @@ -84,6 +88,13 @@ public Property<JavaBaseline> getJavaBaseline() {
return this.javaBaseline;
}

@Override
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}

/**
* Format the source files and provide a {@link Stream} of {@link FileEdit} instances.
* @return the file edits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -50,6 +52,45 @@ public void checkOk() throws IOException {
assertThat(formattedContent).contains("class Simple {").contains(" public static void main");
}

@Test
public void checkUpToDate() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");
// Format that changes files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Format of already formatted files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Up-to-date
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}

@Test
public void notUpToDateWhenJavaBaselineChanges() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");
// Format that changes files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Format of already formatted files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Up-to-date
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("java-baseline=8"));
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@Test
public void notUpToDateWhenIndentationStyleChanges() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");
// Format that changes files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Format of already formatted files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Up-to-date
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("indentation-style=spaces"));
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@Test
public void checkSpacesOk() throws IOException {
BuildResult result = this.gradleBuild.source("src/test/resources/format-spaces").build("format");
Expand Down

0 comments on commit 837a166

Please sign in to comment.