@Test public void doesNothingIfTaskSkipped() throws IOException, TransformerException { Project project = ProjectBuilder.builder().withName("fooproject").withProjectDir(projectDir.getRoot()).build(); Checkstyle checkstyle = createCheckstyleTask(project); checkstyle.setDidWork(false); StyleTaskTimer timer = mock(StyleTaskTimer.class); when(timer.getTaskTimeNanos(checkstyle)).thenReturn(FAILED_CHECKSTYLE_TIME_NANOS); File targetFile = new File(projectDir.getRoot(), "reports/report.xml"); CircleStyleFinalizer finalizer = (CircleStyleFinalizer) project .task(ImmutableMap.of("type", CircleStyleFinalizer.class), "checkstyleTestCircleFinalizer"); finalizer.setReportParser(CheckstyleReportHandler.PARSER); finalizer.setStyleTask(checkstyle); finalizer.setReporting(checkstyle); finalizer.setStyleTaskTimer(timer); finalizer.setTargetFile(targetFile); finalizer.createCircleReport(); assertThat(targetFile).doesNotExist(); assertThat(finalizer.getDidWork()).isFalse(); }
/** * Create Checkstyle tasks * * @param tasks Task container * @param checkstyleConfig Checkstyle configuration * @param sources Source sets * @param files * @param context */ @Finalize public void configureCheckstyleTasks(ModelMap<Checkstyle> tasks, CheckstyleConfig checkstyleConfig, ProjectSourceSet sources, FileOperations files, ProjectContext context) { for (JavaSourceSet source : sources.withType(JavaSourceSet.class)) { String taskName = getCheckstyleTaskName(source); if (!checkstyleConfig.getIgnoreSourceSets().contains(source.getParentName())) { tasks.named(taskName, t -> { File checkstyleConfigFile = getCheckstyleConfigFile(source.getParentName(), files); t.setGroup(JavaBasePlugin.VERIFICATION_GROUP); t.setConfigFile(checkstyleConfigFile); if (checkstyleConfigFile.getParentFile() != null) { t.setConfigDir(context.provider(() -> checkstyleConfigFile.getParentFile())); } t.dependsOn(getUpdateConfigTaskName(source)); }); } } }
/** * Assign some standard tasks and tasks created by third-party plugins to their task groups according to the * order of things for Checkstyle Addons. */ public void adjustTaskGroupAssignments() { final TaskContainer tasks = project.getTasks(); tasks.getByName(BasePlugin.ASSEMBLE_TASK_NAME).setGroup(ARTIFACTS_GROUP_NAME); tasks.getByName(JavaPlugin.JAR_TASK_NAME).setGroup(ARTIFACTS_GROUP_NAME); final SourceSet sqSourceSet = buildUtil.getSourceSet(BuildUtil.SONARQUBE_SOURCE_SET_NAME); tasks.getByName(JavaPlugin.COMPILE_JAVA_TASK_NAME).setGroup(BasePlugin.BUILD_GROUP); tasks.getByName(sqSourceSet.getCompileJavaTaskName()).setGroup(BasePlugin.BUILD_GROUP); tasks.getByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME).setGroup(BasePlugin.BUILD_GROUP); for (final FindBugs fbTask : tasks.withType(FindBugs.class)) { fbTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); } for (final Checkstyle csTask : tasks.withType(Checkstyle.class)) { csTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); } for (final Copy task : tasks.withType(Copy.class)) { if (task.getName().startsWith("process") && task.getName().endsWith("Resources")) { task.setGroup(LifecycleBasePlugin.BUILD_GROUP); } } }
@Override public void apply(Project rootProject) { final String circleReportsDir = System.getenv("CIRCLE_TEST_REPORTS"); if (circleReportsDir == null) { return; } configureBuildFailureFinalizer(rootProject, circleReportsDir); final StyleTaskTimer timer = new StyleTaskTimer(); rootProject.getGradle().addListener(timer); rootProject.allprojects(new Action<Project>() { @Override public void execute(final Project project) { project.getTasks().withType(Checkstyle.class, new Action<Checkstyle>() { @Override public void execute(Checkstyle checkstyleTask) { configureCheckstyleTask(project, checkstyleTask, circleReportsDir, timer); } }); project.getTasks().withType(FindBugs.class, new Action<FindBugs>() { @Override public void execute(FindBugs findbugsTask) { configureFindbugsTask(project, findbugsTask, circleReportsDir, timer); } }); } }); }
private void configureCheckstyleTask( final Project project, final Checkstyle checkstyleTask, final String circleReportsDir, final StyleTaskTimer timer) { // Ensure XML output is enabled checkstyleTask.doFirst(new Action<Task>() { @Override public void execute(Task task) { checkstyleTask.getReports().findByName("xml").setEnabled(true); } }); // Configure the finalizer task CircleStyleFinalizer finalizer = createTask( project.getTasks(), checkstyleTask.getName() + "CircleFinalizer", CircleStyleFinalizer.class); if (finalizer == null) { // Already registered (happens if the user applies us to the root project and subprojects) return; } finalizer.setReportParser(CheckstyleReportHandler.PARSER); finalizer.setStyleTask(checkstyleTask); finalizer.setReporting(checkstyleTask); finalizer.setStyleTaskTimer(timer); finalizer.setTargetFile(new File( new File(circleReportsDir, "checkstyle"), project.getName() + "-" + checkstyleTask.getName() + ".xml")); checkstyleTask.finalizedBy(finalizer); }
@Test public void translatesCheckstyleReport() throws IOException, TransformerException { Project project = ProjectBuilder.builder().withName("fooproject").withProjectDir(projectDir.getRoot()).build(); Checkstyle checkstyle = createCheckstyleTask(project); checkstyle.setDidWork(true); StyleTaskTimer timer = mock(StyleTaskTimer.class); when(timer.getTaskTimeNanos(checkstyle)).thenReturn(FAILED_CHECKSTYLE_TIME_NANOS); File targetFile = new File(projectDir.getRoot(), "reports/report.xml"); CircleStyleFinalizer finalizer = (CircleStyleFinalizer) project .task(ImmutableMap.of("type", CircleStyleFinalizer.class), "checkstyleTestCircleFinalizer"); finalizer.setReportParser(CheckstyleReportHandler.PARSER); finalizer.setStyleTask(checkstyle); finalizer.setReporting(checkstyle); finalizer.setStyleTaskTimer(timer); finalizer.setTargetFile(targetFile); finalizer.createCircleReport(); String report = Resources.toString(targetFile.toURI().toURL(), UTF_8); String expectedReport = Resources.toString(testFile("two-namecheck-failures-checkstyle-report.xml"), UTF_8); assertThat(report).isEqualTo(expectedReport); }
private Checkstyle createCheckstyleTask(Project project) throws IOException { Checkstyle checkstyle = project.getTasks().create("checkstyleTest", Checkstyle.class); SingleFileReport xmlReport = checkstyle.getReports().getByName("xml"); String originalReportXml = readTestFile("two-namecheck-failures-checkstyle.xml"); String modifiedReportXml = originalReportXml.replace(ROOT.toString(), projectDir.getRoot().getCanonicalPath().toString()); File modifiedReportFile = projectDir.newFile(); Files.write(modifiedReportXml, modifiedReportFile, UTF_8); xmlReport.setDestination(modifiedReportFile); return checkstyle; }
@Override protected void configureTaskDefaults(Checkstyle task, final String baseName) { Configuration configuration = project.getConfigurations().getAt("checkstyle"); configureDefaultDependencies(configuration); configureTaskConventionMapping(configuration, task); configureReportsConventionMapping(task, baseName); }
private void configureTaskConventionMapping(Configuration configuration, Checkstyle task) { ConventionMapping taskMapping = task.getConventionMapping(); taskMapping.map("checkstyleClasspath", Callables.returning(configuration)); taskMapping.map("config", () -> extension.getConfig()); taskMapping.map("configProperties", () -> extension.getConfigProperties()); taskMapping.map("ignoreFailures", () -> extension.isIgnoreFailures()); taskMapping.map("showViolations", () -> extension.isShowViolations()); }
private void configureReportsConventionMapping(Checkstyle task, final String baseName) { task.getReports().all(report -> { ConventionMapping reportMapping = conventionMappingOf(report); reportMapping.map("enabled", Callables.returning(true)); reportMapping.map("destination", () -> new File(extension.getReportsDir(), baseName + "." + report.getName())); }); }
public static boolean isStyleTask(Task task) { return task instanceof Checkstyle || task instanceof FindBugs; }
@Test public void onlyTestAndStyleTasks() { listener.afterExecute(mock(org.gradle.api.tasks.testing.Test.class), succeeded()); listener.afterExecute(mock(Checkstyle.class), succeeded()); assertThat(listener.getTestCases()).isEmpty(); }
@Override protected Class<Checkstyle> getTaskType() { return Checkstyle.class; }
@Override protected void configureForSourceSet(final AndroidSourceSet sourceSet, Checkstyle task) { task.setDescription("Run Checkstyle analysis for " + sourceSet.getName() + " classes"); task.setClasspath(getCompileClasspath(sourceSet)); task.setSource(getAllJava(sourceSet)); }