borg-rb 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,87 +2,123 @@ require 'spec_helper'
2
2
  require 'borg/cli/applications'
3
3
 
4
4
  describe Borg::CLI::Applications do
5
- before :each do
6
- @config = double("config1")
7
- @files = %w{cap/applications/app1.rb cap/applications/app1/stg1.rb cap/applications/app1/stg2.rb}
8
- Dir.stub("[]").and_return(@files)
9
- @cli = Capistrano::CLI.new([])
5
+ before :all do
6
+ class MockCLI < Capistrano::CLI
7
+ include Borg::CLI::Applications
8
+ end
9
+ end
10
+
11
+ after :all do
12
+ Object.send(:remove_const, :MockCLI)
13
+ end
14
+
15
+ let(:app_config) { <<-RUBY.gsub(/^ {4}/, '')
16
+ application :app1 do
17
+ puts 'application = app1'
18
+ end
19
+
20
+ stage :app1, :prd do
21
+ puts 'stage = prd'
22
+ end
23
+
24
+ stage :app1, :stg do
25
+ puts 'stage = stg'
26
+ end
27
+
28
+ stage :app1, :alf do
29
+ puts 'stage = alf'
30
+ end
31
+ RUBY
32
+ }
33
+
34
+ before :all do
35
+ @env = Support::IsolatedEnvironment.new
36
+ @env.create_file('cap/applications/app.rb', app_config)
37
+ @app_config_path = @env.workdir_path.join('cap/applications/app.rb')
38
+ @files = [@app_config_path]
39
+ end
40
+
41
+ after :all do
42
+ @env.close
10
43
  end
11
44
 
12
- context "#load_applications when called" do
45
+ before do
46
+ Dir.stub('[]').and_return(@files)
47
+ @cli = MockCLI.new([])
48
+ @config = double('config1')
49
+ end
50
+
51
+ describe '#load_applications' do
13
52
  before do
14
53
  @files.each { |f| @config.should_receive(:load).with(f) }
15
54
  end
16
55
 
17
- it "should load all files in ./cap/applications/**/*.rb" do
56
+ it 'loads files in ./cap/applications/**/*.rb' do
18
57
  @cli.send :load_applications, @config
19
58
  end
20
59
 
21
- context "with a second call" do
60
+ context 'when already invoked in the past' do
22
61
  before do
23
62
  @cli.send :load_applications, @config
24
- @config2 = double("config2")
25
- @files.each { |f| @config2.should_not_receive(:load).with(f) }
26
63
  end
27
64
 
28
- it "should not load all files in ./cap/applications/**/*.rb for further calls" do
65
+ it 'does not load files in ./cap/applications/**/*.rb anymore' do
66
+ @config2 = double('config2')
29
67
  @cli.send :load_applications, @config2
68
+ @files.each { |f| @config2.should_not_receive(:load).with(f) }
30
69
  end
31
70
  end
32
71
  end
33
72
 
34
- context "#separate_actions_and_applications when called" do
73
+ describe '#separate_actions_and_applications' do
35
74
  before do
36
75
  @cli.instance_eval do
37
76
  @options = {}
38
77
  @options[:actions] = %w{app1 app2:stg1 app2:stg3 test test2}
39
78
  end
40
- @config.stub(:applications).and_return({
41
- app1: double("app1"),
42
- app2: double("app2")
43
- })
79
+ @config.stub(:applications).
80
+ and_return({app1: double('app1'), app2: double('app2')})
44
81
  @config.applications[:app1].stub(:stages).and_return({})
45
- @config.applications[:app2].stub(:stages).and_return({
46
- stg1: double("stg1"),
47
- stg2: double("stg2")
48
- })
82
+ @config.applications[:app2].stub(:stages).
83
+ and_return({stg1: double('stg1'),stg2: double('stg2')})
49
84
  end
50
- it "should remove all applications from actions" do
85
+ it 'removes all applications from actions' do
51
86
  @cli.send :separate_actions_and_applications, @config
