From b5f398a41d4798bc7c1f18da17221e8b05403c7c Mon Sep 17 00:00:00 2001 From: Frank Koehl Date: Wed, 5 Nov 2014 10:38:44 -0500 Subject: [PATCH 1/3] Added helpers to look for deployments without any roles. --- lib/whiskey_disk/helpers.rb | 12 ++- spec/whiskey_disk/helpers_spec.rb | 122 +++++++++++++++++------------- 2 files changed, 81 insertions(+), 53 deletions(-) diff --git a/lib/whiskey_disk/helpers.rb b/lib/whiskey_disk/helpers.rb index cb3c039..33565dd 100644 --- a/lib/whiskey_disk/helpers.rb +++ b/lib/whiskey_disk/helpers.rb @@ -5,6 +5,16 @@ def role?(role) ENV['WD_ROLES'].split(':').include?(role.to_s) end +# does the current deployment have any role definitions? +def no_roles? + ENV['WD_ROLES'] == '' +end + +# look for a given role, or lack of all roles +def nothing_or_role?(role) + no_roles? || role?(role) +end + # have files of interest changed on this deployment? def changed?(path) return true unless gc = git_changes @@ -47,4 +57,4 @@ def git_changes_path def rsync_changes_path File.join(changes_file_root, '.whiskey_disk_rsync_changes') -end \ No newline at end of file +end diff --git a/spec/whiskey_disk/helpers_spec.rb b/spec/whiskey_disk/helpers_spec.rb index 0d7938a..539ee9b 100644 --- a/spec/whiskey_disk/helpers_spec.rb +++ b/spec/whiskey_disk/helpers_spec.rb @@ -5,7 +5,7 @@ it 'accepts a role string' do lambda { role?('web') }.should.not.raise(ArgumentError) end - + it 'requires a role string' do lambda { role? }.should.raise(ArgumentError) end @@ -19,26 +19,44 @@ ENV['WD_ROLES'] = '' role?(:web).should.be.false end - + it 'returns true if the role, as a symbol is among the roles in the WD_ROLES env variable' do ENV['WD_ROLES'] = 'db:web' role?(:db).should.be.true end - + it 'returns true if the role, as a string is among the roles in the WD_ROLES env variable' do ENV['WD_ROLES'] = 'db:web' role?('db').should.be.true end - + it 'returns false if the role, as a symbol is not among the roles in the WD_ROLES env variable' do ENV['WD_ROLES'] = 'db:web' role?(:app).should.be.false end - + it 'returns false if the role, as a string is not among the roles in the WD_ROLES env variable' do ENV['WD_ROLES'] = 'db:web' role?('app').should.be.false end + + it "returns true if checking for no roles" do + ENV['WD_ROLES'] = '' + no_roles?.should.be.true + ENV['WD_ROLES'] = 'db:web' + no_roles?.should.be.false + end + + it "returns true when given role is set, or no roles exist" do + ENV['WD_ROLES'] = '' + nothing_or_role?.should.be.true + nothing_or_role?('foo').should.be.true + nothing_or_role?('web').should.be.true + ENV['WD_ROLES'] = 'db:web' + nothing_or_role?.should.be.false + nothing_or_role?('foo').should.be.false + nothing_or_role?('web').should.be.true + end end def set_git_changes(changes) @@ -56,40 +74,40 @@ def set_rsync_changes(changes) @non_matching_file = '/nowhere/file' @substring_file = '/nowhere/filething' @random_file = '/random/path' - + set_git_changes([]) set_rsync_changes([]) end - + it 'accepts a path' do lambda { changed?('foo') }.should.not.raise(ArgumentError) end - + it 'requires a path' do lambda { changed? }.should.raise(ArgumentError) end - + it 'returns true when the specified file is in the list of git changes' do set_git_changes([ @matching_file, @random_file]) changed?(@matching_file).should.be.true end - + it 'ignores trailing "/"s in the provided path when doing an exact git change match' do set_git_changes([ @matching_file, @random_file]) - changed?(@matching_file + '///').should.be.true + changed?(@matching_file + '///').should.be.true end - + it 'returns true when the specified path is a full path prefix in the list of git changes' do set_git_changes([ @matching_file , @random_file]) - changed?(@matching_path).should.be.true + changed?(@matching_path).should.be.true end - + it 'ignores trailing "/"s in the provided path when doing a path git change match' do set_git_changes([ @matching_file , @random_file]) - changed?(@matching_path + '///').should.be.true + changed?(@matching_path + '///').should.be.true end - it 'returns true when the specified file is in the list of rsync changes' do + it 'returns true when the specified file is in the list of rsync changes' do set_rsync_changes([ @matching_file, @random_file]) changed?(@matching_file).should.be.true end @@ -101,29 +119,29 @@ def set_rsync_changes(changes) it 'returnes true when the specified path is a full path prefix in the list of git changes' do set_rsync_changes([ @matching_file , @random_file]) - changed?(@matching_path).should.be.true + changed?(@matching_path).should.be.true end - + it 'ignores trailing "/"s in the provided path when doing a path rsync change match' do set_rsync_changes([ @matching_file , @random_file]) - changed?(@matching_path + '///').should.be.true + changed?(@matching_path + '///').should.be.true end - + it 'ignores regex metacharacters when looking for a git match' do set_git_changes([ '/path/to/somestring']) changed?('/path/to/some.*').should.be.false end - + it 'ignores regex metacharacters when looking for an rsync match' do set_rsync_changes([ '/path/to/somestring']) changed?('/path/to/some.*').should.be.false end - + it 'returns true when the git changes file cannot be found' do set_git_changes(nil) - changed?(@matching_file).should.be.true + changed?(@matching_file).should.be.true end - + it 'returns false if not path or file matches the specified file' do set_git_changes([@matching_file, @matching_path, @random_file, @substring_file]) set_rsync_changes([@matching_file, @matching_path, @random_file, @substring_file]) @@ -153,32 +171,32 @@ def set_rsync_changes(changes) spec/whiskey_disk/helpers_spec.rb spec/whiskey_disk_spec.rb whiskey_disk.gemspec -' +' end - + it 'works without arguments' do lambda { git_changes }.should.not.raise(ArgumentError) end - + it 'does not allow arguments' do lambda { git_changes(:foo) }.should.raise(ArgumentError) end - + it 'returns nil when a git changes file cannot be found' do self.stub!(:read_git_changes_file).and_raise git_changes.should.be.nil end - + it 'returns an empty list if no files are found in the git changes file' do self.stub!(:read_git_changes_file).and_return('') git_changes.should == [] end - + it 'returns a list of all filenames mentioned in the git changes file' do self.stub!(:read_git_changes_file).and_return(@contents) git_changes.should == @contents.split("\n") end - + it 'strips duplicates from filenames mentioned in the git changes file' do lines = @contents.split("\n") duplicates = @contents + lines.first + "\n" + lines.last + "\n" @@ -329,15 +347,15 @@ def set_rsync_changes(changes) self.stub!(:git_changes_path).and_return(@changes_path) File.stub!(:read).with(@changes_path).and_return(@contents) end - + it 'works without arguments' do lambda { read_git_changes_file }.should.not.raise(ArgumentError) end - + it 'does not allow arguments' do lambda { read_git_changes_file(:foo) }.should.raise(ArgumentError) end - + it 'reads the git changes file' do File.should.receive(:read) do |arg| arg.should == @changes_path @@ -345,11 +363,11 @@ def set_rsync_changes(changes) end read_git_changes_file end - + it 'returns the contents of the git changes file' do read_git_changes_file.should == @contents end - + it 'fails if the git changes file cannot be read' do File.stub!(:read).with(@changes_path).and_raise(Errno::ENOENT) lambda { read_git_changes_file }.should.raise(Errno::ENOENT) @@ -363,15 +381,15 @@ def set_rsync_changes(changes) self.stub!(:rsync_changes_path).and_return(@changes_path) File.stub!(:read).with(@changes_path).and_return(@contents) end - + it 'works without arguments' do lambda { read_rsync_changes_file }.should.not.raise(ArgumentError) end - + it 'does not allow arguments' do lambda { read_rsync_changes_file(:foo) }.should.raise(ArgumentError) end - + it 'reads the rsync changes file' do File.should.receive(:read) do |arg| arg.should == @changes_path @@ -379,11 +397,11 @@ def set_rsync_changes(changes) end read_rsync_changes_file end - + it 'returns the contents of the rsync changes file' do read_rsync_changes_file.should == @contents end - + it 'fails if the rsync changes file cannot be read' do File.stub!(:read).with(@changes_path).and_raise(Errno::ENOENT) lambda { read_rsync_changes_file }.should.raise(Errno::ENOENT) @@ -397,22 +415,22 @@ def set_rsync_changes(changes) IO.stub!(:popen).with("git rev-parse --show-toplevel").and_return(@io_handle) @io_handle.stub!(:read).and_return(@git_path + "\n") end - + it 'works without arguments' do lambda { git_changes_path }.should.not.raise(ArgumentError) end - + it 'does not allow arguments' do lambda { git_changes_path(:foo) }.should.raise(ArgumentError) end - + it 'returns the path to the .whiskey_disk_git_changes file in the git top-level path' do git_changes_path.should == File.join(@git_path, '.whiskey_disk_git_changes') end - + it 'returns the path to the .whiskey_disk_git_changes file in the current directory of the git top-level cannot be found' do @io_handle.stub!(:read).and_return('') - git_changes_path.should == File.join(Dir.pwd, '.whiskey_disk_git_changes') + git_changes_path.should == File.join(Dir.pwd, '.whiskey_disk_git_changes') end end @@ -423,21 +441,21 @@ def set_rsync_changes(changes) IO.stub!(:popen).with("git rev-parse --show-toplevel").and_return(@io_handle) @io_handle.stub!(:read).and_return(@rsync_path + "\n") end - + it 'works without arguments' do lambda { rsync_changes_path }.should.not.raise(ArgumentError) end - + it 'does not allow arguments' do lambda { rsync_changes_path(:foo) }.should.raise(ArgumentError) end - + it 'returns the path to the .whiskey_disk_rsync_changes file in the git top-level path' do rsync_changes_path.should == File.join(@rsync_path, '.whiskey_disk_rsync_changes') end - + it 'returns the path to the .whiskey_disk_rsync_changes file in the current directory of the git top-level cannot be found' do @io_handle.stub!(:read).and_return('') - rsync_changes_path.should == File.join(Dir.pwd, '.whiskey_disk_rsync_changes') + rsync_changes_path.should == File.join(Dir.pwd, '.whiskey_disk_rsync_changes') end -end \ No newline at end of file +end From 59cdc85ef9a84a890856bbb0a6ada4655ad932e7 Mon Sep 17 00:00:00 2001 From: Frank Koehl Date: Wed, 5 Nov 2014 10:47:13 -0500 Subject: [PATCH 2/3] Bad test --- spec/whiskey_disk/helpers_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/whiskey_disk/helpers_spec.rb b/spec/whiskey_disk/helpers_spec.rb index 539ee9b..362d1c7 100644 --- a/spec/whiskey_disk/helpers_spec.rb +++ b/spec/whiskey_disk/helpers_spec.rb @@ -49,11 +49,9 @@ it "returns true when given role is set, or no roles exist" do ENV['WD_ROLES'] = '' - nothing_or_role?.should.be.true nothing_or_role?('foo').should.be.true nothing_or_role?('web').should.be.true ENV['WD_ROLES'] = 'db:web' - nothing_or_role?.should.be.false nothing_or_role?('foo').should.be.false nothing_or_role?('web').should.be.true end From 119094567ddc3aec0cb1b4d978e03634f850317c Mon Sep 17 00:00:00 2001 From: Frank Koehl Date: Wed, 5 Nov 2014 12:07:16 -0500 Subject: [PATCH 3/3] Added check for nil state --- lib/whiskey_disk/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/whiskey_disk/helpers.rb b/lib/whiskey_disk/helpers.rb index 33565dd..4b19175 100644 --- a/lib/whiskey_disk/helpers.rb +++ b/lib/whiskey_disk/helpers.rb @@ -7,7 +7,7 @@ def role?(role) # does the current deployment have any role definitions? def no_roles? - ENV['WD_ROLES'] == '' + ENV['WD_ROLES'].nil? || ENV['WD_ROLES'] == '' end # look for a given role, or lack of all roles