|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 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.maven; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import org.assertj.core.api.Assertions; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import com.diffplug.spotless.ProcessRunner; |
| 26 | + |
| 27 | +class IdeHookTest extends MavenIntegrationHarness { |
| 28 | + private String output, error; |
| 29 | + private File dirty, clean, diverge, outofbounds; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void before() throws IOException { |
| 33 | + writePomWithFormatSteps("<includes>\n" + |
| 34 | + " <include>DIRTY.md</include>\n" + |
| 35 | + " <include>CLEAN.md</include>\n" + |
| 36 | + " </includes>\n" + |
| 37 | + " <replace>\n" + |
| 38 | + " <name>Greetings to Mars</name>\n" + |
| 39 | + " <search>World</search>\n" + |
| 40 | + " <replacement>Mars</replacement>\n" + |
| 41 | + " </replace>"); |
| 42 | + |
| 43 | + dirty = setFile("DIRTY.md").toContent("World"); |
| 44 | + clean = setFile("CLEAN.md").toContent("Mars"); |
| 45 | + outofbounds = setFile("OUTOFBOUNDS.md").toContent("Mars"); |
| 46 | + ; |
| 47 | + } |
| 48 | + |
| 49 | + private void runWith(String... arguments) throws IOException, InterruptedException { |
| 50 | + ProcessRunner.Result result = mavenRunner() |
| 51 | + .withArguments(arguments) |
| 52 | + .runNoError(); |
| 53 | + |
| 54 | + this.output = result.stdOutUtf8(); |
| 55 | + this.error = result.stdErrUtf8(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void dirty() throws IOException, InterruptedException { |
| 60 | + runWith("spotless:apply", "--quiet", "-DspotlessIdeHook=\"" + dirty.getAbsolutePath() + "\"", "-DspotlessIdeHookUseStdOut=true"); |
| 61 | + Assertions.assertThat(output).isEqualTo("Mars"); |
| 62 | + Assertions.assertThat(error).startsWith("IS DIRTY"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void clean() throws IOException, InterruptedException { |
| 67 | + runWith("spotless:apply", "--quiet", "-DspotlessIdeHook=" + clean.getAbsolutePath(), "-DspotlessIdeHookUseStdOut=true"); |
| 68 | + Assertions.assertThat(output).isEmpty(); |
| 69 | + Assertions.assertThat(error).startsWith("IS CLEAN"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void outofbounds() throws IOException, InterruptedException { |
| 74 | + runWith("spotless:apply", "--quiet", "-DspotlessIdeHook=" + outofbounds.getAbsolutePath(), "-DspotlessIdeHookUseStdOut=true"); |
| 75 | + Assertions.assertThat(output).isEmpty(); |
| 76 | + Assertions.assertThat(error).isEmpty(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void notAbsolute() throws IOException, InterruptedException { |
| 81 | + runWith("spotless:apply", "--quiet", "-DspotlessIdeHook=\"pom.xml\"", "-DspotlessIdeHookUseStdOut=true"); |
| 82 | + Assertions.assertThat(output).isEmpty(); |
| 83 | + Assertions.assertThat(error).contains("Argument passed to spotlessIdeHook must be an absolute path"); |
| 84 | + } |
| 85 | +} |
0 commit comments