52
- @cli.options[:actions].should == %w{app2:stg3 test test2}
53
- @cli.options[:applications].should == [
54
- @config.applications[:app1],
55
- @config.applications[:app2].stages[:stg1]
56
- ]
87
+ expect(@cli.options[:actions]).to eq %w{app2:stg3 test test2}
88
+ expect(@cli.options[:applications]).to eq [
89
+ @config.applications[:app1],
90
+ @config.applications[:app2].stages[:stg1]
91
+ ]
57
92
  end
58
93
 
59
- it "should queue both stages if app2" do
94
+ it 'queues both stages if app2' do
60
95
  @cli.instance_eval do
61
96
  @options[:actions] = %w{app1 app2 test test2}
62
97
  end
63
98
 
64
99
  @cli.send :separate_actions_and_applications, @config
65
100
 
66
- @cli.options[:actions].should == %w{test test2}
67
- @cli.options[:applications].should == [
68
- @config.applications[:app1],
69
- @config.applications[:app2].stages[:stg1],
70
- @config.applications[:app2].stages[:stg2]
71
- ]
101
+ expect(@cli.options[:actions]).to eq %w{test test2}
102
+ expect(@cli.options[:applications]).to eq [
103
+ @config.applications[:app1],
104
+ @config.applications[:app2].stages[:stg1],
105
+ @config.applications[:app2].stages[:stg2]
106
+ ]
72
107
  end
73
108
 
74
- it "should raise exception if configs are not isolated to start of the actions list" do
109
+ it 'raises an exception when configs are not isolated to start of the actions list' do
75
110
  @cli.instance_eval do
76
- @options[:actions] << "app2:stg2"
111
+ @options[:actions] << 'app2:stg2'
77
112
  end
78
- expect{ @cli.send(:separate_actions_and_applications, @config)}.to raise_error(ArgumentError, "Can not have non application configs between application configs")
113
+ expect{
114
+ @cli.send(:separate_actions_and_applications, @config)
115
+ }.to raise_error(ArgumentError, 'Can not have non application configs between application configs')
79
116
  end
80
117
 
81
118
  end
82
119
 
83
- context "#execute_requested_actions_with_applications when called" do
120
+ describe '#execute_requested_actions_with_applications' do
84
121
  before do
85
-
86
122
  @cli.instance_eval do
87
123
  @apps_loaded = true
88
124
  @options = {}
@@ -90,27 +126,25 @@ describe Borg::CLI::Applications do
90
126
  @options[:applications] = []
91
127
  end
92
128
  @config.stub(:applications).and_return({})
93
-
94
129
  @cli.stub(:execute_requested_actions_without_applications)
95
-
96
130
  end
97
131
 
98
- it "should load applications" do
132
+ it 'loads applications' do
99
133
  @cli.should_receive(:load_applications).with(@config)
100
134
  @cli.execute_requested_actions_with_applications @config
101
135
  end
102
136
 
103
- it "should separate actions and applications" do
137
+ it 'separates actions and applications' do
104
138
  @cli.should_receive(:separate_actions_and_applications).with(@config)
105
139
  @cli.execute_requested_actions_with_applications @config
106
140
  end
107
141
 
108
- it "should call #execute_requested_actions_without_applications when there is an empty applications list" do
142
+ it 'calls #execute_requested_actions_without_applications when there is an empty applications list' do
109
143
  @cli.should_receive(:execute_requested_actions_without_applications).with(@config)
110
144
  @cli.execute_requested_actions_with_applications @config
111
145
  end
112
146
 
113
- context "when applications exist" do
147
+ context 'when applications exist' do
114
148
  before do
115
149
  apps = [double(:app1), double(:app2)]
116
150
  apps[0].stub(:name).and_return(:app1)
@@ -125,23 +159,23 @@ describe Borg::CLI::Applications do
125
159
  Thread.current[:borg_application] = nil
126
160
  end
127
161
 
128
- it "should call call execute! for each application" do
162
+ it 'calls execute! for each application' do
129
163
  @cli.stub(:load_applications)
130
164
  @cli.stub(:separate_actions_and_applications)
131
165
  @cli.should_receive(:execute!).with().twice
132
166
  @cli.execute_requested_actions_with_applications @config
133
167
  end
134
168
 
135
- it "should set Thread's borg_application" do
169
+ it 'sets Thread.current[:borg_application]' do
136
170
  @cli.stub(:load_applications)
