/** Method interface has been changed to * {@link GroovyExtension#importOrder(String...)}.*/ @Deprecated public void importOrder(List<String> importOrder) { getProject().getLogger().warn( StringPrinter.buildStringFromLines( "'importOrder([x, y, z])' is deprecated.", "Use 'importOrder x, y, z' instead.", "For details see https://github.com/diffplug/spotless/tree/master/plugin-gradle#applying-to-java-source")); importOrder(importOrder.toArray(new String[0])); }
static GradleException youShouldTurnOnPaddedCell(SpotlessTask task) { Path rootPath = task.getProject().getRootDir().toPath(); return new GradleException(StringPrinter.buildStringFromLines( "You have a misbehaving rule which can't make up its mind.", "This means that spotlessCheck will fail even after spotlessApply has run.", "", "This is a bug in a formatting rule, not Spotless itself, but Spotless can", "work around this bug and generate helpful bug reports for the broken rule", "if you add 'paddedCell()' to your build.gradle as such: ", "", " spotless {", " format 'someFormat', {", " ...", " paddedCell()", " }", " }", "", "The next time you run spotlessCheck, it will put helpful bug reports into", "'" + rootPath.relativize(diagnoseDir(task).toPath()) + "', and spotlessApply", "and spotlessCheck will be self-consistent from here on out.", "", "For details see " + URL)); }
/** Use {@link #eclipse(String)} instead */ @Deprecated public void eclipseFormatFile(String eclipseVersion, Object eclipseFormatFile) { Objects.requireNonNull(eclipseVersion, "eclipseVersion"); Objects.requireNonNull(eclipseFormatFile, "eclipseFormatFile"); getProject().getLogger().warn( StringPrinter.buildStringFromLines( "'eclipseFormatFile [version] <file>' is deprecated.", "Use 'eclipse([version]).configFile(<file>)' instead.", "For details see https://github.com/diffplug/spotless/tree/master/plugin-gradle#applying-to-java-source")); eclipse(eclipseVersion).configFile(eclipseFormatFile); }
private void assertFailureMessage(Bundle bundle, String... expectedOutput) { String msg = bundle.checkFailureMsg(); String expected = StringPrinter.buildStringFromLines(expectedOutput).trim(); Assert.assertEquals(expected, msg); } }
/** Method interface has been changed to * {@link JavaExtension#importOrder(String...)}.*/ @Deprecated public void importOrder(List<String> importOrder) { getProject().getLogger().warn( StringPrinter.buildStringFromLines( "'importOrder([x, y, z])' is deprecated.", "Use 'importOrder x, y, z' instead.", "For details see https://github.com/diffplug/spotless/tree/master/plugin-gradle#applying-to-java-source")); addStep(ImportOrderStep.createFromOrder(importOrder)); }
private void assertTaskFailure(SpotlessTask task, String... expectedLines) throws Exception { String msg = getTaskErrorMessage(task); String firstLine = "The following files had format violations:\n"; String lastLine = "\nRun 'gradlew spotlessApply' to fix these violations."; Assertions.assertThat(msg).startsWith(firstLine).endsWith(lastLine); String middle = msg.substring(firstLine.length(), msg.length() - lastLine.length()); String expectedMessage = StringPrinter.buildStringFromLines(expectedLines); Assertions.assertThat(middle).isEqualTo(expectedMessage.substring(0, expectedMessage.length() - 1)); }
private void assertFolderContents(String subfolderName, String... files) throws IOException { File subfolder = new File(rootFolder(), subfolderName); Assert.assertTrue(subfolder.isDirectory()); String asList = String.join("\n", Arrays.asList(files)); Assert.assertEquals(StringPrinter.buildStringFromLines(files).trim(), asList); }
public static Provisioner fromProject(Project project) { Objects.requireNonNull(project); return (withTransitives, mavenCoords) -> { try { Dependency[] deps = mavenCoords.stream() .map(project.getBuildscript().getDependencies()::create) .toArray(Dependency[]::new); Configuration config = project.getRootProject().getBuildscript().getConfigurations().detachedConfiguration(deps); config.setDescription(mavenCoords.toString()); config.setTransitive(withTransitives); return config.resolve(); } catch (Exception e) { logger.log(Level.SEVERE, StringPrinter.buildStringFromLines("You probably need to add a repository containing the '" + mavenCoords + "' artifact in the 'build.gradle' of your root project.", "E.g.: 'buildscript { repositories { mavenCentral() }}'", "Note that included buildscripts (using 'apply from') do not share their buildscript repositories with the underlying project.", "You have to specify the missing repository explicitly in the buildscript of the root project."), e); throw e; } }; }
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") static void check(SpotlessTask task, Formatter formatter, List<File> problemFiles) throws IOException { if (problemFiles.isEmpty()) { // if the first pass was successful, then paddedCell() mode is unnecessary task.getLogger().info(StringPrinter.buildStringFromLines( task.getName() + " is in paddedCell() mode, but it doesn't need to be.", "If you remove that option, spotless will run ~2x faster.", "For details see " + URL)); } File diagnoseDir = diagnoseDir(task); File rootDir = task.getProject().getRootDir(); List<File> stillFailing = PaddedCellBulk.check(rootDir, diagnoseDir, formatter, problemFiles); if (!stillFailing.isEmpty()) { throw task.formatViolationsFor(formatter, problemFiles); } } }
private void fixWindowsBugTestcase(String... lines) { String input = StringPrinter.buildStringFromLines(lines); Assert.assertEquals(input, GoogleJavaFormatStep.fixWindowsBug(input, "1.1")); } }
private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) { String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim(); int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith); List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput())); String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1)); Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith); Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size()) .isEqualTo(result.getTasks().size()); } }
private String generatedManifest() { return StringPrinter.buildStringFromLines( "Manifest-Version: 1.0", "Bundle-ManifestVersion: 2", "Bundle-SymbolicName: test", "Bundle-Version: 0.0.0.ERRORSETVERSION", "Export-Package: test;uses:=\"com.diffplug.common.base\";version=\"0.0.0\"", "Import-Package: com.diffplug.common.base;version=\"[3.4,4)\"", "Require-Capability: osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.8))\""); } }
private void testTarget(boolean useDefaultTarget) throws IOException { String target = useDefaultTarget ? "" : "target 'other.gradle'"; String buildContent = StringPrinter.buildStringFromLines( "plugins {", " id 'com.diffplug.gradle.spotless'", "}", "spotless {", " groovyGradle {", target, " licenseHeader('" + HEADER + "', 'plugins')", " }", "}"); setFile("build.gradle").toContent(buildContent); gradleRunner().withArguments("spotlessApply").build(); if (useDefaultTarget) { assertFile("build.gradle").hasContent(HEADER + "\n" + buildContent); } else { assertFile("build.gradle").hasContent(buildContent); } } }
private void testDumpMethod() { StackDumper.dump("some message"); String errOutput = testCaseErr.toString(); Assert.assertTrue(errOutput, errOutput.startsWith(StringPrinter.buildStringFromLines( "+----------\\", "| some message", "| at com.diffplug.common.base.StackDumperTest.testDumpMethod(StackDumperTest.java:96)"))); Assert.assertTrue(errOutput, errOutput.endsWith(StringPrinter.buildStringFromLines( "| at com.diffplug.common.base.StackDumperTest$1.run(StackDumperTest.java:62)", "+----------/"))); }
@Test public void assertCustomJarExcluded() throws IOException { String expected = StringPrinter.buildStringFromLines( "Manifest-Version: 1.0", ""); write("src/main/resources/META-INF/MANIFEST.MF", "Manifest-Version: 1.0"); String buildscriptAddendum = StringPrinter.buildStringFromLines( "task customJar(type: Jar) {", " classifier 'custom'", "}", "osgiBndManifest { ", " mergeWithExisting true", "}"); testCase(buildscriptAddendum, expected, "customJar"); }
@Test public void testDumpFiltered() throws Throwable { // dump with no stack runWithCleanStack(() -> { StackDumper.dump("some message", StackDumper.captureStackBelow(StackDumperTest.class)); Assert.assertEquals(StringPrinter.buildStringFromLines( "+----------\\", "| some message", "| at com.diffplug.common.base.StackDumperTest$1.run(StackDumperTest.java:62)", "+----------/"), testCaseErr.toString()); }); }
@Test public void assertClasspathChanged() throws IOException { // write the normal eclipse file String plainEclipse = testCase("eclipse"); // write the excluded build folder file String underTestEclipse = testCase("com.diffplug.gradle.eclipse.buildproperties"); // assert the expected thing was added to the .project file Assert.assertEquals(StringPrinter.buildStringFromLines( "INSERT", " <classpathentry path=\"\" including=\"META-INF/|images/\" kind=\"src\"/>", ""), Diff.computeDiff(plainEclipse, underTestEclipse)); }
@Test public void assertClasspathChangedPre2_14() throws IOException { // write the normal eclipse file String plainEclipse = testCase("eclipse", "2.13"); // write the excluded build folder file String underTestEclipse = testCase("com.diffplug.gradle.eclipse.projectdeps", "2.13"); // assert the expected thing was added to the .project file Assert.assertEquals(StringPrinter.buildStringFromLines( "DELETE", "kind=\"src\" path=\"/a", "INSERT", "exported=\"true\" path=\"/a\" kind=\"src\" combineaccessrules=\"true"), Diff.computeDiff(plainEclipse, underTestEclipse)); }
@Test public void assertClasspathChangedPost2_14() throws IOException { // write the normal eclipse file String plainEclipse = testCase("eclipse", "2.14-rc-4"); // write the excluded build folder file String underTestEclipse = testCase("com.diffplug.gradle.eclipse.projectdeps", "2.14-rc-4"); // assert the expected thing was added to the .project file Assert.assertEquals(StringPrinter.buildStringFromLines( "INSERT", " exported=\"true\"", "INSERT", " combineaccessrules=\"true\""), Diff.computeDiff(plainEclipse, underTestEclipse)); }
@Test public void testBeforeAfterUnsatisfied() { UnderTest list = new UnderTest(); list.add("batter").after("cake"); list.add("cake").after("batter"); list.assertException(StringPrinter.buildStringFromLines( "Could not satisfy order constraints:", "batter must be after cake", "cake must be after batter")); }