Skip to content

Commit 3280bed

Browse files
authored
Additional JSON CLI formatter fix (#14511)
A list of binaries was incorrectly converted by `unicode:characters_to_binary` into one big binary. Add a function head to match this case. Also, add tests for the values that were not correctly formatted prior to #14101 and #14381
1 parent 41337bb commit 3280bed

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ defmodule RabbitMQ.CLI.Formatters.Json do
9393

9494
defp convert_erlang_strings([]), do: []
9595

96+
defp convert_erlang_strings([b | _rest]=data) when is_binary(b) do
97+
# Assume this is a list of strings already converted
98+
data
99+
end
100+
96101
defp convert_erlang_strings(data) when is_list(data) do
97102
try do
98103
case :unicode.characters_to_binary(data, :utf8) do

deps/rabbitmq_cli/test/json_formatting_test.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ defmodule JSONFormattingTest do
3838
assert Map.has_key?(doc, "os")
3939
assert Map.has_key?(doc, "pid")
4040
assert Map.has_key?(doc, "rabbitmq_version")
41+
assert Map.has_key?(doc, "active_plugins")
42+
assert Map.has_key?(doc, "config_files")
43+
assert Map.has_key?(doc, "log_files")
44+
45+
active_plugins = doc["active_plugins"]
46+
assert is_list(active_plugins)
47+
assert Enum.all?(active_plugins, &is_binary/1)
48+
49+
config_files = doc["config_files"]
50+
assert is_list(config_files)
51+
assert Enum.all?(config_files, &is_binary/1)
52+
53+
log_files = doc["log_files"]
54+
assert is_list(log_files)
55+
assert Enum.all?(log_files, &is_binary/1)
4156

4257
assert doc["alarms"] == []
4358
end
@@ -55,10 +70,35 @@ defmodule JSONFormattingTest do
5570

5671
{:ok, doc} = JSON.decode(output)
5772

73+
assert Map.has_key?(doc, "running_nodes")
74+
running_nodes = doc["running_nodes"]
75+
assert is_list(running_nodes)
76+
assert Enum.all?(running_nodes, &is_binary/1)
77+
5878
assert Enum.member?(doc["disk_nodes"], node)
5979
assert Map.has_key?(doc["listeners"], node)
6080
assert Map.has_key?(doc["versions"], node)
81+
assert Map.has_key?(doc["versions"], node)
6182
assert doc["alarms"] == []
6283
assert doc["partitions"] == %{}
6384
end
85+
86+
test "JSON output of environment" do
87+
set_scope(:ctl)
88+
89+
node = to_string(get_rabbit_hostname())
90+
command = ["environment", "-n", node, "--formatter=json"]
91+
92+
output =
93+
capture_io(:stdio, fn ->
94+
error_check(command, exit_ok())
95+
end)
96+
97+
{:ok, doc} = JSON.decode(output)
98+
assert Map.has_key?(doc, "rabbit")
99+
rabbit = doc["rabbit"]
100+
assert Map.has_key?(rabbit, "data_dir")
101+
data_dir = rabbit["data_dir"]
102+
assert is_binary(data_dir)
103+
end
64104
end

0 commit comments

Comments
 (0)