137
171
  @cli.stub(:separate_actions_and_applications)
138
172
  @cli.stub(:execute!) do
139
- @cli.options[:applications].should include Thread.current[:borg_application]
173
+ expect(@cli.options[:applications]).to include Thread.current[:borg_application]
140
174
  end
141
175
  @cli.execute_requested_actions_with_applications @config
142
176
  end
143
177
 
144
- it "should call #execute_requested_actions_without_applications when Thread's borg_application is set" do
178
+ it 'calls #execute_requested_actions_without_applications when Thread.current[:borg_application] is set' do
145
179
  Thread.current[:borg_application] = @cli.options[:applications][0]
146
180
  @cli.stub(:load_applications)
147
181
  @cli.stub(:separate_actions_and_applications)
@@ -150,7 +184,7 @@ describe Borg::CLI::Applications do
150
184
  @cli.execute_requested_actions_with_applications @config
151
185
  end
152
186
 
153
- it "should load the application's config when Thread's borg_application is set" do
187
+ it 'loads the application config when Thread.current[:borg_application] is set' do
154
188
  Thread.current[:borg_application] = @cli.options[:applications][0]
155
189
  @cli.stub(:load_applications)
156
190
  @cli.stub(:separate_actions_and_applications)
@@ -158,7 +192,6 @@ describe Borg::CLI::Applications do
158
192
  @cli.options[:applications][0].should_receive(:load_into)
159
193
  @cli.execute_requested_actions_with_applications @config
160
194
  end
161
-
162
195
  end
163
196
  end
164
197
  end
