Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.4.0] Fix valid json when using jsonproto output in queries with new --ouput=streamed_jsonproto implementation. #19226

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static ImmutableList<OutputFormatter> getDefaultFormatters() {
new PackageOutputFormatter(),
new LocationOutputFormatter(),
new GraphOutputFormatter(),
new JSONProtoOutputFormatter(),
new XmlOutputFormatter(),
new ProtoOutputFormatter(),
new StreamedJSONProtoOutputFormatter(),
new StreamedProtoOutputFormatter());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public OrderOutputConverter() {
effectTags = {OptionEffectTag.TERMINAL_OUTPUT},
help =
"The format in which the query results should be printed. Allowed values for query are:"
+ " build, graph, jsonproto, label, label_kind, location, maxrank, minrank, package,"
+ " proto, xml.")
+ " build, graph, streamed_jsonproto, label, label_kind, location, maxrank, minrank,"
+ " package, proto, xml.")
public String outputFormat;

@Option(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import java.nio.charset.StandardCharsets;

/**
* An output formatter that outputs a protocol buffer json representation of a query result and
* outputs the json to the output print stream.
* An output formatter that prints a list of targets according to ndjson spec to the output print
* stream.
*/
public class JSONProtoOutputFormatter extends ProtoOutputFormatter {
public class StreamedJSONProtoOutputFormatter extends ProtoOutputFormatter {
@Override
public String getName() {
return "jsonproto";
return "streamed_jsonproto";
}

private final JsonFormat.Printer jsonPrinter = JsonFormat.printer();
Expand All @@ -42,7 +42,11 @@ public void processOutput(Iterable<Target> partialResult)
throws IOException, InterruptedException {
for (Target target : partialResult) {
out.write(
jsonPrinter.print(toTargetProtoBuffer(target)).getBytes(StandardCharsets.UTF_8));
jsonPrinter
.omittingInsignificantWhitespace()
.print(toTargetProtoBuffer(target))
.getBytes(StandardCharsets.UTF_8));
out.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
}
}
};
Expand Down
38 changes: 30 additions & 8 deletions src/test/shell/integration/bazel_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ genquery(name='q',
opts = ["--output=blargh"],)
EOF

local expected_error_msg="in genquery rule //starfruit:q: Invalid output format 'blargh'. Valid values are: label, label_kind, build, minrank, maxrank, package, location, graph, jsonproto, xml, proto"
local expected_error_msg="in genquery rule //starfruit:q: Invalid output format 'blargh'. Valid values are: label, label_kind, build, minrank, maxrank, package, location, graph, xml, proto, streamed_jsonproto, "
bazel build //starfruit:q >& $TEST_log && fail "Expected failure"
expect_log "$expected_error_msg"
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ EOF
expect_log "//${package}:hint"
}

function test_basic_query_jsonproto() {
function test_basic_query_streamed_jsonproto() {
local pkg="${FUNCNAME[0]}"
mkdir -p "$pkg" || fail "mkdir -p $pkg"
cat > "$pkg/BUILD" <<'EOF'
Expand All @@ -1074,17 +1074,39 @@ genrule(
outs = ["bar_out.txt"],
cmd = "echo unused > $(OUTS)",
)
genrule(
name = "foo",
srcs = ["dummy.txt"],
outs = ["foo_out.txt"],
cmd = "echo unused > $(OUTS)",
)
EOF
bazel query --output=jsonproto --noimplicit_deps "//$pkg:bar" > output 2> "$TEST_log" \
bazel query --output=streamed_jsonproto --noimplicit_deps "//$pkg/..." > output 2> "$TEST_log" \
|| fail "Expected success"
cat output >> "$TEST_log"

# Verify that the appropriate attributes were included.
assert_contains "\"ruleClass\": \"genrule\"" output
assert_contains "\"name\": \"//$pkg:bar\"" output
assert_contains "\"ruleInput\": \[\"//$pkg:dummy.txt\"\]" output
assert_contains "\"ruleOutput\": \[\"//$pkg:bar_out.txt\"\]" output
assert_contains "echo unused" output

foo_line_number=$(grep -n "foo" output | cut -d':' -f1)
bar_line_number=$(grep -n "bar" output | cut -d':' -f1)

foo_ndjson_line=$(sed -n "${foo_line_number}p" output)
bar_ndjson_line=$(sed -n "${bar_line_number}p" output)

echo "$foo_ndjson_line" > foo_ndjson_file
echo "$bar_ndjson_line" > bar_ndjson_file

assert_contains "\"ruleClass\":\"genrule\"" foo_ndjson_file
assert_contains "\"name\":\"//$pkg:foo\"" foo_ndjson_file
assert_contains "\"ruleInput\":\[\"//$pkg:dummy.txt\"\]" foo_ndjson_file
assert_contains "\"ruleOutput\":\[\"//$pkg:foo_out.txt\"\]" foo_ndjson_file
assert_contains "echo unused" foo_ndjson_file

assert_contains "\"ruleClass\":\"genrule\"" bar_ndjson_file
assert_contains "\"name\":\"//$pkg:bar\"" bar_ndjson_file
assert_contains "\"ruleInput\":\[\"//$pkg:dummy.txt\"\]" bar_ndjson_file
assert_contains "\"ruleOutput\":\[\"//$pkg:bar_out.txt\"\]" bar_ndjson_file
assert_contains "echo unused" bar_ndjson_file
}

run_suite "${PRODUCT_NAME} query tests"