David Carr avatar David Carr committed b8d34bf

implement xml report

Comments (0)

Files changed (3)

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

 package us.carrclan.david.gradle.taglist
 
+import groovy.xml.MarkupBuilder
 import org.gradle.api.tasks.Input
 import org.gradle.api.tasks.Nested
 import org.gradle.api.tasks.SourceTask
     }
 
     private void outputReports(Map<TagDefinition, Set<TagLocation>> results) {
+        // TODO: add support for html
         if (reports.text.enabled) {
             outputText(results)
         }
+        if (reports.xml.enabled) {
+            outputXml(results)
+        }
     }
 
     private void outputText(Map<TagDefinition, Set<TagLocation>> results) {
         }
     }
 
+    private void outputXml(Map<TagDefinition, Set<TagLocation>> results) {
+        reports.xml.destination.withWriter { Writer writer ->
+            MarkupBuilder xml = new MarkupBuilder(writer)
+            xml.doubleQuotes = true
+            xml.report() {
+                results.each { curTag, curMatches ->
+                    tag(name: curTag.name) {
+                        patterns() {
+                            for (curPattern in curTag.patterns) {
+                                pattern(curPattern.pattern())
+                            }
+                        }
+                        matches() {
+                            for (curMatch in curMatches) {
+                                match(file: project.relativePath(curMatch.file),
+                                        line: curMatch.lineNum, column: curMatch.columnNum, curMatch.match)
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     private Map<TagDefinition, Set<TagLocation>> findTagMatches() {
         Map<TagDefinition, Set<TagLocation>> results = [:]
         for (TagDefinition tag : getTags()) {

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

     }
 
     @Test
-    void testDefaultReportGeneration() {
+    void defaultReportGeneration() {
         plugin.apply(project)
         project.apply plugin: "java"
         createJavaSourceFile()
         assert task.state.executed
         assert project.file("build/reports/taglist/taglist.txt").text ==
                 getClass().getResource("report.expected.txt").text
+        assert !project.file("build/reports/taglist/taglist.html").exists()
+        assert !project.file("build/reports/taglist/taglist.xml").exists()
+    }
+
+    @Test
+    void additionalFormatGeneration() {
+        plugin.apply(project)
+        project.apply plugin: "java"
+        createJavaSourceFile()
+        project.taglist.tags = [
+                new TagDefinition(name: "TODO", patterns: [~/(?i)todo/]),
+                new TagDefinition(name:  "XXX", patterns: [~/(?i)xxx/])
+        ]
+        TaglistReport task = (TaglistReport) project.tasks.getByName("taglistReport")
+        task.reports.text.enabled = false
+        // TODO: add test for HTML report
+        task.reports.xml.enabled = true
+        task.execute()
+        assert project.file("build/reports/taglist/taglist.xml").text.trim() ==
+                getClass().getResource("report.expected.xml").text.trim()
+        assert !project.file("build/reports/taglist/taglist.txt").exists()
     }
 
     private void createJavaSourceFile() {

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

+<report>
+  <tag name="TODO">
+    <patterns>
+      <pattern>(?i)todo</pattern>
+    </patterns>
+    <matches>
+      <match file="src/main/java/User.java" line="8" column="8">TODO: add constructor</match>
+      <match file="src/main/java/User.java" line="11" column="9">todo use char[] for password instead of String</match>
+      <match file="src/main/java/User.java" line="14" column="51">TODO: null handling</match>
+    </matches>
+  </tag>
+  <tag name="XXX">
+    <patterns>
+      <pattern>(?i)xxx</pattern>
+    </patterns>
+    <matches>
+      <match file="src/main/java/User.java" line="6" column="33">XXX: store a hash rather than the plaintext password</match>
+    </matches>
+  </tag>
+</report>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.