Skip to content

Commit 7ceff16

Browse files
committed
Surface Detekt stderr output for test targets
1 parent db2e0a6 commit 7ceff16

5 files changed

Lines changed: 46 additions & 14 deletions

File tree

detekt/defs.bzl

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

277+
stderr_output = ctx.actions.declare_file("{}_stderr.txt".format(ctx.label.name))
278+
run_files.append(stderr_output)
279+
action_outputs.append(stderr_output)
280+
detekt_arguments.add("--stderr-output", "{}".format(stderr_output.path))
281+
277282
ctx.actions.run(
278283
mnemonic = "Detekt",
279284
progress_message = "Running Detekt for {}".format(str(ctx.label)),
@@ -298,13 +303,13 @@ def _impl(
298303
#!/bin/bash
299304
set -euo pipefail
300305
exit_code=$(cat {execution_result})
301-
report=$(cat {text_report})
302-
if [ ! -z "$report" ]; then
303-
echo "$report"
306+
stderr_content=$(cat {stderr_output})
307+
if [ ! -z "$stderr_content" ]; then
308+
echo "$stderr_content"
304309
fi
305310
{baseline_script}
306311
exit "$exit_code"
307-
""".format(execution_result = execution_result.short_path, text_report = txt_report.short_path, baseline_script = baseline_script),
312+
""".format(execution_result = execution_result.short_path, stderr_output = stderr_output.short_path, baseline_script = baseline_script),
308313
is_executable = True,
309314
)
310315

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
@@ -69,6 +69,13 @@ public ExecutableResult execute(String[] args) {
6969
ExecutionUtils.writeExecutionResultToFile(result.statusCode(), executionResultOutputPath);
7070

7171
if (ExecutionUtils.shouldRunAsTestTarget(detektWrapperArguments)) {
72+
// TODO: Cleanup the logic so the same output is written to the console for both build and test commands
73+
String stderrOutputPathStr =
74+
ExecutionUtils.getValueForArgumentName(detektWrapperArguments, "--stderr-output");
75+
if (stderrOutputPathStr != null) {
76+
Path stderrOutputPath = Paths.get(stderrOutputPathStr);
77+
ExecutionUtils.writeStderrToFile(result.output(), stderrOutputPath);
78+
}
7279
result = new ExecutableResult.Success();
7380
}
7481
return result;

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)