spec/unit/cli_spec.rb in chef-dk-0.2.1 vs spec/unit/cli_spec.rb in chef-dk-0.3.0
- old
+ new
@@ -1,6 +1,5 @@
-#
# 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.
@@ -57,13 +56,19 @@
let(:version_message) { "Chef Development Kit Version: #{ChefDK::VERSION}\n" }
def run_cli(expected_exit_code)
expect(cli).to receive(:exit).with(expected_exit_code)
+ expect(cli).to receive(:sanity_check!)
cli.run
end
+ def run_cli_with_sanity_check(expected_exit_code)
+ expect(cli).to receive(:exit).with(expected_exit_code)
+ cli.run
+ end
+
subject(:cli) do
ChefDK::CLI.new(argv).tap do |c|
allow(c).to receive(:commands_map).and_return(commands_map)
allow(c).to receive(:stdout).and_return(stdout_io)
allow(c).to receive(:stderr).and_return(stderr_io)
@@ -105,10 +110,22 @@
run_cli(0)
expect(stdout).to eq(version_message)
end
end
+ context "given an invalid option" do
+
+ let(:argv) { %w[-nope] }
+
+ it "prints an 'invalid option message and the help output, then exits non-zero" do
+ run_cli(1)
+ expect(stdout).to eq(base_help_message)
+ expect(stderr).to eq("invalid option: -nope\n")
+ end
+
+ end
+
context "given an invalid/unknown subcommand" do
let(:argv) { %w[ancient-aliens] }
it "prints an 'unknown command' message and the help output" do
expected_err = "Unknown command `ancient-aliens'.\n"
@@ -146,6 +163,88 @@
run_cli(23)
expect(test_result[:params]).to eq(params)
end
end
+ context "sanity_check!" do
+ context "on unix" do
+ it "complains if embedded is first" do
+ expect(cli).to receive(:env).and_return({'PATH' => '/opt/chefdk/embedded/bin:/opt/chefdk/bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).not_to eq(base_help_message)
+ expect(stdout).to include("please reverse that order")
+ expect(stdout).to include("chef shell-init")
+ end
+
+ it "complains if only embedded is present" do
+ expect(cli).to receive(:env).and_return({'PATH' => '/opt/chefdk/embedded/bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).not_to eq(base_help_message)
+ expect(stdout).to include("you must add")
+ expect(stdout).to include("chef shell-init")
+ end
+
+ it "passes when both are present in the correct order" do
+ expect(cli).to receive(:env).and_return({'PATH' => '/opt/chefdk/bin:/opt/chefdk/embedded/bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).to eq(base_help_message)
+ end
+
+ it "passes when only the omnibus bin dir is present" do
+ expect(cli).to receive(:env).and_return({'PATH' => '/opt/chefdk/bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).to eq(base_help_message)
+ end
+ end
+
+ context "on windows" do
+ before do
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
+ stub_const("File::PATH_SEPARATOR", ';')
+ end
+
+ it "complains if embedded is first" do
+ expect(cli).to receive(:env).and_return({'PATH' => 'C:\opscode\chefdk\embedded\bin;C:\opscode\chefdk\bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).not_to eq(base_help_message)
+ expect(stdout).to include("please reverse that order")
+ expect(stdout).to include("chef shell-init")
+ end
+
+ it "complains if only embedded is present" do
+ expect(cli).to receive(:env).and_return({'PATH' => 'C:\opscode\chefdk\embedded\bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).not_to eq(base_help_message)
+ expect(stdout).to include("you must add")
+ expect(stdout).to include("chef shell-init")
+ end
+
+ it "passes when both are present in the correct order" do
+ expect(cli).to receive(:env).and_return({'PATH' => 'C:\opscode\chefdk\bin;C:\opscode\chefdk\embedded\bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).to eq(base_help_message)
+ end
+
+ it "passes when only the omnibus bin dir is present" do
+ expect(cli).to receive(:env).and_return({'PATH' => 'C:\opscode\chefdk\bin' })
+ allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chefdk/embedded/bin")
+ allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chefdk/bin")
+ run_cli_with_sanity_check(0)
+ expect(stdout).to eq(base_help_message)
+ end
+ end
+ end
end