@@ -0,0 +1,187 @@
1
+ require 'spec_helper'
2
+ require 'borg/cli'
3
+
4
+ describe Borg::CLI do
5
+ let(:app_config) { <<-RUBY.gsub(/^ {4}/, '')
6
+ application :app1 do
7
+ puts 'application = app1'
8
+ end
9
+
10
+ stage :app1, :prd do
11
+ puts 'stage = prd'
12
+ end
13
+
14
+ stage :app1, :stg do
15
+ puts 'stage = stg'
16
+ end
17
+
18
+ stage :app1, :alf do
19
+ puts 'stage = alf'
20
+ end
21
+ RUBY
22
+ }
23
+
24
+ before :all do
25
+ @env = Support::IsolatedEnvironment.new
26
+ @env.create_file('cap/applications/app.rb', app_config)
27
+ @app_config_path = @env.workdir_path.join('cap/applications/app.rb')
28
+ @files = [@app_config_path]
29
+ end
30
+
31
+ after :all do
32
+ @env.close
33
+ end
34
+
35
+ before do
36
+ Dir.stub('[]').and_return(@files)
37
+ @cli = Borg::CLI.new([])
38
+ @config = double('config1')
39
+ end
40
+
41
+ describe '#load_applications' do
42
+ before do
43
+ @files.each { |f| @config.should_receive(:load).with(f) }
44
+ end
45
+
46
+ it 'loads files in ./cap/applications/**/*.rb' do
47
+ @cli.send :load_applications, @config
48
+ end
49
+
50
+ context 'when already invoked in the past' do
51
+ before do
52
+ @cli.send :load_applications, @config
53
+ end
54
+
55
+ it 'does not load files in ./cap/applications/**/*.rb anymore' do
56
+ @config2 = double('config2')
57
+ @cli.send :load_applications, @config2
58
+ @files.each { |f| @config2.should_not_receive(:load).with(f) }
59
+ end
60
+ end
61
+ end
62
+
63
+ describe '#separate_actions_and_applications' do
64
+ before do
65
+ @cli.instance_eval do
66
+ @options = {}
67
+ @options[:actions] = %w{app1 app2:stg1 app2:stg3 test test2}
68
+ end
69
+ @config.stub(:applications).
70
+ and_return({app1: double('app1'), app2: double('app2')})
71
+ @config.applications[:app1].stub(:stages).and_return({})
72
+ @config.applications[:app2].stub(:stages).
73
+ and_return({stg1: double('stg1'),stg2: double('stg2')})
74
+ end
75
+ it 'removes all applications from actions' do
76
+ @cli.send :separate_actions_and_applications, @config
77
+ expect(@cli.options[:actions]).to eq %w{app2:stg3 test test2}
78
+ expect(@cli.options[:applications]).to eq [
79
+ @config.applications[:app1],
80
+ @config.applications[:app2].stages[:stg1]
81
+ ]
82
+ end
83
+
84
+ it 'queues both stages if app2' do
85
+ @cli.instance_eval do
86
+ @options[:actions] = %w{app1 app2 test test2}
87
+ end
88
+
89
+ @cli.send :separate_actions_and_applications, @config
90
+
91
+ expect(@cli.options[:actions]).to eq %w{test test2}
92
+ expect(@cli.options[:applications]).to eq [
93
+ @config.applications[:app1],
94
+ @config.applications[:app2].stages[:stg1],
95
+ @config.applications[:app2].stages[:stg2]
96
+ ]
97
+ end
98
+
99
+ it 'raises an exception when configs are not isolated to start of the actions list' do
100
+ @cli.instance_eval do
101
+ @options[:actions] << 'app2:stg2'
102
+ end
103
+ expect{
104
+ @cli.send(:separate_actions_and_applications, @config)
105
+ }.to raise_error(ArgumentError, 'Can not have non application configs between application configs')
106
+ end
107
+
108
+ end
109
+
110
+ describe '#execute_requested_actions_with_applications' do
111
+ before do
112
+ @cli.instance_eval do
113
+ @apps_loaded = true
114
+ @options = {}
115
+ @options[:actions] = %w{app1 app2:stg1 app2:stg3 test test2}
116
+ @options[:applications] = []
117
+ end
118
+ @config.stub(:applications).and_return({})
119
+ @cli.stub(:execute_requested_actions_without_applications)
120
+ end
121
+
122
+ it 'loads applications' do
123
+ @cli.should_receive(:load_applications).with(@config)
124
+ @cli.execute_requested_actions_with_applications @config
125
+ end
126
+
127
+ it 'separates actions and applications' do
128
+ @cli.should_receive(:separate_actions_and_applications).with(@config)
129
+ @cli.execute_requested_actions_with_applications @config
130
+ end
131
+
132
+ it 'calls #execute_requested_actions_without_applications when there is an empty applications list' do
133
+ @cli.should_receive(:execute_requested_actions_without_applications).with(@config)
134
+ @cli.execute_requested_actions_with_applications @config
135
+ end
136
+
137
+ context 'when applications exist' do
138
+ before do
139
+ apps = [double(:app1), double(:app2)]
140
+ apps[0].stub(:name).and_return(:app1)
141
+ apps[1].stub(:name).and_return(:app2)
142
+ @cli.instance_eval do
143
+ @options[:applications] = apps
144
+ end
145
+ @cli.stub(:puts)
146
+ end
147
+
148
+ after do
149
+ Thread.current[:borg_application] = nil
150
+ end
151
+
152
+ it 'calls execute! for each application' do
153
+ @cli.stub(:load_applications)
154
+ @cli.stub(:separate_actions_and_applications)
155
+ @cli.should_receive(:execute!).with().twice
156
+ @cli.execute_requested_actions_with_applications @config
157
+ end
158
+
159
+ it 'sets Thread.current[:borg_application]' do
160
+ @cli.stub(:load_applications)
161
+ @cli.stub(:separate_actions_and_applications)
162
+ @cli.stub(:execute!) do
163
+ expect(@cli.options[:applications]).to include Thread.current[:borg_application]
164
+ end
165
+ @cli.execute_requested_actions_with_applications @config
166
+ end
167
+
168
+ it 'calls #execute_requested_actions_without_applications when Thread.current[:borg_application] is set' do
169
+ Thread.current[:borg_application] = @cli.options[:applications][0]
170
+ @cli.stub(:load_applications)
171
+ @cli.stub(:separate_actions_and_applications)
172
+ @cli.should_receive(:execute_requested_actions_without_applications).with(@config)
173
+ @cli.options[:applications][0].stub(:load_into)
174
+ @cli.execute_requested_actions_with_applications @config
175
+ end
176
+
177
+ it 'loads the application config when Thread.current[:borg_application] is set' do
178
+ Thread.current[:borg_application] = @cli.options[:applications][0]
179
+ @cli.stub(:load_applications)
180
+ @cli.stub(:separate_actions_and_applications)
181
+ @cli.stub(:execute_requested_actions_without_applications)
182
+ @cli.options[:applications][0].should_receive(:load_into)
183
+ @cli.execute_requested_actions_with_applications @config
184
+ end
185
+ end
186
+ end
187
+ end
@@ -2,66 +2,63 @@ require 'spec_helper'
2
2
  require 'borg/configuration/applications'
