Skip to content

Commit

Permalink
Implement junit compatible report for Android Lint (#167)
Browse files Browse the repository at this point in the history
Fixes #167
  • Loading branch information
arunkumar9t2 committed Apr 18, 2024
1 parent 8335e9d commit 40c99d5
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 236 deletions.
8 changes: 8 additions & 0 deletions project_views/default.bazelproject
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
directories:
.
-bazel-bin
-bazel-grab-bazel-common
-bazel-out
-bazel-cache
-bazel-testlogs
-bazel-genfiles

# Automatically includes all relevant targets under the 'directories' above
derive_targets_from_directories: true
Expand All @@ -9,6 +14,9 @@ workspace_type: java

java_language_level: 11

test_sources:
*/src/test/*

additional_languages:
java
kotlin
Expand Down
9 changes: 8 additions & 1 deletion rules/android/lint/lint_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ def _lint_report_action(
fail_on_warning,
fail_on_information,
lint_result_xml_file,
lint_result_junit_xml_file,
partial_results_dir,
jdk_home,
project_xml_file,
Expand Down Expand Up @@ -402,7 +403,8 @@ def _lint_report_action(
args.add("--fail-on-warning", fail_on_warning)
args.add("--fail-on-information", fail_on_information)

args.add("--output-xml", lint_result_xml_file.path)
args.add("--output-xml", lint_result_xml_file)
args.add("--output-junit-xml", lint_result_junit_xml_file)
args.add("--result-code", result_code)

mnemonic = "AndroidLint"
Expand Down Expand Up @@ -458,6 +460,7 @@ def _lint_aspect_impl(target, ctx):
lint_results_dir = ctx.actions.declare_directory("lint/" + target.label.name + "_results_dir")

lint_result_xml_file = ctx.actions.declare_file("lint/" + target.label.name + "_lint_result.xml")
lint_result_junit_xml_file = ctx.actions.declare_file("lint/" + target.label.name + "_lint_result_junit.xml")
lint_result_code_file = ctx.actions.declare_file("lint/" + target.label.name + "_lint_result_code")

# Project Xmls
Expand Down Expand Up @@ -573,6 +576,7 @@ def _lint_aspect_impl(target, ctx):
fail_on_warning = sources.fail_on_warning,
fail_on_information = sources.fail_on_information,
lint_result_xml_file = lint_result_xml_file,
lint_result_junit_xml_file = lint_result_junit_xml_file,
partial_results_dir = lint_results_dir,
models_dir = lint_models_dir,
jdk_home = java_runtime_info.java_home,
Expand All @@ -599,6 +603,7 @@ def _lint_aspect_impl(target, ctx):
outputs = [
lint_results_dir,
lint_result_xml_file,
lint_result_junit_xml_file,
lint_updated_baseline_file,
lint_result_code_file,
],
Expand All @@ -612,6 +617,7 @@ def _lint_aspect_impl(target, ctx):
partial_results_dir = lint_partial_results_dir,
models_dir = lint_models_dir,
lint_result_xml = lint_result_xml_file,
lint_junit_xml = lint_result_junit_xml_file,
result_code = lint_result_code_file,
updated_baseline = lint_updated_baseline_file,
)
Expand All @@ -623,6 +629,7 @@ def _lint_aspect_impl(target, ctx):
enabled = enabled,
partial_results_dir = None,
lint_result_xml = None,
lint_junit_xml = None,
result_code = None,
updated_baseline = None,
)
Expand Down
15 changes: 13 additions & 2 deletions rules/android/lint/lint_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def _lint_test_impl(ctx):

executable = ctx.actions.declare_file("%s_lint.sh" % target.label.name)

lint_junit_xml_file = lint_info.lint_junit_xml
lint_result_xml_file = ctx.outputs.lint_result
lint_result_code = lint_info.result_code
if lint_info.enabled:
Expand All @@ -15,8 +16,12 @@ def _lint_test_impl(ctx):
output = lint_result_xml_file,
)
else:
lint_result_code = ctx.actions.declare_file("%s_result_code" % target.label.name)
lint_junit_xml_file = ctx.actions.declare_file("%s_junit.xml" % target.label.name)
ctx.actions.write(output = lint_junit_xml_file, content = "")

ctx.actions.write(output = lint_result_xml_file, content = "")

lint_result_code = ctx.actions.declare_file("%s_result_code" % target.label.name)
ctx.actions.write(output = lint_result_code, content = "0")

ctx.actions.write(
Expand All @@ -25,17 +30,23 @@ def _lint_test_impl(ctx):
content = """
#!/bin/bash
cat {lint_result_xml_file}
cp {lint_junit_xml_file} $XML_OUTPUT_FILE
exit $(cat {lint_result_code})
""".format(
lint_result_xml_file = lint_result_xml_file.short_path,
lint_junit_xml_file = lint_junit_xml_file.short_path,
lint_result_code = lint_result_code.short_path,
),
)

return [
DefaultInfo(
executable = executable,
runfiles = ctx.runfiles(files = [lint_result_xml_file, lint_result_code]),
runfiles = ctx.runfiles(files = [
lint_junit_xml_file,
lint_result_xml_file,
lint_result_code,
]),
files = depset([
ctx.outputs.lint_result,
]),
Expand Down
1 change: 1 addition & 0 deletions rules/android/lint/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ AndroidLintNodeInfo = provider(
models_dir = "Lint models directory",
updated_baseline = "The updated baseline XML",
lint_result_xml = "The lint results XML file",
lint_junit_xml = "Lint result formatted in Junit format",
),
)

Expand Down
2 changes: 0 additions & 2 deletions tools/cli_utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ kotlin_library(
visibility = [
"//visibility:public",
],
deps = [
],
)
9 changes: 9 additions & 0 deletions tools/lint/src/main/java/com/grab/lint/Document.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.grab.lint

import org.w3c.dom.Element
import org.w3c.dom.NodeList

fun NodeList.elements() = (0 until length).map { item(it) as Element }

operator fun Element.get(name: String): String = getAttribute(name)
operator fun Element.set(name: String, value: String) = setAttribute(name, value)
22 changes: 0 additions & 22 deletions tools/lint/src/main/java/com/grab/lint/File.kt

This file was deleted.

1 change: 0 additions & 1 deletion tools/lint/src/main/java/com/grab/lint/LintDependency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ data class LintDependency(
companion object {
fun from(encodedString: String): LintDependency {
val (name, android, library, partialResultsDir, modelsDir) = encodedString.split("^")
// val partialResults = resolveSymlinks(File(partialResultsDir), workingDir)
return LintDependency(
name = name,
android = android.toBoolean(),
Expand Down
27 changes: 19 additions & 8 deletions tools/lint/src/main/java/com/grab/lint/LintReportCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.ajalt.clikt.parameters.options.required
import java.io.File
import java.nio.file.Path
import kotlin.io.path.pathString
import kotlin.system.measureTimeMillis
import com.android.tools.lint.Main as LintCli

class LintReportCommand : LintBaseCommand() {
Expand All @@ -17,12 +18,18 @@ class LintReportCommand : LintBaseCommand() {
help = "The lint baseline file"
).convert { File(it) }.required()

private val outputXml by option(
private val lintResultXml by option(
"-o",
"--output-xml",
help = "Lint output xml"
).convert { File(it) }.required()

private val outputJunitXml by option(
"-oj",
"--output-junit-xml",
help = "Lint output in Junit format"
).convert { File(it) }.required()

private val resultCode by option(
"-rc",
"--result-code",
Expand All @@ -46,13 +53,18 @@ class LintReportCommand : LintBaseCommand() {
projectXml: File,
tmpBaseline: File,
) {
val newBaseline = runLint(workingDir, projectXml, tmpBaseline)
newBaseline.copyTo(updatedBaseline)
val elapsed = measureTimeMillis {
runLint(workingDir, projectXml, tmpBaseline, lintResultXml)
}
tmpBaseline.copyTo(updatedBaseline)
LintResults(
name = name,
lintResultsFile = lintResultXml,
elapsed = elapsed,
resultCodeFile = resultCode,
lintResultsFile = outputXml,
failOnWarnings = failOnWarnings,
outputJunitXml = outputJunitXml,
failOnInformation = failOnInformation,
failOnWarnings = failOnWarnings,
).process()
}

Expand All @@ -62,17 +74,16 @@ class LintReportCommand : LintBaseCommand() {

override val createProjectXml: Boolean = false

private fun runLint(workingDir: Path, projectXml: File, tmpBaseline: File): File {
private fun runLint(workingDir: Path, projectXml: File, tmpBaseline: File, lintResultXml: File) {
val cliArgs = (defaultLintOptions + listOf(
"--project", projectXml.toString(),
"--xml", outputXml.toString(),
"--xml", lintResultXml.toString(),
"--baseline", tmpBaseline.absolutePath,
"--path-variables", pathVariables,
"--cache-dir", workingDir.resolve("cache").pathString,
"--update-baseline", // Always update the baseline, so we can copy later if needed
"--report-only" // Only do reporting
)).toTypedArray()
LintCli().run(cliArgs)
return tmpBaseline
}
}
Loading

0 comments on commit 40c99d5

Please sign in to comment.