Skip to content

Commit b6ade69

Browse files
committed
Surface Detekt stderr output for test targets
1 parent 23d0868 commit b6ade69

6 files changed

Lines changed: 49 additions & 17 deletions

File tree

.bazelrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
common --test_tag_filters=-manual
22

3-
# abseil-cpp and protobuf (pulled in by stardoc) use std::visit/std::get with std::variant,
4-
# which requires macOS 10.13+. Set the minimum OS for both target and host/exec tool builds.
5-
# Uncomment the below if running into Stardoc issues on Mac.
3+
# abseil-cpp and protobuf use std::visit/std::get with std::variant, which requires macOS 10.13+.
4+
# Set the minimum OS for both target and host/exec tool builds.
5+
# Uncomment the below if running into issues on Mac.
66
# build --macos_minimum_os=10.13
77
# build --host_macos_minimum_os=10.13
88

detekt/defs.bzl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ def _impl(
280280
action_outputs.append(execution_result)
281281
detekt_arguments.add("--execution-result", "{}".format(execution_result.path))
282282

283+
stderr_output = ctx.actions.declare_file("{}_stderr.txt".format(ctx.label.name))
284+
run_files.append(stderr_output)
285+
action_outputs.append(stderr_output)
286+
detekt_arguments.add("--stderr-output", "{}".format(stderr_output.path))
287+
283288
ctx.actions.run(
284289
mnemonic = "Detekt",
285290
progress_message = "Running Detekt for {}".format(str(ctx.label)),
@@ -304,13 +309,13 @@ def _impl(
304309
#!/bin/bash
305310
set -euo pipefail
306311
exit_code=$(cat {execution_result})
307-
report=$(cat {text_report})
308-
if [ ! -z "$report" ]; then
309-
echo "$report"
312+
stderr_content=$(cat {stderr_output})
313+
if [ ! -z "$stderr_content" ]; then
314+
echo "$stderr_content"
310315
fi
311316
{baseline_script}
312317
exit "$exit_code"
313-
""".format(execution_result = execution_result.short_path, text_report = txt_report.short_path, baseline_script = baseline_script),
318+
""".format(execution_result = execution_result.short_path, stderr_output = stderr_output.short_path, baseline_script = baseline_script),
314319
is_executable = True,
315320
)
316321

detekt/wrapper/src/main/java/io/buildfoundation/bazel/detekt/ExecutionUtils.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public static void writeExecutionResultToFile(Integer exitCode, Path executionRe
4040
}
4141
}
4242

43+
/** Writes the stderr output to a file */
44+
public static void writeStderrToFile(String stderrContent, Path stderrOutputPath) {
45+
try {
46+
Files.write(stderrOutputPath, stderrContent.getBytes(), StandardOpenOption.CREATE);
47+
} catch (IOException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
4352
public static boolean isParamsFile(String argument) {
4453
return argument.startsWith("@");
4554
}
@@ -55,14 +64,14 @@ public static List<String> readArgumentsFromFile(Path filePath) {
5564
}
5665
}
5766

58-
/**
59-
* Retrieves the value associated with the given argument name from the input arguments list.
60-
*
61-
* @param inputArgs List of input arguments.
62-
* @param argName The name of the argument whose value needs to be fetched.
63-
* @return The value associated with the given argument name or null if the argument is not found.
64-
*/
65-
public static String getValueForArgumentName(List<String> inputArgs, String argName) {
67+
/**
68+
* Retrieves the value associated with the given argument name from the input arguments list.
69+
*
70+
* @param inputArgs List of input arguments.
71+
* @param argName The name of the argument whose value needs to be fetched.
72+
* @return The value associated with the given argument name or null if the argument is not found.
73+
*/
74+
public static String getValueForArgumentName(List<String> inputArgs, String argName) {
6675
try {
6776
// Get the index of the argument and return the value at the next index.
6877
int indexOfArgument = inputArgs.indexOf(argName);
@@ -80,7 +89,9 @@ public static String getValueForArgumentName(List<String> inputArgs, String argN
8089
* Sanitizes the Detekt arguments by excluding arguments used solely by the detekt-wrapper
8190
*/
8291
public static List<String> sanitizeDetektArguments(List<String> inputArgs) {
83-
Set<String> excludedArgs = new HashSet<>(Arrays.asList("--execution-result", "--run-as-test-target"));
92+
Set<String> excludedArgs =
93+
new HashSet<>(
94+
Arrays.asList("--execution-result", "--run-as-test-target", "--stderr-output"));
8495
return filterOutArgValuePairs(inputArgs, excludedArgs);
8596
}
8697

detekt/wrapper/src/main/java/io/buildfoundation/bazel/detekt/execute/Executable.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ public ExecutableResult execute(String[] args) {
6868

6969
ExecutionUtils.writeExecutionResultToFile(result.statusCode(), executionResultOutputPath);
7070

71+
String stderrOutputPathStr =
72+
ExecutionUtils.getValueForArgumentName(detektWrapperArguments, "--stderr-output");
73+
if (stderrOutputPathStr != null) {
74+
Path stderrOutputPath = Paths.get(stderrOutputPathStr);
75+
ExecutionUtils.writeStderrToFile(result.output(), stderrOutputPath);
76+
}
77+
7178
if (ExecutionUtils.shouldRunAsTestTarget(detektWrapperArguments)) {
7279
result = new ExecutableResult.Success();
7380
}

tests/analysis/tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def _action_full_contents_test_impl(ctx):
8484
"{{source_dir}}/test_target_full_detekt_report.html",
8585
"{{source_dir}}/test_target_full_detekt_report.xml",
8686
"{{source_dir}}/test_target_full_exit_code.txt",
87+
"{{source_dir}}/test_target_full_stderr.txt",
8788
])
8889

8990
asserts.equals(env, expected_inputs, [file.short_path for file in action.inputs.to_list()])
@@ -143,6 +144,7 @@ def _action_blank_contents_test_impl(ctx):
143144
expected_outputs = _expand_paths(env.ctx, [
144145
"{{source_dir}}/test_target_blank_detekt_report.txt",
145146
"{{source_dir}}/test_target_blank_exit_code.txt",
147+
"{{source_dir}}/test_target_blank_stderr.txt",
146148
])
147149

148150
asserts.equals(env, expected_inputs, [file.short_path for file in action.inputs.to_list()])

tests/integration/BUILD

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
load("//detekt:defs.bzl", "detekt")
1+
load("//detekt:defs.bzl", "detekt", "detekt_create_baseline")
22

33
filegroup(
44
name = "detekt_config_lenient",
55
srcs = ["detekt_config_lenient.yml"],
66
tags = ["manual"],
77
)
88

9+
detekt_create_baseline(
10+
name = "create_integration_test_detekt_baseline",
11+
srcs = glob(["src/main/kotlin/**/*.kt"]),
12+
baseline = "detekt_baseline.xml",
13+
tags = ["manual"],
14+
)
15+
916
detekt(
1017
name = "detekt_without_config",
1118
srcs = glob(["src/main/kotlin/**/*.kt"]),

0 commit comments

Comments
 (0)