From 3ce9768cd7929a624a0b54a51f9a70c6358f3810 Mon Sep 17 00:00:00 2001 From: sersut Date: Thu, 6 Mar 2014 11:42:21 -0800 Subject: [PATCH 1/3] Verify command for chef which runs the specs for the components. --- .gitignore | 1 + chef-dk.gemspec | 1 + lib/chef-dk/cli.rb | 18 +- lib/chef-dk/command/base.rb | 2 +- lib/chef-dk/command/verify.rb | 141 ++++++++++++- lib/chef-dk/exceptions.rb | 34 ++++ lib/chef-dk/helpers.rb | 49 +++++ spec/spec_helper.rb | 3 +- spec/unit/cli_spec.rb | 2 +- spec/unit/command/verify_spec.rb | 185 ++++++++++++++++++ .../missing_apps/embedded/.keep | 0 .../embedded/apps/berkshelf/.keep | 0 .../embedded/apps/test-kitchen/.keep | 0 .../valid/embedded/apps/berkshelf/verify_me | 1 + .../valid/embedded/apps/chef/verify_me | 2 + .../embedded/apps/test-kitchen/verify_me | 1 + 16 files changed, 417 insertions(+), 23 deletions(-) create mode 100644 lib/chef-dk/exceptions.rb create mode 100644 lib/chef-dk/helpers.rb create mode 100644 spec/unit/command/verify_spec.rb create mode 100644 spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/.keep create mode 100644 spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/berkshelf/.keep create mode 100644 spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/test-kitchen/.keep create mode 100755 spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/verify_me create mode 100755 spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef/verify_me create mode 100755 spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/test-kitchen/verify_me diff --git a/.gitignore b/.gitignore index 24c49c436..5629071b1 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ Gemfile.local # http://gembundler.com/man/bundle-exec.1.html b/ binstubs/ +vendor/ # RVM and RBENV ruby version files .rbenv-version diff --git a/chef-dk.gemspec b/chef-dk.gemspec index 4ec46846b..fbe3d26c6 100644 --- a/chef-dk.gemspec +++ b/chef-dk.gemspec @@ -34,6 +34,7 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.add_dependency "mixlib-cli", "~> 1.4" + gem.add_dependency "mixlib-shellout", "~> 1.3" %w(rspec-core rspec-expectations rspec-mocks).each do |dev_gem| gem.add_development_dependency dev_gem, "~> 2.14.0" diff --git a/lib/chef-dk/cli.rb b/lib/chef-dk/cli.rb index c52dd96f3..86756b129 100644 --- a/lib/chef-dk/cli.rb +++ b/lib/chef-dk/cli.rb @@ -19,10 +19,12 @@ require 'chef-dk/version' require 'chef-dk/commands_map' require 'chef-dk/builtin_commands' +require 'chef-dk/helpers' module ChefDK class CLI include Mixlib::CLI + include ChefDK::Helpers banner(<<-BANNER) Usage: @@ -96,22 +98,6 @@ def show_help end end - def err(message) - stderr.print("#{message}\n") - end - - def msg(message) - stdout.print("#{message}\n") - end - - def stdout - $stdout - end - - def stderr - $stderr - end - def exit(n) Kernel.exit(n) end diff --git a/lib/chef-dk/command/base.rb b/lib/chef-dk/command/base.rb index b7d00db42..e3c3a23f7 100644 --- a/lib/chef-dk/command/base.rb +++ b/lib/chef-dk/command/base.rb @@ -26,12 +26,12 @@ class Base :short => "-h", :long => "--help", :description => "Show this message", - :on => :tail, :boolean => true def initialize super end + end end end diff --git a/lib/chef-dk/command/verify.rb b/lib/chef-dk/command/verify.rb index a3e0bc4e5..2a6bf56a2 100644 --- a/lib/chef-dk/command/verify.rb +++ b/lib/chef-dk/command/verify.rb @@ -16,16 +16,149 @@ # require 'chef-dk/command/base' +require 'chef-dk/exceptions' +require 'chef-dk/helpers' module ChefDK module Command class Verify < ChefDK::Command::Base - banner "Usage: chef verify <>" + include ChefDK::Helpers + include ChefDK::Exceptions - def run(params) - parse_options - puts "TODO: Implement verify." + banner "Usage: chef verify" + + class << self + def component(name, arguments) + components[name] = arguments + end + + def components + @components ||= {} + @components + end + end + + def components + self.class.components + end + + # + # Components included in Chef Development kit: + # :base_dir => Relative path of the component w.r.t. #{omnibus_dir}/apps + # :test_cmd => Test command to be launched for the component + # + component "berkshelf", + :base_dir => "berkshelf", + # For berks the real command to run is "bundle exec thor spec:ci" + # We can't run it right now since mercurial specs are included in the + # test suite by default. We will be able to switch to that command when + # this is merged: + # https://github.com/berkshelf/berkshelf/pull/1021 + :test_cmd => "bundle exec rspec --color --format progress spec/unit --tag ~hg && \ + bundle exec cucumber --color --format progress --tags ~@no_run --strict" + + component "test-kitchen", + :base_dir => "test-kitchen", + :test_cmd => "bundle exec rake" + + component "chef-client", + :base_dir => "chef", + :test_cmd => "bundle exec rspec" + + + attr_reader :omnibus_dir + attr_reader :verification_threads + attr_reader :verification_results + attr_reader :verification_status + + def initialize + super + @verification_threads = [ ] + @verification_results = [ ] + @verification_status = 0 + end + + def run(params = [ ]) + parse_options(params) + + locate_omnibus_dir + invoke_tests + wait_for_tests + report_results + + verification_status end + + # + # Locates the directory components are installed on the system. + # + # In omnibus installations ruby lives at: + # omnibus_install_dir/embedded/bin and components live at + # omnibus_install_dir/embedded/apps + # + def locate_omnibus_dir + @omnibus_dir = File.expand_path(File.join(Gem.ruby, "..","..", "apps")) + raise OmnibusInstallNotFound.new() unless File.directory? omnibus_dir + + components.each do |component, component_info| + unless File.exists? component_path(component_info) + raise MissingComponentError.new(component) + end + end + end + + def component_path(component_info) + File.join(omnibus_dir, component_info[:base_dir]) + end + + def invoke_tests + components.each do |component, component_info| + # Run the component specs in parallel + verification_threads << Thread.new do + result = system_command component_info[:test_cmd], + :cwd => component_path(component_info), + :env => { + "PATH" => "#{File.join(omnibus_dir, "bin")}:#{ENV['PATH']}" + }, + :timeout => 3600 + + @verification_status = 1 if result.exitstatus != 0 + + { + :component => component, + :result => result + } + end + + msg("Running verification for component '#{component}'") + end + end + + def wait_for_tests + while !verification_threads.empty? + verification_threads.each do |t| + if t.join(1) + verification_threads.delete t + verification_results << t.value + msg("") + msg(t.value[:result].stdout) + msg(t.value[:result].stderr) if t.value[:result].stderr + else + $stdout.write "." + end + end + end + end + + def report_results + msg("") + msg("---------------------------------------------") + verification_results.each do |result| + message = result[:result].exitstatus == 0 ? "succeeded" : "failed" + msg("Verification of component '#{result[:component]}' #{message}.") + end + end + end end end diff --git a/lib/chef-dk/exceptions.rb b/lib/chef-dk/exceptions.rb new file mode 100644 index 000000000..6bedcbb68 --- /dev/null +++ b/lib/chef-dk/exceptions.rb @@ -0,0 +1,34 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module ChefDK + module Exceptions + + class MissingComponentError < RuntimeError + def initialize(component_name) + super("Component #{component_name} is missing.") + end + end + + class OmnibusInstallNotFound < RuntimeError + def initialize() + super("Can not find omnibus installation directory for Chef.") + end + end + + end +end diff --git a/lib/chef-dk/helpers.rb b/lib/chef-dk/helpers.rb new file mode 100644 index 000000000..e97ea3288 --- /dev/null +++ b/lib/chef-dk/helpers.rb @@ -0,0 +1,49 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'mixlib/shellout' + +module ChefDK + module Helpers + + # + # Runs given commands using mixlib-shellout + # + def system_command(*command_args) + cmd = Mixlib::ShellOut.new(*command_args) + cmd.run_command + cmd + end + + def err(message) + stderr.print("#{message}\n") + end + + def msg(message) + stdout.print("#{message}\n") + end + + def stdout + $stdout + end + + def stderr + $stderr + end + + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ccabbaa07..1f3a982b2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,7 +19,8 @@ require 'rspec/mocks' require 'pry-debugger' +FIXTURES_PATH = File.expand_path(File.dirname(__FILE__) + "/unit/fixtures/") + RSpec.configure do |c| c.include ChefDK end - diff --git a/spec/unit/cli_spec.rb b/spec/unit/cli_spec.rb index f77edc138..b7358320a 100644 --- a/spec/unit/cli_spec.rb +++ b/spec/unit/cli_spec.rb @@ -1,4 +1,4 @@ -# + # # Copyright:: Copyright (c) 2014 Chef Software Inc. # License:: Apache License, Version 2.0 # diff --git a/spec/unit/command/verify_spec.rb b/spec/unit/command/verify_spec.rb new file mode 100644 index 000000000..f5a40563b --- /dev/null +++ b/spec/unit/command/verify_spec.rb @@ -0,0 +1,185 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' +require 'chef-dk/command/verify' + +describe ChefDK::Command::Verify do + let(:command_instance) { ChefDK::Command::Verify.new() } + + def run_command(expected_exit_code, command_options = [ ]) + expect(command_instance.run(command_options)).to eq(expected_exit_code) + end + + describe "when locating omnibus directory" do + it "should find omnibus directory from ruby path" do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + command_instance.locate_omnibus_dir + expect(command_instance.omnibus_dir).to include("eg_omnibus_dir/valid/embedded") + end + + it "should raise OmnibusInstallNotFound if directory is not looking like omnibus" do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH,".rbenv/versions/2.1.1/bin/ruby")) + expect{command_instance.locate_omnibus_dir}.to raise_error(ChefDK::Exceptions::OmnibusInstallNotFound) + end + + it "raises OmnibusInstallNotFound if omnibus directory doesn't exist" do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH,"eg_omnibus_dir/missing_apps/embedded/bin/ruby")) + expect{command_instance.locate_omnibus_dir}.to raise_error(ChefDK::Exceptions::OmnibusInstallNotFound) + end + + it "raises MissingComponentError when a component doesn't exist" do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH,"eg_omnibus_dir/missing_component/embedded/bin/ruby")) + expect{command_instance.locate_omnibus_dir}.to raise_error(ChefDK::Exceptions::MissingComponentError) + end + end + + describe "when running verify command" do + let(:stdout_io) { StringIO.new } + + def stdout + stdout_io.string + end + + before do + command_instance.stub(:stdout).and_return(stdout_io) + end + + it "should have components by default" do + expect(command_instance.components).not_to be_empty + end + + it "should have components by default" do + expect(command_instance.banner).to eq("Usage: chef verify") + end + + describe "with single command with success" do + before do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + command_instance.stub(:components).and_return({ + "successful_comp" => { + :base_dir => "berkshelf", + :test_cmd => "./verify_me" + } + }) + + run_command(0) + end + + it "should report the success of the command" do + expect(stdout).to include("Verification of component 'successful_comp' succeeded.") + end + + it "reports the component test output" do + expect(stdout).to include("you are good to go...") + end + end + + describe "with single command with failure" do + before do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + command_instance.stub(:components).and_return({ + "failing_comp" => { + :base_dir => "chef", + :test_cmd => "./verify_me" + } + }) + + run_command(1) + end + + it "should report the failure of the command" do + expect(stdout).to include("Verification of component 'failing_comp' failed.") + end + + it "reports the component test output" do + expect(stdout).to include("i'm not feeling good today...") + end + end + + describe "with multiple commands with success" do + before do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + command_instance.stub(:components).and_return({ + "successful_comp_1" => { + :base_dir => "berkshelf", + :test_cmd => "./verify_me" + }, + "successful_comp_2" => { + :base_dir => "test-kitchen", + :test_cmd => "./verify_me" + } + }) + + run_command(0) + end + + it "should report the success of the command" do + expect(stdout).to include("Verification of component 'successful_comp_1' succeeded.") + expect(stdout).to include("Verification of component 'successful_comp_2' succeeded.") + end + + it "reports the component test outputs" do + expect(stdout).to include("you are good to go...") + expect(stdout).to include("my friend everything is good...") + end + + it "should report the output of the first verification first" do + index_first = stdout.index("you are good to go...") + index_second = stdout.index("my friend everything is good...") + expect(index_second > index_first).to be_true + end + end + + describe "with multiple commands with failures" do + before do + Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + command_instance.stub(:components).and_return({ + "successful_comp_1" => { + :base_dir => "berkshelf", + :test_cmd => "./verify_me" + }, + "successful_comp_2" => { + :base_dir => "test-kitchen", + :test_cmd => "./verify_me" + }, + "failing_comp" => { + :base_dir => "chef", + :test_cmd => "./verify_me" + } + + }) + + run_command(1) + end + + it "should report the success and failure of the commands" do + expect(stdout).to include("Verification of component 'successful_comp_1' succeeded.") + expect(stdout).to include("Verification of component 'successful_comp_2' succeeded.") + expect(stdout).to include("Verification of component 'failing_comp' failed.") + end + + it "reports the component test outputs" do + expect(stdout).to include("you are good to go...") + expect(stdout).to include("my friend everything is good...") + expect(stdout).to include("i'm not feeling good today...") + end + end + + end + +end diff --git a/spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/.keep b/spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/berkshelf/.keep b/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/berkshelf/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/test-kitchen/.keep b/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/test-kitchen/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/verify_me b/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/verify_me new file mode 100755 index 000000000..1425635be --- /dev/null +++ b/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/verify_me @@ -0,0 +1 @@ +echo "you are good to go..." diff --git a/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef/verify_me b/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef/verify_me new file mode 100755 index 000000000..799d4c157 --- /dev/null +++ b/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef/verify_me @@ -0,0 +1,2 @@ +echo "i'm not feeling good today..." +exit(1) diff --git a/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/test-kitchen/verify_me b/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/test-kitchen/verify_me new file mode 100755 index 000000000..733ddbb50 --- /dev/null +++ b/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/test-kitchen/verify_me @@ -0,0 +1 @@ +echo "my friend everything is good..." From 3219e646d6f87aab4fb69ef92585ab474e6f9936 Mon Sep 17 00:00:00 2001 From: sersut Date: Thu, 6 Mar 2014 15:52:20 -0800 Subject: [PATCH 2/3] Handle default options (-v / --version / -h / --help) outside optparser. --- lib/chef-dk/cli.rb | 2 +- lib/chef-dk/command/base.rb | 34 ++++++++ lib/chef-dk/command/verify.rb | 4 - spec/unit/command/base_spec.rb | 83 +++++++++++++++++++ .../unit/fixtures/command/cli_test_command.rb | 3 +- 5 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 spec/unit/command/base_spec.rb diff --git a/lib/chef-dk/cli.rb b/lib/chef-dk/cli.rb index 86756b129..f90b6ca51 100644 --- a/lib/chef-dk/cli.rb +++ b/lib/chef-dk/cli.rb @@ -80,7 +80,7 @@ def run_subcommands(params) return false if subcommand_name.nil? return false if option?(subcommand_name) if have_command?(subcommand_name) - instantiate_subcommand(subcommand_name).run(subcommand_params) + instantiate_subcommand(subcommand_name).run_with_default_options(subcommand_params) else err("Unknown command `#{subcommand_name}'.") show_help diff --git a/lib/chef-dk/command/base.rb b/lib/chef-dk/command/base.rb index e3c3a23f7..f56f62b6a 100644 --- a/lib/chef-dk/command/base.rb +++ b/lib/chef-dk/command/base.rb @@ -16,11 +16,14 @@ # require 'mixlib/cli' +require 'chef-dk/helpers' +require 'chef-dk/version' module ChefDK module Command class Base include Mixlib::CLI + include ChefDK::Helpers option :help, :short => "-h", @@ -28,10 +31,41 @@ class Base :description => "Show this message", :boolean => true + option :version, + :short => "-v", + :long => "--version", + :description => "Show chef version", + :boolean => true + def initialize super end + # + # optparser overwrites -h / --help options with its own. + # In order to control this behavior, make sure the default options are + # handled here. + # + def run_with_default_options(params = [ ]) + if needs_help?(params) + msg(banner) + 0 + elsif needs_version?(params) + msg("Chef Development Kit Version: #{ChefDK::VERSION}") + 0 + else + run(params) + end + end + + def needs_help?(params) + params.include?("-h") || params.include?("--help") + end + + def needs_version?(params) + params.include?("-v") || params.include?("--version") + end + end end end diff --git a/lib/chef-dk/command/verify.rb b/lib/chef-dk/command/verify.rb index 2a6bf56a2..c86acd03d 100644 --- a/lib/chef-dk/command/verify.rb +++ b/lib/chef-dk/command/verify.rb @@ -17,12 +17,10 @@ require 'chef-dk/command/base' require 'chef-dk/exceptions' -require 'chef-dk/helpers' module ChefDK module Command class Verify < ChefDK::Command::Base - include ChefDK::Helpers include ChefDK::Exceptions banner "Usage: chef verify" @@ -79,8 +77,6 @@ def initialize end def run(params = [ ]) - parse_options(params) - locate_omnibus_dir invoke_tests wait_for_tests diff --git a/spec/unit/command/base_spec.rb b/spec/unit/command/base_spec.rb new file mode 100644 index 000000000..71dc0305c --- /dev/null +++ b/spec/unit/command/base_spec.rb @@ -0,0 +1,83 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' +require 'chef-dk/command/verify' + +describe ChefDK::Command::Base do + class TestCommand < ChefDK::Command::Base + banner "use me please" + + option :user, + :short => "-u", + :long => "--user", + :description => "If the user exists", + :boolean => true + + def run(params) + parse_options(params) + msg("thanks for passing me #{config[:user]}") + end + end + + let(:stdout_io) { StringIO.new } + let(:command_instance) { TestCommand.new() } + + def stdout + stdout_io.string + end + + before do + command_instance.stub(:stdout).and_return(stdout_io) + end + + + def run_command(options) + command_instance.run_with_default_options(options) + end + + it "should print the banner for -h" do + run_command(["-h"]) + expect(stdout).to eq("use me please\n") + end + + it "should print the banner for --help" do + run_command(["--help"]) + expect(stdout).to eq("use me please\n") + end + + it "should print the version for -v" do + run_command(["-v"]) + expect(stdout).to eq("Chef Development Kit Version: #{ChefDK::VERSION}\n") + end + + it "should print the version for --version" do + run_command(["--version"]) + expect(stdout).to eq("Chef Development Kit Version: #{ChefDK::VERSION}\n") + end + + it "should run the command passing in the custom options for long custom options" do + run_command(["--user"]) + expect(stdout).to eq("thanks for passing me true\n") + end + + it "should run the command passing in the custom options for short custom options" do + run_command(["-u"]) + expect(stdout).to eq("thanks for passing me true\n") + end + +end diff --git a/spec/unit/fixtures/command/cli_test_command.rb b/spec/unit/fixtures/command/cli_test_command.rb index b1a3d9155..c5058b677 100644 --- a/spec/unit/fixtures/command/cli_test_command.rb +++ b/spec/unit/fixtures/command/cli_test_command.rb @@ -1,7 +1,8 @@ +require 'chef-dk/command/base' module ChefDK module Command - class TestCommand + class TestCommand < ChefDK::Command::Base def self.reset! @test_result = nil From 0522b49e1c578bbd6e4c06ff63eb5068737072f8 Mon Sep 17 00:00:00 2001 From: sersut Date: Thu, 6 Mar 2014 16:41:33 -0800 Subject: [PATCH 3/3] Reorganization based on PR comments. --- spec/helpers.rb | 22 ++++++++++++++++++++++ spec/spec_helper.rb | 4 ++-- spec/unit/command/verify_spec.rb | 16 ++++++++-------- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 spec/helpers.rb diff --git a/spec/helpers.rb b/spec/helpers.rb new file mode 100644 index 000000000..6d9470f1d --- /dev/null +++ b/spec/helpers.rb @@ -0,0 +1,22 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module Helpers + def fixtures_path + File.expand_path(File.dirname(__FILE__) + "/unit/fixtures/") + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1f3a982b2..b2c4f356c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,9 +18,9 @@ require 'rubygems' require 'rspec/mocks' require 'pry-debugger' - -FIXTURES_PATH = File.expand_path(File.dirname(__FILE__) + "/unit/fixtures/") +require 'helpers' RSpec.configure do |c| c.include ChefDK + c.include Helpers end diff --git a/spec/unit/command/verify_spec.rb b/spec/unit/command/verify_spec.rb index f5a40563b..1f9782659 100644 --- a/spec/unit/command/verify_spec.rb +++ b/spec/unit/command/verify_spec.rb @@ -27,23 +27,23 @@ def run_command(expected_exit_code, command_options = [ ]) describe "when locating omnibus directory" do it "should find omnibus directory from ruby path" do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path, "eg_omnibus_dir/valid/embedded/bin/ruby")) command_instance.locate_omnibus_dir expect(command_instance.omnibus_dir).to include("eg_omnibus_dir/valid/embedded") end it "should raise OmnibusInstallNotFound if directory is not looking like omnibus" do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH,".rbenv/versions/2.1.1/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path,".rbenv/versions/2.1.1/bin/ruby")) expect{command_instance.locate_omnibus_dir}.to raise_error(ChefDK::Exceptions::OmnibusInstallNotFound) end it "raises OmnibusInstallNotFound if omnibus directory doesn't exist" do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH,"eg_omnibus_dir/missing_apps/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path,"eg_omnibus_dir/missing_apps/embedded/bin/ruby")) expect{command_instance.locate_omnibus_dir}.to raise_error(ChefDK::Exceptions::OmnibusInstallNotFound) end it "raises MissingComponentError when a component doesn't exist" do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH,"eg_omnibus_dir/missing_component/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path,"eg_omnibus_dir/missing_component/embedded/bin/ruby")) expect{command_instance.locate_omnibus_dir}.to raise_error(ChefDK::Exceptions::MissingComponentError) end end @@ -69,7 +69,7 @@ def stdout describe "with single command with success" do before do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path, "eg_omnibus_dir/valid/embedded/bin/ruby")) command_instance.stub(:components).and_return({ "successful_comp" => { :base_dir => "berkshelf", @@ -91,7 +91,7 @@ def stdout describe "with single command with failure" do before do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path, "eg_omnibus_dir/valid/embedded/bin/ruby")) command_instance.stub(:components).and_return({ "failing_comp" => { :base_dir => "chef", @@ -113,7 +113,7 @@ def stdout describe "with multiple commands with success" do before do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path, "eg_omnibus_dir/valid/embedded/bin/ruby")) command_instance.stub(:components).and_return({ "successful_comp_1" => { :base_dir => "berkshelf", @@ -147,7 +147,7 @@ def stdout describe "with multiple commands with failures" do before do - Gem.stub(:ruby).and_return(File.join(FIXTURES_PATH, "eg_omnibus_dir/valid/embedded/bin/ruby")) + Gem.stub(:ruby).and_return(File.join(fixtures_path, "eg_omnibus_dir/valid/embedded/bin/ruby")) command_instance.stub(:components).and_return({ "successful_comp_1" => { :base_dir => "berkshelf",