From 175700ad769454c0988822cf0be4746774d8287b Mon Sep 17 00:00:00 2001 From: Nerf-qh Date: Tue, 14 Mar 2023 11:24:32 +0100 Subject: [PATCH 1/2] fix: set correct running services value --- lib/neptuno/cli/activate.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/neptuno/cli/activate.rb b/lib/neptuno/cli/activate.rb index 4523cf6..b646d3d 100644 --- a/lib/neptuno/cli/activate.rb +++ b/lib/neptuno/cli/activate.rb @@ -17,7 +17,7 @@ def call(services: [], **options) count = 0 command_services_to("activate to services", all: options.fetch(:all), services_as_args: services) do |services| system("cd #{neptuno_path} && docker compose up -d #{services.join(" ")}") - running_services = ::Neptuno::CLI::List.new.running_services.first.keys + running_services = ::Neptuno::CLI::List.new.running_services # array of services (string) running_services.sort.each do |service| spinners[service] ||= multi_spinner.register("[:spinner] :state #{service}") spinners[service].update(state: "- ") @@ -28,7 +28,7 @@ def call(services: [], **options) running_services.sort.each do |service| service_ps = ps.find { |s| s =~ /#{project}[-_]#{service}[-_]\d\s/ } - + status = nil status = :dead if service_ps.to_s.include?("exited") status = :starting if service_ps.to_s.include?("starting") status = :unhealthy if service_ps.to_s.include?("(unhealthy") From 8e3eca6a08f45d74a77c2cc95dac0643f52db2a9 Mon Sep 17 00:00:00 2001 From: Nerf-qh Date: Tue, 14 Mar 2023 15:50:52 +0100 Subject: [PATCH 2/2] Add tests --- test/ude/cli/activate_test.rb | 70 +++++++++++++++++++++++++++++++ test/ude/cli/list_test.rb | 23 ++++++++++ test/ude/support/cli_ovverides.rb | 17 ++++++++ 3 files changed, 110 insertions(+) create mode 100644 test/ude/cli/activate_test.rb create mode 100644 test/ude/cli/list_test.rb create mode 100644 test/ude/support/cli_ovverides.rb diff --git a/test/ude/cli/activate_test.rb b/test/ude/cli/activate_test.rb new file mode 100644 index 0000000..d80ecc5 --- /dev/null +++ b/test/ude/cli/activate_test.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'test_helper' +require './test/ude/support/cli_ovverides' + +describe Neptuno::CLI::Activate do + describe '#call' do + project_folder = '/app/test-neptuno' + before do + @activate = Neptuno::CLI::Activate.new + @options = { force: false, all: false } + + @cli_list_mock = Minitest::Mock.new + @system_mock = MiniTest::Mock.new + @multi_spinner_mock = MiniTest::Mock.new + @spinner_inst_mock_foo = MiniTest::Mock.new + @last_spinner_mock = MiniTest::Mock.new + end + + it 'call docker compose up' do + services = %w[foo] + + @cli_list_mock.expect :running_services, services + @system_mock.expect :call, nil, ['cd /app/test-neptuno && docker compose up -d foo'] + + @multi_spinner_mock.expect(:register, @spinner_inst_mock_foo, [String]) + @spinner_inst_mock_foo.expect(:update, nil) do |args| + assert_equal ({ state: '- ' }), args + end + + @spinner_inst_mock_foo.expect :auto_spin, nil + @spinner_inst_mock_foo.expect :instance_variable_get, :stopped, [:@state] + + slash_checker = lambda do |cmd| + assert_includes [ + 'cd /app/test-neptuno && docker compose ps', + 'cd /app/test-neptuno/procfiles/foo && overmind start -D -N > /dev/null 2>&1' + ], cmd + "test-neptuno-foo-1 (healthy)\n db (healthy)" + end + + @spinner_inst_mock_foo.expect(:update, nil) do |args| + assert_equal ({ state: 'ready ' }), args + end + @spinner_inst_mock_foo.expect :success, nil + @last_spinner_mock.expect :auto_spin, nil + @last_spinner_mock.expect :stop, nil + TTY::Spinner.stub(:new, @last_spinner_mock) do + TTY::Spinner::Multi.stub(:new, @multi_spinner_mock) do + Neptuno::CLI::List.stub(:new, @cli_list_mock) do + @activate.stub(:neptuno_path, project_folder) do + @activate.stub(:system, @system_mock) do + @activate.stub(:`, slash_checker) do + @activate.stub(:sleep, nil) do + @activate.call(services: services, **@options) + @cli_list_mock.verify + @system_mock.verify + @multi_spinner_mock.verify + @spinner_inst_mock_foo.verify + @last_spinner_mock.verify + end + end + end + end + end + end + end + end + end +end diff --git a/test/ude/cli/list_test.rb b/test/ude/cli/list_test.rb new file mode 100644 index 0000000..d5e819b --- /dev/null +++ b/test/ude/cli/list_test.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'test_helper' +require './test/ude/support/cli_ovverides' + +describe Neptuno::CLI::List do + describe '#running_services' do + neptuno_path = '/app/neptuno-test' + it 'returns an array of services' do + @cli_list = Neptuno::CLI::List.new + args_checker = lambda do |cmd| + assert_equal 'cd /app/neptuno-test && docker compose ps --status running --services', cmd + "foo\nbar" + end + + @cli_list.stub(:neptuno_path, neptuno_path) do + @cli_list.stub(:`, args_checker) do + assert_equal %w[foo bar], @cli_list.running_services + end + end + end + end +end diff --git a/test/ude/support/cli_ovverides.rb b/test/ude/support/cli_ovverides.rb new file mode 100644 index 0000000..3a85c4b --- /dev/null +++ b/test/ude/support/cli_ovverides.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'neptuno/cli' + +# Allow to pass initialize without neptuno config + +module Neptuno + module CLI + class List + def initialize; end + end + + class Activate + def initialize; end + end + end +end