From 42a863f9377b529b6514ddaceccd69c9b0b9c515 Mon Sep 17 00:00:00 2001 From: Pat Nafarrete Date: Wed, 25 Sep 2019 17:42:25 -0400 Subject: [PATCH] Add characterization tests for helpers.rb (#120) * Add characterization tests for helpers.rb * Place tests under :test namespace * Set default rake action to test:default --- Rakefile | 8 ++- lib/deployinator/tasks/tests.rake | 7 +++ test/character/deployinator_helpers_test.rb | 57 +++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/character/deployinator_helpers_test.rb diff --git a/Rakefile b/Rakefile index 2e91382..ef0ac01 100644 --- a/Rakefile +++ b/Rakefile @@ -10,6 +10,12 @@ def command?(command) system("type #{command} &> /dev/null") end -task :default => 'deployinator:test:unit' +task :default => 'test:unit' + +namespace :test do + task :default => 'deployinator:test:unit' + task :character => 'deployinator:test:character' + task :unit => 'deployinator:test:unit' +end load 'deployinator/tasks/tests.rake' diff --git a/lib/deployinator/tasks/tests.rake b/lib/deployinator/tasks/tests.rake index 04c1dc3..296bbe2 100644 --- a/lib/deployinator/tasks/tests.rake +++ b/lib/deployinator/tasks/tests.rake @@ -18,5 +18,12 @@ namespace :deployinator do t.pattern = "#{File.dirname(__FILE__)}/../../../test/functional/**/*_test.rb" t.verbose = false end + + desc 'Run deployinator characterization tests' + Rake::TestTask.new :character do |t| + t.libs << 'lib' + t.pattern = "#{File.dirname(__FILE__)}/../../../test/character/**/*_test.rb" + t.verbose = false + end end end diff --git a/test/character/deployinator_helpers_test.rb b/test/character/deployinator_helpers_test.rb new file mode 100644 index 0000000..2d5426c --- /dev/null +++ b/test/character/deployinator_helpers_test.rb @@ -0,0 +1,57 @@ +require 'test/unit' +require 'mocha/test_unit' +require 'open3' + +require File.expand_path('../../lib/deployinator/helpers.rb',__dir__) + +class DeployinatorHelpersTest < Test::Unit::TestCase + + def test_run_cmd_calls_popen3_once_with_expected_command + # arrange + cmd = 'fakecommand' + deployinator_helper = Class.new.extend(Deployinator::Helpers) + Open3.expects(:popen3).with(cmd).once + + # act & assert + deployinator_helper.run_cmd(cmd) + end + + def test_run_cmd_fails_while_log_errors_is_false + # arrange + cmd = 'echo -n epicfail && exit 1' + deployinator_helper = Class.new.extend(Deployinator::Helpers) + deployinator_helper.expects(:log_and_stream).times(5) + log_errors = false + + expected = { + :exit_code => 1, + :stdout => 'epicfail' + } + + # act + actual = deployinator_helper.run_cmd(cmd, nil, log_errors) + + # assert + assert_equal(expected, actual) + end + + def test_run_cmd_fails_while_log_errors_is_true + # arrange + cmd = 'echo -n epicfail && exit 1' + deployinator_helper = Class.new.extend(Deployinator::Helpers) + deployinator_helper.expects(:log_and_stream).times(5) + log_errors = true + + expected = { + :exit_code => 1, + :stdout => 'epicfail' + } + + # act + actual = deployinator_helper.run_cmd(cmd, nil, log_errors) + + # assert + assert_equal(expected, actual) + end + +end