Skip to content
This repository
Browse code

ensure that --init does not overwrite an existing file and actually e…

…xists upon completion
  • Loading branch information...
commit 2f2fbf9cca18597135807e795ded2bcf731a5aa4 1 parent a49e1a7
Wes Bailey authored
6 bin/claws
@@ -6,11 +6,7 @@ require 'yaml'
6 6
7 7 options = Claws::Options.parse
8 8
9   -if options.initialize
10   - Claws::Command::Initialize.exec
11   - puts "Please enter your access key id and secret access key in #{$HOME}/.claws.yml"
12   - exit 0
13   -end
  9 +Claws::Command::Initialize.exec if options.initialize
14 10
15 11 Claws::Command::Version.exec if options.version
16 12
13 lib/claws/command/initialize.rb
@@ -55,9 +55,16 @@ def self.exec
55 55 }
56 56
57 57 conf = File.join(ENV['HOME'], '.claws.yml')
58   - puts "Creating configuration file: #{conf}\n..."
59   - File.open(conf, 'w').write(h.to_yaml)
60   - puts 'Complete!'
  58 +
  59 + if File.exists?(conf)
  60 + puts "Configuration file #{conf} exists! Either remove or modify contents."
  61 + exit 1
  62 + else
  63 + puts "Creating configuration file: #{conf}\n..."
  64 + File.open(conf, 'w').write(h.to_yaml)
  65 + puts "Complete!\nPlease enter your access key id and secret access key in #{conf}"
  66 + exit 0
  67 + end
61 68 end
62 69 end
63 70 end
13 spec/command/initialize_spec.rb
@@ -7,6 +7,7 @@
7 7
8 8 it 'works' do
9 9 File.should_receive(:join).and_return(config)
  10 + File.should_receive(:exists?).and_return(false)
10 11 subject.should_receive(:puts)
11 12
12 13 fh = double( File, :write => true )
@@ -14,6 +15,18 @@
14 15 File.should_receive(:open).with(config, 'w').and_return(fh)
15 16 subject.should_receive(:puts)
16 17
  18 + subject.should_receive(:exit).with(0)
  19 +
  20 + subject.exec
  21 + end
  22 +
  23 + it 'does not overwrite existing configuration file' do
  24 + File.should_receive(:join).and_return(config)
  25 + File.should_receive(:exists?).and_return(true)
  26 +
  27 + subject.should_receive(:puts)
  28 + subject.should_receive(:exit).with(1)
  29 +
17 30 subject.exec
18 31 end
19 32 end

0 comments on commit 2f2fbf9

Please sign in to comment.
Something went wrong with that request. Please try again.