diff options
author | snehaldwivedi <sdwivedi@msystechnologies.com> | 2021-06-23 04:01:47 -0700 |
---|---|---|
committer | snehaldwivedi <sdwivedi@msystechnologies.com> | 2021-07-15 22:00:55 -0700 |
commit | 7aefe3dacace2976464935321932779b5ae4d510 (patch) | |
tree | 5c4ab2bba692caae083cca3e2d131a347e5e6128 | |
parent | b25ddb322afacc0fa1b24244c9e41ecfd65c16d3 (diff) | |
download | chef-7aefe3dacace2976464935321932779b5ae4d510.tar.gz |
Improved fatal condition to check file & dir exists
Signed-off-by: snehaldwivedi <sdwivedi@msystechnologies.com>
-rw-r--r-- | knife/lib/chef/knife/client_create.rb | 19 | ||||
-rw-r--r-- | knife/spec/unit/knife/client_create_spec.rb | 23 |
2 files changed, 35 insertions, 7 deletions
diff --git a/knife/lib/chef/knife/client_create.rb b/knife/lib/chef/knife/client_create.rb index 0df8c2c666..47411003b7 100644 --- a/knife/lib/chef/knife/client_create.rb +++ b/knife/lib/chef/knife/client_create.rb @@ -85,13 +85,24 @@ class Chef if config[:file] file = config[:file] - unless File.writable?(File.dirname(file)) - ui.fatal "Dir #{File.dirname(file)} is not writable. Check permissions." + dir_name = File.dirname(file) + if Dir.exist?(dir_name) + unless File.writable?(dir_name) + ui.fatal "Dir #{dir_name} is not writable. Check permissions." + exit 1 + end + else + ui.fatal "Dir #{dir_name} dose not exist." exit 1 end - unless File.writable?(file) - ui.fatal "File #{config[:file]} is not writable. Check permissions." + if File.exist?(file) + unless File.writable?(file) + ui.fatal "File #{config[:file]} is not writable. Check permissions." + exit 1 + end + else + ui.fatal "File #{file} dose not exist." exit 1 end end diff --git a/knife/spec/unit/knife/client_create_spec.rb b/knife/spec/unit/knife/client_create_spec.rb index 0b842d93d6..44c2f0e4db 100644 --- a/knife/spec/unit/knife/client_create_spec.rb +++ b/knife/spec/unit/knife/client_create_spec.rb @@ -167,10 +167,18 @@ describe Chef::Knife::ClientCreate do end end - describe "with -f or --file when dir or file is not writable" do - it "when the directory is not writable" do + describe "with -f or --file when dir or file is not writable or does not exists" do + it "when the directory does not exists" do knife.config[:file] = "example/client1.pem" - expect(knife.ui).to receive(:fatal).with("Dir example is not writable. Check permissions.") + expect(knife.ui).to receive(:fatal).with("Dir example dose not exist.") + expect { knife.run }.to raise_error(SystemExit) + end + + it "when the directory not writable" do + tmp_dir = Dir.mktmpdir + knife.config[:file] = "#{tmp_dir}/client1.pem" + File.chmod(777, tmp_dir) + expect(knife.ui).to receive(:fatal).with("Dir #{tmp_dir} is not writable. Check permissions.") expect { knife.run }.to raise_error(SystemExit) end @@ -178,6 +186,15 @@ describe Chef::Knife::ClientCreate do tmp_dir = Dir.mktmpdir file_path = "#{tmp_dir}/client1.pem" knife.config[:file] = file_path + expect(knife.ui).to receive(:fatal).with("File #{file_path} dose not exist.") + expect { knife.run }.to raise_error(SystemExit) + end + + it "when the file is not writable" do + tmp_file = Tempfile.new + file_path = tmp_file.path + knife.config[:file] = file_path + File.chmod(777, tmp_file.path) expect(knife.ui).to receive(:fatal).with("File #{file_path} is not writable. Check permissions.") expect { knife.run }.to raise_error(SystemExit) end |