Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers for deployments without roles #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/whiskey_disk/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'].nil? || 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
Expand Down Expand Up @@ -47,4 +57,4 @@ def git_changes_path

def rsync_changes_path
File.join(changes_file_root, '.whiskey_disk_rsync_changes')
end
end
120 changes: 68 additions & 52 deletions spec/whiskey_disk/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,26 +19,42 @@
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?('foo').should.be.true
nothing_or_role?('web').should.be.true
ENV['WD_ROLES'] = 'db:web'
nothing_or_role?('foo').should.be.false
nothing_or_role?('web').should.be.true
end
end

def set_git_changes(changes)
Expand All @@ -56,40 +72,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
Expand All @@ -101,29 +117,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])
Expand Down Expand Up @@ -153,32 +169,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"
Expand Down Expand Up @@ -329,27 +345,27 @@ 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
@contents
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)
Expand All @@ -363,27 +379,27 @@ 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
@contents
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)
Expand All @@ -397,22 +413,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

Expand All @@ -423,21 +439,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
end