1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

fix 'filter' variable so it works as documented

The variable ':filter' is documented as requiring the keys 'hosts' and
'roles' to set Host and Role filters. In actual fact it uses 'host' and
'role'. This commit adds support and tests for both.

Fixes #1457
This commit is contained in:
Nick Townsend 2015-07-16 16:03:20 -07:00
parent 0d2c542fd1
commit 368ee4fca8
2 changed files with 67 additions and 0 deletions

View file

@ -100,6 +100,8 @@ module Capistrano
@filters << Filter.new(:role, ENV['ROLES']) if ENV['ROLES']
@filters << Filter.new(:host, ENV['HOSTS']) if ENV['HOSTS']
fh = fetch_for(:filter,{}) || {}
@filters << Filter.new(:host, fh[:hosts]) if fh[:hosts]
@filters << Filter.new(:role, fh[:roles]) if fh[:roles]
@filters << Filter.new(:host, fh[:host]) if fh[:host]
@filters << Filter.new(:role, fh[:role]) if fh[:role]
end

View file

@ -132,6 +132,38 @@ describe Capistrano::DSL do
end
end
describe 'setting an internal hosts filter' do
subject { dsl.roles(:app) }
it 'is ignored' do
dsl.set :filter, { hosts: 'example3.com' }
expect(subject.map(&:hostname)).to eq(['example3.com', 'example4.com'])
end
end
describe 'setting an internal roles filter' do
subject { dsl.roles(:app) }
it 'ignores it' do
dsl.set :filter, { roles: :web }
expect(subject.map(&:hostname)).to eq(['example3.com','example4.com'])
end
end
describe 'setting an internal hosts and roles filter' do
subject { dsl.roles(:app) }
it 'ignores it' do
dsl.set :filter, { roles: :web, hosts: 'example1.com' }
expect(subject.map(&:hostname)).to eq(['example3.com','example4.com'])
end
end
describe 'setting an internal regexp hosts filter' do
subject { dsl.roles(:all) }
it 'is ignored' do
dsl.set :filter, { hosts: /1/ }
expect(subject.map(&:hostname)).to eq(%w{example1.com example2.com example3.com example4.com example5.com})
end
end
end
describe 'when defining role with reserved name' do
@ -496,6 +528,21 @@ describe Capistrano::DSL do
dsl.on(all)
end
it 'filters by roles from the :filter variable' do
hosts = dsl.roles(:web)
all = dsl.roles(:all)
SSHKit::Coordinator.expects(:new).with(hosts).returns(@coordinator)
dsl.set :filter, { roles: 'web' }
dsl.on(all)
end
it 'filters by hosts and roles from the :filter variable' do
all = dsl.roles(:all)
SSHKit::Coordinator.expects(:new).with([]).returns(@coordinator)
dsl.set :filter, { roles: 'db', hosts: 'example3.com' }
dsl.on(all)
end
it 'filters from ENV[ROLES]' do
hosts = dsl.roles(:db)
all = dsl.roles(:all)
@ -549,6 +596,24 @@ describe Capistrano::DSL do
dsl.on('server.local')
end
it "selects nothing when a roles filter is present" do
dsl.set :filter, { roles: 'web' }
SSHKit::Coordinator.expects(:new).with([]).returns(@coordinator)
dsl.on('my.server')
end
it "selects using the string when a hosts filter is present" do
dsl.set :filter, { hosts: 'server.local' }
SSHKit::Coordinator.expects(:new).with(['server.local']).returns(@coordinator)
dsl.on('server.local')
end
it "doesn't select when a hosts filter is present that doesn't match" do
dsl.set :filter, { hosts: 'ruby.local' }
SSHKit::Coordinator.expects(:new).with([]).returns(@coordinator)
dsl.on('server.local')
end
end
end