From c7e8ab9b560bccf4c10563eaa99dad57d9b674c3 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Thu, 14 Feb 2019 15:10:22 +0300 Subject: [PATCH 1/5] New command: version, closes #314 --- .../cli/ctl/commands/version_command.ex | 45 ++++++++++++++++++ deps/rabbitmq_cli/lib/rabbitmqctl.ex | 5 ++ .../test/ctl/version_command_test.exs | 47 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex create mode 100644 deps/rabbitmq_cli/test/ctl/version_command_test.exs diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex new file mode 100644 index 0000000000..8e25419cac --- /dev/null +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/version_command.ex @@ -0,0 +1,45 @@ +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved. + +defmodule RabbitMQ.CLI.Ctl.Commands.VersionCommand do + alias RabbitMQ.CLI.Core.Validators + + @behaviour RabbitMQ.CLI.CommandBehaviour + + def scopes(), do: [:ctl, :diagnostics, :plugins] + + use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments + + def validate_execution_environment([] = args, opts) do + Validators.rabbit_is_loaded(args, opts) + end + + def run([], %{formatter: "json"}) do + {:ok, %{version: to_string(:rabbit_misc.version())}} + end + def run([], %{formatter: "csv"}) do + row = [version: to_string(:rabbit_misc.version())] + {:ok, [row]} + end + def run([], _opts) do + {:ok, to_string(:rabbit_misc.version())} + end + use RabbitMQ.CLI.DefaultOutput + + def usage, do: "version" + + def banner(_, _), do: nil +end diff --git a/deps/rabbitmq_cli/lib/rabbitmqctl.ex b/deps/rabbitmq_cli/lib/rabbitmqctl.ex index 416ea44ff7..300428e73e 100644 --- a/deps/rabbitmq_cli/lib/rabbitmqctl.ex +++ b/deps/rabbitmq_cli/lib/rabbitmqctl.ex @@ -62,6 +62,11 @@ defmodule RabbitMQCtl do {:ok, ExitCodes.exit_ok(), HelpCommand.all_usage(parsed_options)}; end + def exec_command(["--version"] = _unparsed_command, opts) do + # rewrite `--version` as `version` + exec_command(["version"], opts) + end + def exec_command(unparsed_command, output_fun) do {command, command_name, arguments, parsed_options, invalid} = Parser.parse(unparsed_command) diff --git a/deps/rabbitmq_cli/test/ctl/version_command_test.exs b/deps/rabbitmq_cli/test/ctl/version_command_test.exs new file mode 100644 index 0000000000..ba1df78b7b --- /dev/null +++ b/deps/rabbitmq_cli/test/ctl/version_command_test.exs @@ -0,0 +1,47 @@ +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved. + + +defmodule VersionCommandTest do + use ExUnit.Case + import TestHelper + + @command RabbitMQ.CLI.Ctl.Commands.VersionCommand + + setup_all do + Application.load(:rabbit) + end + + setup context do + {:ok, opts: %{}} + end + + test "merge_defaults: merges no defaults" do + assert @command.merge_defaults([], %{}) == {[], %{}} + end + + test "validate: treats positional arguments as a failure" do + assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args} + end + + test "validate: treats empty positional arguments and default switches as a success" do + assert @command.validate([], %{}) == :ok + end + + test "run: returns Erlang/OTP version on the local node", context do + {:ok, version} = @command.run([], context[:opts]) + assert Regex.match?(~r/^\d+\.\d+\.\d+/, version) + end +end From b7579f9027a463024b4ec9975c72f64e92ccd204 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Thu, 14 Feb 2019 15:48:36 +0300 Subject: [PATCH 2/5] Squash a couple of test warnings --- deps/rabbitmq_cli/test/ctl/version_command_test.exs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/deps/rabbitmq_cli/test/ctl/version_command_test.exs b/deps/rabbitmq_cli/test/ctl/version_command_test.exs index ba1df78b7b..f60a692b1a 100644 --- a/deps/rabbitmq_cli/test/ctl/version_command_test.exs +++ b/deps/rabbitmq_cli/test/ctl/version_command_test.exs @@ -16,7 +16,6 @@ defmodule VersionCommandTest do use ExUnit.Case - import TestHelper @command RabbitMQ.CLI.Ctl.Commands.VersionCommand @@ -40,8 +39,8 @@ defmodule VersionCommandTest do assert @command.validate([], %{}) == :ok end - test "run: returns Erlang/OTP version on the local node", context do - {:ok, version} = @command.run([], context[:opts]) + test "run: returns Erlang/OTP version on the local node" do + {:ok, version} = @command.run([], %{}) assert Regex.match?(~r/^\d+\.\d+\.\d+/, version) end end From b4b4bb434561b521dddf02441ebe60e42d30299e Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Thu, 14 Feb 2019 16:17:35 +0300 Subject: [PATCH 3/5] Return :ok from setup_all --- deps/rabbitmq_cli/test/ctl/version_command_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/deps/rabbitmq_cli/test/ctl/version_command_test.exs b/deps/rabbitmq_cli/test/ctl/version_command_test.exs index f60a692b1a..c6f7549fc2 100644 --- a/deps/rabbitmq_cli/test/ctl/version_command_test.exs +++ b/deps/rabbitmq_cli/test/ctl/version_command_test.exs @@ -21,6 +21,7 @@ defmodule VersionCommandTest do setup_all do Application.load(:rabbit) + :ok end setup context do From 99eaec4a8e927b8eb46c98e01ab80913f4a044ab Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Thu, 14 Feb 2019 16:34:55 +0300 Subject: [PATCH 4/5] version command test: remove an environment-specific test `rabbit_misc:version/0` requires rabbit.app to be on code path and loaded which is not always the case on the test node. The command is so simple that this doesn't introduce enough risk to justify spending more time on it. --- .../rabbitmq_cli/test/ctl/version_command_test.exs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/deps/rabbitmq_cli/test/ctl/version_command_test.exs b/deps/rabbitmq_cli/test/ctl/version_command_test.exs index c6f7549fc2..ca8e0719e4 100644 --- a/deps/rabbitmq_cli/test/ctl/version_command_test.exs +++ b/deps/rabbitmq_cli/test/ctl/version_command_test.exs @@ -19,15 +19,6 @@ defmodule VersionCommandTest do @command RabbitMQ.CLI.Ctl.Commands.VersionCommand - setup_all do - Application.load(:rabbit) - :ok - end - - setup context do - {:ok, opts: %{}} - end - test "merge_defaults: merges no defaults" do assert @command.merge_defaults([], %{}) == {[], %{}} end @@ -39,9 +30,4 @@ defmodule VersionCommandTest do test "validate: treats empty positional arguments and default switches as a success" do assert @command.validate([], %{}) == :ok end - - test "run: returns Erlang/OTP version on the local node" do - {:ok, version} = @command.run([], %{}) - assert Regex.match?(~r/^\d+\.\d+\.\d+/, version) - end end From 667373119eda9d5911df788d7b1bd9c5afada966 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Wed, 20 Feb 2019 12:09:33 +0300 Subject: [PATCH 5/5] Feature flag tests belong under test/ctl --- deps/rabbitmq_cli/test/{ => ctl}/enable_feature_flag_test.exs | 0 .../test/{ => ctl}/list_feature_flags_command_test.exs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename deps/rabbitmq_cli/test/{ => ctl}/enable_feature_flag_test.exs (100%) rename deps/rabbitmq_cli/test/{ => ctl}/list_feature_flags_command_test.exs (100%) diff --git a/deps/rabbitmq_cli/test/enable_feature_flag_test.exs b/deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs similarity index 100% rename from deps/rabbitmq_cli/test/enable_feature_flag_test.exs rename to deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs diff --git a/deps/rabbitmq_cli/test/list_feature_flags_command_test.exs b/deps/rabbitmq_cli/test/ctl/list_feature_flags_command_test.exs similarity index 100% rename from deps/rabbitmq_cli/test/list_feature_flags_command_test.exs rename to deps/rabbitmq_cli/test/ctl/list_feature_flags_command_test.exs