Skip to content

Commit

Permalink
Move more sources around and add back the tests that existed before r…
Browse files Browse the repository at this point in the history
…estructuring
  • Loading branch information
Bencodes committed Oct 21, 2023
1 parent 8adeabe commit 97e5413
Show file tree
Hide file tree
Showing 34 changed files with 432 additions and 87 deletions.
3 changes: 3 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "rules_android_lint_deps",
artifacts = [
# Testing
"org.assertj:assertj-core:3.24.2",
"junit:junit:4.13.2",
# Worker Dependencies
# TODO(bencodes) Remove these and use the worker impl. that Bazel defines internally
"com.squareup.moshi:moshi:1.15.0",
Expand Down
14 changes: 7 additions & 7 deletions rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ bzl_library(
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
deps = [
"//rules/private:collect_aar_outputs_aspect",
":collect_aar_outputs_aspect",
"@bazel_skylib//lib:dicts",
],
)

bzl_library(
name = "collect_aar_outputs_aspect",
srcs = ["collect_aar_outputs_aspect.bzl"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "extensions",
srcs = ["extensions.bzl"],
Expand All @@ -52,9 +58,3 @@ bzl_library(
srcs = ["toolchain.bzl"],
visibility = ["//visibility:public"],
)

py_binary(
name = "test_runner_executable",
srcs = ["test_runner_executable.py"],
visibility = ["//visibility:public"],
)
7 changes: 0 additions & 7 deletions rules/collect_aar_outputs_aspect.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
"""Aspect that collects the AAR outputs.
Lint needs access to the raw archived aar outputs. This aspect collects them all from the dependences,
exports, and associates of the target. It then wraps them up in an AndroidLintAARInfo provider so that
Lint targets have access to them.
"""

AndroidLintAARInfo = provider(
"A provider to collect all aars from transitive dependencies",
fields = {
Expand Down
6 changes: 3 additions & 3 deletions rules/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load(
_dicts = "dicts",
)
load(
"//rules/private:collect_aar_outputs_aspect.bzl",
"//rules:collect_aar_outputs_aspect.bzl",
_AndroidLintAARInfo = "AndroidLintAARInfo",
_collect_aar_outputs_aspect = "collect_aar_outputs_aspect",
)
Expand All @@ -27,12 +27,12 @@ _AUTOMATIC_EXEC_GROUPS_ENABLED = dict(

_ATTRS = dict(
_lint_wrapper = attr.label(
default = "//rules/private/cli:lint_wrapper",
default = "//src/cli:cli",
executable = True,
cfg = "exec",
),
_test_runner_executable = attr.label(
default = "//rules:test_runner_executable",
default = "//src:test_runner_executable",
executable = True,
cfg = "exec",
),
Expand Down
7 changes: 0 additions & 7 deletions rules/private/BUILD.bazel

This file was deleted.

34 changes: 0 additions & 34 deletions rules/private/collect_aar_outputs_aspect.bzl

This file was deleted.

5 changes: 5 additions & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
py_binary(
name = "test_runner_executable",
srcs = ["test_runner_executable.py"],
visibility = ["//visibility:public"],
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 15 additions & 15 deletions rules/private/cli/BUILD → src/cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:lint.bzl", "ktlint_fix", "ktlint_test")

java_binary(
name = "lint_wrapper",
name = "cli",
main_class = "com.rules.android.lint.cli.AndroidLintAction",
visibility = ["//visibility:public"],
runtime_deps = [":lint"],
)

kt_jvm_library(
name = "lint",
srcs = glob(["*.kt"]),
visibility = ["//visibility:public"],
deps = [
"//src/worker",
"@rules_android_lint_deps//:com_android_tools_lint_lint",
"@rules_android_lint_deps//:com_android_tools_lint_lint_api",
"@rules_android_lint_deps//:com_android_tools_lint_lint_checks",
"@rules_android_lint_deps//:com_android_tools_lint_lint_model",
"@rules_android_lint_deps//:com_xenomachina_kotlin_argparser",
],
)

ktlint_test(
name = "lint_ktlint_test",
srcs = glob(["*.kt"]),
Expand All @@ -22,17 +36,3 @@ ktlint_fix(
config = "//:editorconfig",
visibility = ["//visibility:private"],
)

kt_jvm_library(
name = "lint",
srcs = glob(["*.kt"]),
visibility = ["//visibility:public"],
deps = [
"//rules/private/worker",
"@rules_android_lint_deps//:com_android_tools_lint_lint",
"@rules_android_lint_deps//:com_android_tools_lint_lint_api",
"@rules_android_lint_deps//:com_android_tools_lint_lint_checks",
"@rules_android_lint_deps//:com_android_tools_lint_lint_model",
"@rules_android_lint_deps//:com_xenomachina_kotlin_argparser",
],
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_suite(
name = "tests",
tests = [
":versions_test",
"//tests/private/cli:tests",
"//tests/private/worker:tests",
"//tests/src/cli:tests",
"//tests/src/worker:tests",
],
)
6 changes: 0 additions & 6 deletions tests/private/cli/BUILD

This file was deleted.

6 changes: 0 additions & 6 deletions tests/private/worker/BUILD

This file was deleted.

76 changes: 76 additions & 0 deletions tests/src/cli/AndroidLintActionArgsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.rules.android.lint.cli

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.nio.file.Paths

@RunWith(JUnit4::class)
class AndroidLintActionArgsTest {

@Test
fun `does parse all arguments`() {
val parseArgs = AndroidLintActionArgs.parseArgs(
args = listOf(
"--module-name",
"test_module_name",
"--label",
"test",
"--src",
"path/to/Foo.kt",
"--output",
"output.jar",
"--project-config-output",
"project.xml",
"--resource",
"path/to/resource/strings.xml",
"--android-manifest",
"AndroidManifest.xml",
"--baseline-file",
"lib_lint_baseline.xml",
"--config-file",
"lint_config.xml",
"--custom-rule",
"custom_rule.jar",
"--classpath",
"classpath.jar",
"--classpath",
"classpath.aar",
"--autofix",
"--regenerate-baseline-files",
"--warnings-as-errors",
"--enable-check",
"custom-check",
"--disable-check",
"custom-disabled-check",
"--compile-sdk-version",
"1.6",
"--java-language-level",
"1.7",
"--kotlin-language-level",
"1.8",
),
)

assertThat(parseArgs.moduleName).isEqualTo("test_module_name")
assertThat(parseArgs.label).isEqualTo("test")
assertThat(parseArgs.srcs).containsExactly(Paths.get("path/to/Foo.kt"))
assertThat(parseArgs.output).isEqualTo(Paths.get("output.jar"))
assertThat(parseArgs.projectConfigOutput).isEqualTo(Paths.get("project.xml"))
assertThat(parseArgs.resources).containsExactly(Paths.get("path/to/resource/strings.xml"))
assertThat(parseArgs.baselineFile).isEqualTo(Paths.get("lib_lint_baseline.xml"))
assertThat(parseArgs.config).isEqualTo(Paths.get("lint_config.xml"))
assertThat(parseArgs.customChecks).containsExactly(Paths.get("custom_rule.jar"))
assertThat(parseArgs.classpath)
.containsExactly(Paths.get("classpath.jar"), Paths.get("classpath.aar"))
assertThat(parseArgs.autofix).isTrue
assertThat(parseArgs.regenerateBaselineFile).isTrue
assertThat(parseArgs.warningsAsErrors).isTrue
assertThat(parseArgs.enableChecks).containsExactly("custom-check")
assertThat(parseArgs.disableChecks).containsExactly("custom-disabled-check")
assertThat(parseArgs.compileSdkVersion).isEqualTo("1.6")
assertThat(parseArgs.javaLanguageLevel).isEqualTo("1.7")
assertThat(parseArgs.kotlinLanguageLevel).isEqualTo("1.8")
}
}
13 changes: 13 additions & 0 deletions tests/src/cli/AndroidLintActionTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.rules.android.lint.cli

import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class AndroidLintActionTest {
@Test
fun `empty test case`() {
// TODO(bencodes) Add some tests for AndroidLintAction
}
}
56 changes: 56 additions & 0 deletions tests/src/cli/AndroidLintProjectTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.rules.android.lint.cli

import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.nio.file.Path

@RunWith(JUnit4::class)
class AndroidLintProjectTest {

@Rule
@JvmField
var tmpDirectory = TemporaryFolder()

private fun TemporaryFolder.newPath(name: String): Path = this.newFile(name).toPath()

@Test
fun `test asXMLString does produce correct project file content`() {
assertThat(
createProjectXMLString(
moduleName = "test_module_name",
srcs = listOf(tmpDirectory.newPath("Foo.kt")),
resources = listOf(tmpDirectory.newPath("foo.xml")),
androidManifest = tmpDirectory.newPath("AndroidManifest.xml"),
classpathJars = listOf(tmpDirectory.newPath("Foo.jar")),
classpathAars = listOf(tmpDirectory.newPath("Foo.aar")),
classpathExtractedAarDirectories = listOf(
Pair(
tmpDirectory.newPath("Bar.aar"),
tmpDirectory.newFolder("tmp/unpacked_aars/bar/").toPath(),
),
),
customLintChecks = listOf(tmpDirectory.newPath("tmp/unpacked_aars/bar/lint.jar")),
),
).isEqualTo(
"""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<module android="true" name="test_module_name">
<src file="{root}/Foo.kt"/>
<resource file="{root}/foo.xml"/>
<manifest file="{root}/AndroidManifest.xml"/>
<classpath jar="{root}/Foo.jar"/>
<aar file="{root}/Foo.aar"/>
<aar extracted="{root}/tmp/unpacked_aars/bar" file="{root}/Bar.aar"/>
</module>
<lint-checks jar="{root}/tmp/unpacked_aars/bar/lint.jar"/>
</project>
""".trimIndent().replace("{root}", tmpDirectory.root.absolutePath),
)
}
}
58 changes: 58 additions & 0 deletions tests/src/cli/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_test")
load("@rules_kotlin//kotlin:lint.bzl", "ktlint_fix", "ktlint_test")

kt_jvm_test(
name = "AndroidLintActionArgsTest",
srcs = ["AndroidLintActionArgsTest.kt"],
associates = ["//src/cli:lint"],
test_class = "com.rules.android.lint.cli.AndroidLintActionArgsTest",
deps = [
"@rules_android_lint_deps//:junit_junit",
"@rules_android_lint_deps//:org_assertj_assertj_core",
],
)

kt_jvm_test(
name = "AndroidLintActionTest",
srcs = ["AndroidLintActionTest.kt"],
associates = ["//src/cli:lint"],
test_class = "com.rules.android.lint.cli.AndroidLintActionTest",
deps = [
"@rules_android_lint_deps//:junit_junit",
"@rules_android_lint_deps//:org_assertj_assertj_core",
],
)

kt_jvm_test(
name = "AndroidLintProjectTest",
srcs = ["AndroidLintProjectTest.kt"],
associates = ["//src/cli:lint"],
test_class = "com.rules.android.lint.cli.AndroidLintProjectTest",
deps = [
"@rules_android_lint_deps//:junit_junit",
"@rules_android_lint_deps//:org_assertj_assertj_core",
],
)

ktlint_test(
name = "lint_ktlint_test",
srcs = glob(["*.kt"]),
config = "//:editorconfig",
visibility = ["//visibility:private"],
)

ktlint_fix(
name = "lint_ktlint_fix",
srcs = glob(["*.kt"]),
config = "//:editorconfig",
visibility = ["//visibility:private"],
)

test_suite(
name = "tests",
tests = [
":AndroidLintActionArgsTest",
":AndroidLintActionTest",
":AndroidLintProjectTest",
],
)
Loading

0 comments on commit 97e5413

Please sign in to comment.