Commits

David Carr committed 3fb2b60

cleanup and finish implementation of text report

  • Participants
  • Parent commits 82b1806

Comments (0)

Files changed (6)

src/main/groovy/us/carrclan/david/gradle/taglist/TagLocation.groovy

     String getMatch() {
         return lineContent.substring(columnNum-1)
     }
-
-    @Override
-    String toString() {
-        return "[${file.path}:${lineNum}:${columnNum}]: ${match}"
-    }
 }

src/main/groovy/us/carrclan/david/gradle/taglist/TaglistPlugin.groovy

 
 import org.gradle.api.Plugin
 import org.gradle.api.Project
-import org.gradle.api.Task
-import org.gradle.api.file.FileVisitDetails
 import org.gradle.api.plugins.JavaBasePlugin
 import org.gradle.api.plugins.ReportingBasePlugin
 import org.gradle.api.reporting.Report
 import org.gradle.api.reporting.ReportingExtension
-import org.gradle.api.tasks.SourceSet
-import org.gradle.api.tasks.diagnostics.ProjectReportTask
 
 class TaglistPlugin implements Plugin<Project> {
     @Override
             extension.conventionMapping.sourceSets = { project.sourceSets }
         }
         project.tasks.withType(TaglistReport) { task ->
-            println "moo"
             task.conventionMapping.with {
-                println "quack"
-//                sourceSets = { extension.sourceSets }
                 tags = { extension.tags }
-//                htmlFile = { new File(extension.reportsDir, "taglist.html") }
             }
             task.source = { extension.sourceSets.collect {it.allSource} as Object[] }
             task.reports.all { Report report ->

src/main/groovy/us/carrclan/david/gradle/taglist/TaglistReport.groovy

 package us.carrclan.david.gradle.taglist
 
-import org.gradle.api.DefaultTask
-import org.gradle.api.file.FileCollection
-import org.gradle.api.file.FileVisitDetails
-import org.gradle.api.internal.ConventionTask
-import org.gradle.api.reporting.Reporting
-import org.gradle.api.reporting.SingleFileReport
-import org.gradle.api.reporting.internal.TaskReportContainer
 import org.gradle.api.tasks.Input
-import org.gradle.api.tasks.InputFiles
 import org.gradle.api.tasks.Nested
-import org.gradle.api.tasks.OutputFile
-import org.gradle.api.tasks.SkipWhenEmpty
-import org.gradle.api.tasks.SourceSet
 import org.gradle.api.tasks.SourceTask
 import org.gradle.api.tasks.TaskAction
-import org.gradle.internal.reflect.Instantiator
 
-import javax.inject.Inject
-
-class TaglistReport extends SourceTask { //implements Reporting<TaglistReports> {
-//    @OutputFile
-//    File htmlFile
+class TaglistReport extends SourceTask {
     @Input
     List<TagDefinition> tags
-//    @SkipWhenEmpty
-//    @InputFiles
-//    FileCollection files
-//    @Input
-//    Collection<SourceSet> sourceSets
 
     @Nested
     TaglistReports reports = new TaglistReports(this)
 
-
-
     @TaskAction
     void run() {
         Map<TagDefinition, Set<TagLocation>> results = findTagMatches()
     }
 
     private void outputReports(Map<TagDefinition, Set<TagLocation>> results) {
-        printResults(results)
         if (reports.text.enabled) {
             outputText(results)
         }
     }
 
     private void outputText(Map<TagDefinition, Set<TagLocation>> results) {
+        String separator = "-" * 60
         reports.text.destination.withPrintWriter { PrintWriter writer ->
-            writer.printf()
-        }
-    }
-
-    private void printResults(Map<TagDefinition, Set<TagLocation>> results) {
-        for (TagDefinition tagDef : getTags()) {
-            Set<TagLocation> locations = results[tagDef]
-            println "${tagDef.name}: ${locations.size()}"
-            for (TagLocation location : locations) {
-                println "  ${location}"
+            writer.println("Summary")
+            writer.println(separator)
+            for (tagDef in getTags()) {
+                writer.printf("%s: %d%n", tagDef.name, results[tagDef].size())
+            }
+            results.findAll {k, v -> !v.empty}.each { tagDef, tagMatches ->
+                writer.println()
+                writer.println(tagDef.name)
+                writer.println(separator)
+                for (tagMatch in tagMatches) {
+                    String relPath = project.relativePath(tagMatch.file)
+                    writer.printf("%s:%d:%d: %s%n", relPath, tagMatch.lineNum, tagMatch.columnNum, tagMatch.match)
+                }
             }
         }
     }
     }
 
     void processFile(File file, Map<TagDefinition, Set<TagLocation>> results) {
-        println "Processing file ${file}"
         file.eachLine { String lineContent, int lineNum ->
             processLine(file, lineContent, lineNum, results)
         }
     }
 
     void processLine(File file, String lineContent, int lineNum, Map<TagDefinition, Set<TagLocation>> results) {
-        println "Processing line ${lineContent}"
         getTags().each { TagDefinition tagDef ->
             processTag(file, lineContent, lineNum, tagDef, results)
         }
     }
 
     void processTag(File file, String lineContent, int lineNum, TagDefinition tagDef, Map<TagDefinition, Set<TagLocation>> results) {
-        println "Processing tag ${tagDef.name}"
         int columnIndex = tagDef.findMatch(lineContent)
         if (columnIndex >= 0) {
             results[tagDef].add(new TagLocation(file: file, lineNum: lineNum, columnNum: columnIndex+1, lineContent: lineContent))

src/test/groovy/us/carrclan/david/gradle/taglist/TaglistPluginTest.groovy

     void taskShouldBeRegisteredOnApply() {
         plugin.apply(project)
         createJavaSourceFile()
-        TaglistReport task = project.tasks.getByName("taglistReport")
+        TaglistReport task = (TaglistReport) project.tasks.getByName("taglistReport")
         assert task.source.collect {project.relativePath(it)} == []
         assert task.tags == []
         assert task.reports.text.enabled
         assert project.relativePath(task.reports.html.destination) == "build/reports/taglist/taglist.html"
         assert project.relativePath(task.reports.text.destination) == "build/reports/taglist/taglist.txt"
         assert project.relativePath(task.reports.xml.destination) == "build/reports/taglist/taglist.xml"
-        //assert project.relativePath(project.tasks.taglist.htmlFile) == "build/reports/taglist/taglist.html"
     }
 
     @Test
         createJavaSourceFile()
         project.taglist.tags = [new TagDefinition(name: "Important", patterns: [~/XXX/, ~/FIXME/] as Set)]
         project.taglist.reportsDir = project.file("build/taglist")
-        TaglistReport task = project.tasks.getByName("taglistReport")
+        TaglistReport task = (TaglistReport) project.tasks.getByName("taglistReport")
         assert task.source.collect {project.relativePath(it)} == ["src/main/java/User.java"]
         assert task.tags.collect {it.name} == ["Important"]
         assert project.relativePath(task.reports.html.destination) == "build/taglist/taglist.html"
     void skippedIfNoSourceFiles() {
         plugin.apply(project)
         project.taglist.tags = [new TagDefinition(name: "TODO", patterns: [~/TODO/])]
-        TaglistReport task = project.tasks.getByName("taglistReport")
+        TaglistReport task = (TaglistReport) project.tasks.getByName("taglistReport")
         task.execute()
         assert task.state.skipped
     }
 
     @Test
-    void testReportGeneration() {
+    void testDefaultReportGeneration() {
         plugin.apply(project)
         project.apply plugin: "java"
         createJavaSourceFile()
-        project.taglist.tags = [new TagDefinition(name: "TODO", patterns: [~/TODO/])]
-        TaglistReport task = project.tasks.getByName("taglistReport")
-//        project.tasks.taglist.tags = [tag(new TagDefinition(name: "TODO", patterns: [~/TODO/])]
-//        assert project.tasks.taglist.sourceSets.collect {it.name} == ["main", "test"]
+        project.taglist.tags = [
+                new TagDefinition(name: "TODO", patterns: [~/TODO/, ~/@todo/]),
+                new TagDefinition(name:  "XXX", patterns: [~/XXX/])
+        ]
+        TaglistReport task = (TaglistReport) project.tasks.getByName("taglistReport")
         assert task.source.collect {project.relativePath(it)} == ["src/main/java/User.java"]
         task.execute()
         assert task.state.executed
-        // TODO: assertions
+        assert project.file("build/reports/taglist/taglist.txt").text ==
+                getClass().getResource("report.expected.txt").text
     }
 
     private void createJavaSourceFile() {
         File file1 = project.file("src/main/java/User.java")
         file1.parentFile.mkdirs()
-        file1.text = """
-        This is my file
-        TODO: do something
-        TODO: do something else
-        fin
-        """
+        file1.text = getClass().getResource("User.java").text
     }
 }

src/test/resources/us/carrclan/david/gradle/taglist/User.java

+package us.carrclan.david.gradle.taglist;
+
+class User {
+    private String name;
+    private String email;
+    private String password; // XXX: store a hash rather than the plaintext password
+
+    // TODO: add constructor
+
+    /**
+     * @todo use char[] for password instead of String
+     */
+    public boolean matches(String password) {
+        return password.equals(this.password); // TODO: null handling
+    }
+}

src/test/resources/us/carrclan/david/gradle/taglist/report.expected.txt

+Summary
+------------------------------------------------------------
+TODO: 3
+XXX: 1
+
+TODO
+------------------------------------------------------------
+src/main/java/User.java:8:8: TODO: add constructor
+src/main/java/User.java:11:8: @todo use char[] for password instead of String
+src/main/java/User.java:14:51: TODO: null handling
+
+XXX
+------------------------------------------------------------
+src/main/java/User.java:6:33: XXX: store a hash rather than the plaintext password