3
3
 
4
4
  describe Borg::Configuration::Applications do
5
- it "should be included into Capistrano::Configuration" do
6
- Capistrano::Configuration.ancestors.should include Borg::Configuration::Applications
5
+ before :all do
6
+ class MockConfiguration < Capistrano::Configuration
7
+ include Borg::Configuration::Applications
8
+ end
7
9
  end
8
10
 
9
- context "when a new Capistrano::Configuration is initialized" do
10
- subject { Capistrano::Configuration.new }
11
- it "should initialize the applications hash" do
12
- expect(subject.applications).to eq({})
13
- end
11
+ after :all do
12
+ Object.send(:remove_const, :MockConfiguration)
13
+ end
14
+
15
+ let(:config) { MockConfiguration.new }
16
+
17
+ it 'initializes the applications hash' do
18
+ expect(config.applications.class).to eq Hash
14
19
  end
15
20
 
16
- context "when an applications is defined" do
21
+ context 'when applications are defined' do
17
22
  before do
18
- @config = Capistrano::Configuration.new
19
- @config.load do
20
- application "app1" do
21
- test_notice "You have called app1"
23
+ config.load do
24
+ application 'app1' do
25
+ test_notice 'You have called app1'
26
+
22
27
  end
23
28
  end
24
29
  end
25
- subject { @config }
26
30
 
27
- it "should symobolize the name" do
28
- expect(subject.applications[:app1].name).to eq :app1
29
- end
30
-
31
- it "should create a namespace app1 with task default and a description" do
32
- expect(subject.namespaces[:app1]).to be_true
33
- expect(subject.namespaces[:app1].tasks[:default]).to be_true
34
- expect(subject.namespaces[:app1].tasks[:default].desc).to be_true
35
- end
31
+ it_behaves_like 'an application configuration'
36
32
 
37
- it "should add it to applications hash and have a block" do
38
- expect(subject.applications[:app1].class).to eq Borg::Configuration::Applications::Application
39
- expect(subject.applications[:app1].execution_blocks.count).to eq 1
33
+ it 'creates an execution block for the application' do
34
+ expect(config.applications[:app1].execution_blocks.count).to eq 1
40
35
  end
41
36
  end
37
+
42
38
  end
43
39
 
44
40
  describe Borg::Configuration::Applications::Application do
45
- it "should be initialize all variables" do
46
- app1 = Borg::Configuration::Applications::Application.new(:app1, double("namespace app1"))
41
+ it 'initializes: stage, name, and execution_blocks' do
42
+ app1 = Borg::Configuration::Applications::Application.new(:app1, double('namespace app1'))
47
43
  expect(app1.stages).to eq({})
48
44
  expect(app1.name).to eq(:app1)
49
45
  expect(app1.execution_blocks).to eq([])
50
46
  end
51
47
 
52
- context "an applications with 2 blocks" do
48
+ context 'an applications with 2 blocks' do
53
49
  before do
54
- @app1 = Borg::Configuration::Applications::Application.new(:app1, double("namespace app1"))
50
+ @app1 = Borg::Configuration::Applications::Application.new(:app1, double('namespace app1'))
55
51
  @app1.execution_blocks << -> {
56
- raise "block 1 called"
52
+ raise 'block 1 called'
57
53
  }
58
54
  @app1.execution_blocks << -> {
59
- raise "block 2 called"
55
+ raise 'block 2 called'
60
56
  }
61
57
  end
62
- context "when #load_into is called" do
63
- it "should all blocks into the provided config" do
64
- config = double("test config")
58
+
59
+ context 'when #load_into is called' do
60
+ it 'loads all blocks into the provided config' do
61
+ config = double('test config')
65
62
  config.should_receive(:load).twice
66
63
  @app1.load_into config
67
64
  end