Skip to content

Commit 6aaf031

Browse files
authored
Checkstyle works with generated config files (#227)
Fixes #226
1 parent e41a109 commit 6aaf031

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

java/private/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ bzl_library(
1313
name = "checkstyle",
1414
srcs = ["checkstyle.bzl"],
1515
visibility = ["//java:__subpackages__"],
16-
deps = [":checkstyle_config"],
16+
deps = [
17+
":checkstyle_config",
18+
"@bazel_skylib//lib:paths",
19+
],
1720
)
1821

1922
bzl_library(

java/private/checkstyle.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load(":checkstyle_config.bzl", "CheckStyleInfo")
22
load("@apple_rules_lint//lint:defs.bzl", "LinterInfo")
3+
load("@bazel_skylib//lib:paths.bzl", "paths")
34

45
"""
56
Checkstyle rule implementation
@@ -10,12 +11,15 @@ def _checkstyle_impl(ctx):
1011
config = info.config_file
1112
output_format = info.output_format
1213

14+
config_dir = paths.dirname(config.short_path)
15+
maybe_cd_config_dir = ["cd {}".format(config_dir)] if config_dir else []
16+
1317
script = "\n".join([
1418
"#!/usr/bin/env bash",
1519
"set -o pipefail",
1620
"set -e",
1721
"OLDPWD=$PWD",
18-
"cd {config_dir}".format(config_dir = config.dirname),
22+
] + maybe_cd_config_dir + [
1923
"$OLDPWD/{lib} -f {output_format} -c {config} {srcs} |sed s:$OLDPWD/::g".format(
2024
lib = info.checkstyle.short_path,
2125
output_format = output_format,

java/test/com/github/bazel_contrib/contrib_rules_jvm/checkstyle/BUILD.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ checkstyle_config(
1212
data = ["suppressions.xml"],
1313
)
1414

15+
checkstyle_config(
16+
name = "copied_checkstyle_with_suppressions",
17+
config_file = "copied_checkstyle_with_suppressions.xml",
18+
data = ["suppressions.xml"],
19+
)
20+
1521
checkstyle_test(
1622
name = "this_should_fail_missing_file",
1723
srcs = ["Sample.java"],
@@ -32,8 +38,21 @@ checkstyle_test(
3238
config = ":checkstyle_with_suppressions",
3339
)
3440

41+
checkstyle_test(
42+
name = "this_should_also_pass",
43+
srcs = ["Sample.java"],
44+
config = ":copied_checkstyle_with_suppressions",
45+
)
46+
3547
java_library(
3648
name = "checkstyle",
3749
testonly = True,
3850
srcs = ["Sample.java"],
3951
)
52+
53+
genrule(
54+
name = "copy_config_file",
55+
srcs = ["checkstyle_with_suppressions.xml"],
56+
outs = ["copied_checkstyle_with_suppressions.xml"],
57+
cmd = "cp $< $@",
58+
)

0 commit comments

Comments
 (0)