|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
19 | 19 |
|
| 20 | +import java.time.LocalDate; |
| 21 | + |
20 | 22 | import org.junit.jupiter.api.Test;
|
21 | 23 |
|
22 | 24 | import com.diffplug.spotless.cli.CLIIntegrationHarness;
|
23 | 25 | import com.diffplug.spotless.cli.SpotlessCLIRunner;
|
| 26 | +import com.diffplug.spotless.generic.LicenseHeaderStep; |
24 | 27 |
|
25 | 28 | public class LicenseHeaderTest extends CLIIntegrationHarness {
|
26 | 29 |
|
@@ -75,4 +78,90 @@ void assertDelimiterIsApplied() {
|
75 | 78 |
|
76 | 79 | assertFile("TestFile.java").hasContent("/* License */\n/* keep me */\npublic class TestFile {}");
|
77 | 80 | }
|
| 81 | + |
| 82 | + @Test |
| 83 | + void assertYearModeIsApplied() { |
| 84 | + setFile("TestFile.java").toContent("/* License (c) 2022 */\npublic class TestFile {}"); |
| 85 | + |
| 86 | + SpotlessCLIRunner.Result result = cliRunner() |
| 87 | + .withTargets("TestFile.java") |
| 88 | + .withStep(LicenseHeader.class) |
| 89 | + .withOption("--header", "/* License (c) $YEAR */") |
| 90 | + .withOption("--year-mode", LicenseHeaderStep.YearMode.UPDATE_TO_TODAY.toString()) |
| 91 | + .run(); |
| 92 | + |
| 93 | + assertFile("TestFile.java").hasContent("/* License (c) 2022-" + LocalDate.now().getYear() + " */\npublic class TestFile {}"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void assertYearSeparatorIsApplied() { |
| 98 | + setFile("TestFile.java").toContent("/* License (c) 2022...2023 */\npublic class TestFile {}"); |
| 99 | + |
| 100 | + SpotlessCLIRunner.Result result = cliRunner() |
| 101 | + .withTargets("TestFile.java") |
| 102 | + .withStep(LicenseHeader.class) |
| 103 | + .withOption("--header", "/* License (c) $YEAR */") |
| 104 | + .withOption("--year-mode", LicenseHeaderStep.YearMode.UPDATE_TO_TODAY.toString()) |
| 105 | + |
| 106 | + .withOption("--year-separator", "...") |
| 107 | + .run(); |
| 108 | + |
| 109 | + assertFile("TestFile.java").hasContent("/* License (c) 2022..." + LocalDate.now().getYear() + " */\npublic class TestFile {}"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void assertSkipLinesMatchingIsApplied() { |
| 114 | + setFile("TestFile.java").toContent("/* skip me */\npublic class TestFile {}"); |
| 115 | + |
| 116 | + SpotlessCLIRunner.Result result = cliRunner() |
| 117 | + .withTargets("TestFile.java") |
| 118 | + .withStep(LicenseHeader.class) |
| 119 | + .withOption("--header", "/* License */") |
| 120 | + .withOption("--skip-lines-matching", ".*skip me.*") |
| 121 | + .run(); |
| 122 | + |
| 123 | + assertFile("TestFile.java").hasContent("/* skip me */\n/* License */\npublic class TestFile {}"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void assertPreserveModeIsApplied() { |
| 128 | + setFile("TestFile.java").toContent("/* License (c) 2022 */\npublic class TestFile {}"); |
| 129 | + |
| 130 | + SpotlessCLIRunner.Result result = cliRunner() |
| 131 | + .withTargets("TestFile.java") |
| 132 | + .withStep(LicenseHeader.class) |
| 133 | + .withOption("--header", "/* License (c) $YEAR */") |
| 134 | + .withOption("--year-mode", LicenseHeaderStep.YearMode.PRESERVE.toString()) |
| 135 | + .run(); |
| 136 | + |
| 137 | + assertFile("TestFile.java").hasContent("/* License (c) 2022 */\npublic class TestFile {}"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void assertContentPatternIsAppliedIfMatching() { |
| 142 | + setFile("TestFile.java").toContent("public class TestFile {}"); |
| 143 | + |
| 144 | + SpotlessCLIRunner.Result result = cliRunner() |
| 145 | + .withTargets("TestFile.java") |
| 146 | + .withStep(LicenseHeader.class) |
| 147 | + .withOption("--header", "/* License */") |
| 148 | + .withOption("--content-pattern", ".*TestFile.*") |
| 149 | + .run(); |
| 150 | + |
| 151 | + assertFile("TestFile.java").hasContent("/* License */\npublic class TestFile {}"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void assertContentPatternIsNotAppliedIfNotMatching() { |
| 156 | + setFile("TestFile.java").toContent("public class TestFile {}"); |
| 157 | + |
| 158 | + SpotlessCLIRunner.Result result = cliRunner() |
| 159 | + .withTargets("TestFile.java") |
| 160 | + .withStep(LicenseHeader.class) |
| 161 | + .withOption("--header", "/* License */") |
| 162 | + .withOption("--content-pattern", ".*NonExistent.*") |
| 163 | + .run(); |
| 164 | + |
| 165 | + assertFile("TestFile.java").hasContent("public class TestFile {}"); |
| 166 | + } |
78 | 167 | }
|
0 commit comments