duke 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cli do
4
+ before do
5
+ @cli = Cli.new
6
+ end
7
+
8
+ describe "#new" do
9
+ before do
10
+ Duke::Config.stub(:log_dir).and_return("log_dir")
11
+ Duke::Config.stub(:pid_dir).and_return("pid_dir")
12
+ FileUtils.stub(:mkdir_p)
13
+ FileUtils.stub(:mkdir_p)
14
+ @cli.stub(:directory)
15
+ end
16
+
17
+ context "with no argument" do
18
+ it "creates the config'd pid directory in the current directory" do
19
+ FileUtils.should_receive(:mkdir_p).with("./pid_dir")
20
+ @cli.new
21
+ end
22
+
23
+ it "creates the config'd log directory in the current directory" do
24
+ FileUtils.should_receive(:mkdir_p).with("./log_dir")
25
+ @cli.new
26
+ end
27
+
28
+ it "copies templates into the current directory" do
29
+ @cli.should_receive(:directory).with('templates', '.')
30
+ @cli.new
31
+ end
32
+ end
33
+
34
+ context "with a path argument" do
35
+ it "creates the config'd pid directory in the path" do
36
+ FileUtils.should_receive(:mkdir_p).with("/path/pid_dir")
37
+ @cli.new('/path')
38
+ end
39
+
40
+ it "creates the config'd log directory in the path" do
41
+ FileUtils.should_receive(:mkdir_p).with("/path/log_dir")
42
+ @cli.new('/path')
43
+ end
44
+
45
+ it "copies templates into the path" do
46
+ @cli.should_receive(:directory).with('templates', '/path')
47
+ @cli.new('/path')
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#add(repo_url)" do
53
+ it "creates a Project with repo_url" do
54
+ project = double("project", :repo_dir => 'repo_dir')
55
+ Project.should_receive(:create).with(:repo_url => "repo_url").and_return(project)
56
+ @cli.stub(:puts)
57
+ @cli.add("repo_url")
58
+ end
59
+ end
60
+
61
+ describe "#start(repo_dir, port)" do
62
+ before do
63
+ @project = double("project")
64
+ end
65
+
66
+ it "instantiates the Project with repo_dir" do
67
+ @project.stub(:start)
68
+ Project.should_receive(:find).with(:repo_dir => 'repo_dir').and_return(@project)
69
+ @cli.start('repo_dir', 4567)
70
+ end
71
+
72
+ it "starts the Project on the specified port" do
73
+ @project.should_receive(:start).with(4567)
74
+ Project.stub(:find).and_return(@project)
75
+ @cli.start('repo_dir', 4567)
76
+ end
77
+ end
78
+
79
+ describe "#stop(repo_dir)" do
80
+ before do
81
+ @project = double("project")
82
+ end
83
+
84
+ it "instantiates the Project with repo_dir" do
85
+ @project.stub(:stop)
86
+ Project.should_receive(:find).with(:repo_dir => 'repo_dir').and_return(@project)
87
+ @cli.stop('repo_dir')
88
+ end
89
+
90
+ it "stops the Project in repo_dir" do
91
+ @project.should_receive(:stop)
92
+ Project.stub(:find).and_return(@project)
93
+ @cli.stop('repo_dir')
94
+ end
95
+ end
96
+
97
+ describe "#list" do
98
+ it "calls print_status_msg on each Project" do
99
+ project = double("project")
100
+ project.should_receive(:print_status_msg)
101
+ Project.stub(:all).and_return([project])
102
+ @cli.list
103
+ end
104
+ end
105
+
106
+ describe "#cijoed(repo_dir, port, log_file, pid_file)" do
107
+ it "wraps the command in rvm exec" do
108
+ @cli.should_receive(:rvm_exec).with("repo_dir", "nohup cijoe -p 4567 repo_dir 1>log_file 2>&1 & echo $! > pid_file")
109
+ @cli.stub(:exec)
110
+ @cli.cijoed('repo_dir', 4567, 'log_file', 'pid_file')
111
+ end
112
+
113
+ it "execs the wrapped command" do
114
+ @cli.stub(:rvm_exec).and_return("rvm_exec")
115
+ @cli.should_receive(:exec).with("rvm_exec")
116
+ @cli.cijoed('repo_dir', 4567, 'log_file', 'pid_file')
117
+ end
118
+ end
119
+
120
+ describe "#build(repo_dir)" do
121
+ before do
122
+ @project = double("project")
123
+ end
124
+
125
+ it "instantiates the Project with repo_dir" do
126
+ @project.stub(:build)
127
+ Project.should_receive(:find).with(:repo_dir => 'repo_dir').and_return(@project)
128
+ @cli.build('repo_dir')
129
+ end
130
+
131
+ it "builds the Project in repo_dir" do
132
+ @project.should_receive(:build)
133
+ Project.stub(:find).and_return(@project)
134
+ @cli.build('repo_dir')
135
+ end
136
+ end
137
+
138
+ describe "#runner(repo_dir, cmd)" do
139
+ before do
140
+ @project = double("project")
141
+ end
142
+
143
+ it "finds the Project with repo_dir" do
144
+ @project.stub(:set_runner)
145
+ Project.should_receive(:find).with(:repo_dir => 'repo_dir').and_return(@project)
146
+ @cli.runner('repo_dir', 'cmd')
147
+ end
148
+
149
+ it "sets the Project runner to cmd" do
150
+ @project.should_receive(:set_runner).with('cmd')
151
+ Project.stub(:find).and_return(@project)
152
+ @cli.runner('repo_dir', 'cmd')
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe Controller do
4
+ before do
5
+ Duke::Config.stub(:log_dir).and_return("log_dir")
6
+ Duke::Config.stub(:pid_dir).and_return("pid_dir")
7
+ @controller = Controller.new('repo_dir', 4567)
8
+ @dir = Dir.pwd
9
+ end
10
+
11
+ describe ".pid_files" do
12
+ it "finds a list of all the pid files in the pid dir" do
13
+ Dir.should_receive(:[]).with("pid_dir/*.pid").and_return(['pid_file'])
14
+ Controller.pid_files.should == ['pid_file']
15
+ end
16
+ end
17
+
18
+ describe "#initialize(repo_dir, port)" do
19
+ context "with a repo_dir and a port" do
20
+ it "has a dir that matches cwd" do
21
+ @controller.dir.should == @dir
22
+ end
23
+
24
+ it "has a repo_dir" do
25
+ @controller.repo_dir.should == 'repo_dir'
26
+ end
27
+
28
+ it "has a timeout of 7 seconds" do
29
+ @controller.timeout.should == 7
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "#port" do
35
+ context "when a port is provided during initialization" do
36
+ it "is the provided port" do
37
+ @controller.port.should == 4567
38
+ end
39
+ end
40
+
41
+ context "when a port is not provided during initialization" do
42
+ it "is the current_port" do
43
+ @controller = Controller.new('repo_dir')
44
+ @controller.stub(:current_port).and_return(1234)
45
+ @controller.port.should == 1234
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#current_port" do
51
+ context "when a cijoe is running for repo_dir" do
52
+ it "determines what port cijoe is running on" do
53
+ Controller.stub(:pid_files).and_return(['repo_dir.1234.pid'])
54
+ @controller.current_port.should == 1234
55
+ end
56
+ end
57
+
58
+ context "when a cijoe is not running for repo_dir" do
59
+ it "returns nil" do
60
+ Controller.stub(:pid_files).and_return(['weird_dir.1234.pid'])
61
+ @controller.current_port.should == nil
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#identifier" do
67
+ it "identifies the controller via repo_dir and port" do
68
+ @controller.identifier.should == "repo_dir.4567"
69
+ end
70
+ end
71
+
72
+ describe "#pid_file" do
73
+ it "is the path to the pid file" do
74
+ @controller.pid_file.should == "#{@dir}/pid_dir/repo_dir.4567.pid"
75
+ end
76
+ end
77
+
78
+ describe "#log_file" do
79
+ it "is the path to the log file" do
80
+ @controller.log_file.should == "#{@dir}/log_dir/repo_dir.4567.log"
81
+ end
82
+ end
83
+
84
+ describe "#ping_command" do
85
+ it "verifies that cijoe is running" do
86
+ Duke::Config.stub(:host).and_return("localhost")
87
+ TCPSocket.should_receive(:new).with('localhost', 4567).and_return('sockette!')
88
+ @controller.ping_command.call.should == 'sockette!'
89
+ end
90
+ end
91
+
92
+ describe "#controller" do
93
+ it "instantiates a DaemonController" do
94
+ @controller.stub(:identifier).and_return("identifier")
95
+ @controller.stub(:ping_command).and_return("ping_command")
96
+ @controller.stub(:pid_file).and_return("pid_file")
97
+ @controller.stub(:log_file).and_return("log_file")
98
+ args = {
99
+ :identifier => "identifier",
100
+ :start_command => "duke cijoed repo_dir 4567 log_file pid_file",
101
+ :ping_command => "ping_command",
102
+ :pid_file => "pid_file",
103
+ :log_file => "log_file",
104
+ :log_file_activity_timeout => 7,
105
+ :start_timeout => 7
106
+ }
107
+ DaemonController.should_receive(:new).with(args).and_return("controller")
108
+ @controller.controller.should == "controller"
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,272 @@
1
+ require 'spec_helper'
2
+
3
+ describe Project do
4
+ before do
5
+ @project = Project.new(:repo_dir => 'repo_dir')
6
+ end
7
+
8
+ describe ".all" do
9
+ context "without a repo_dir in the current directory" do
10
+ it "does not contain a Project instance" do
11
+ Dir.stub(:[]).with('*').and_return(['non_repo'])
12
+ project = double("project", :repo_dir? => false)
13
+ Project.stub(:new).with(:repo_dir => 'non_repo').and_return(project)
14
+ Project.all.should be_empty
15
+ end
16
+ end
17
+
18
+ context "with a repo_dir in the current directory" do
19
+ it "contains the Project instance for repo_dir" do
20
+ Dir.stub(:[]).with('*').and_return(['repo_dir'])
21
+ project = double("project", :repo_dir? => true)
22
+ Project.stub(:new).with(:repo_dir => 'repo_dir').and_return(project)
23
+ Project.all.should == [project]
24
+ end
25
+ end
26
+ end
27
+
28
+ describe ".create" do
29
+ before do
30
+ @project = double("project", :clone => nil, :set_runner => nil, :set_campfire => nil)
31
+ Project.stub(:new).with(:repo_url => 'repo_url').and_return(@project)
32
+ end
33
+
34
+ it "instantiates a Project with repo_url" do
35
+ Project.create(:repo_url => 'repo_url').should == @project
36
+ end
37
+
38
+ it "clones the Project instance" do
39
+ @project.should_receive(:clone)
40
+ Project.create(:repo_url => 'repo_url')
41
+ end
42
+
43
+ it "sets the Project instance runner command from config" do
44
+ Duke::Config.stub(:runner).and_return('runner')
45
+ @project.should_receive(:set_runner).with('runner')
46
+ Project.create(:repo_url => 'repo_url')
47
+ end
48
+
49
+ it "sets the Project instance campfire from config" do
50
+ Duke::Config.stub(:campfire).and_return('campfire')
51
+ @project.should_receive(:set_campfire).with('campfire')
52
+ Project.create(:repo_url => 'repo_url')
53
+ end
54
+ end
55
+
56
+ describe "#initialize(opts)" do
57
+ context "when opts contains a repo_url" do
58
+ before do
59
+ repo_url = 'repo_url'
60
+ repo_url.stub(:repo_dir).and_return('repo_dir')
61
+ @project = Project.new(:repo_url => repo_url)
62
+ end
63
+
64
+ it "has a repo_url" do
65
+ @project.repo_url.should == 'repo_url'
66
+ end
67
+
68
+ it "has a repo_dir" do
69
+ @project.repo_dir.should == 'repo_dir'
70
+ end
71
+ end
72
+
73
+ context "when opts contains a repo_dir" do
74
+ it "does not have a repo_url" do
75
+ @project.repo_url.should be_nil
76
+ end
77
+
78
+ it "has a repo_dir" do
79
+ @project.repo_dir.should == 'repo_dir'
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "#controller" do
85
+ it "instantiates a Controller with repo_dir" do
86
+ Controller.stub(:new).with('repo_dir').and_return('controller')
87
+ @project.controller.should == 'controller'
88
+ end
89
+ end
90
+
91
+ describe "#start(port)" do
92
+ before do
93
+ @controller = double("controller")
94
+ end
95
+
96
+ it "instantiates a Controller with repo_dir and port" do
97
+ @controller.stub(:start)
98
+ Controller.should_receive(:new).with('repo_dir', 4567).and_return(@controller)
99
+ @project.start(4567)
100
+ @project.controller.should == @controller
101
+ end
102
+
103
+ it "starts the new controller" do
104
+ @controller.should_receive(:start)
105
+ Controller.stub(:new).and_return(@controller)
106
+ @project.start(4567)
107
+ end
108
+ end
109
+
110
+ describe "#git_dir" do
111
+ it "is the path to the git config dir" do
112
+ @project.git_dir.should == "repo_dir/.git"
113
+ end
114
+ end
115
+
116
+ describe "#repo_dir?" do
117
+ it "determines if repo_dir is an actual git repo" do
118
+ @project.stub(:git_dir).and_return('git_dir')
119
+ File.should_receive(:exist?).with('git_dir').and_return(true)
120
+ @project.repo_dir?.should == true
121
+ end
122
+ end
123
+
124
+ describe "#clone" do
125
+ it "clones the repo_url" do
126
+ project = Project.new(:repo_url => 'repo_url')
127
+ project.should_receive(:run).with("git clone repo_url")
128
+ project.clone
129
+ end
130
+ end
131
+
132
+ describe "#set_config(key, value)" do
133
+ it "adds a key value pair to the git config" do
134
+ @project.stub(:inside).with('repo_dir').and_yield
135
+ @project.should_receive(:run).with("git config --unset-all \"key\"")
136
+ @project.should_receive(:run).with("git config \"key\" \"value\"")
137
+ @project.set_config('key', 'value')
138
+ end
139
+ end
140
+
141
+ describe "#set_runner" do
142
+ it "sets the runner git config entry" do
143
+ @project.should_receive(:set_config).with("cijoe.runner", 'runner')
144
+ @project.set_runner('runner')
145
+ end
146
+ end
147
+
148
+ describe "#set_campfire" do
149
+ it "sets campfire values in the git config" do
150
+ @project.should_receive(:set_config).with("campfire.camp", 'fire')
151
+ @project.should_receive(:set_config).with("campfire.fire", 'camp')
152
+ @project.set_campfire({:camp => 'fire', :fire => 'camp'})
153
+ end
154
+ end
155
+
156
+ describe "#print_status_msg" do
157
+ context "when cijoe is not running" do
158
+ it "puts the repo_dir and indicates cijoe is stopped" do
159
+ @project.stub(:running?).and_return(false)
160
+ @project.should_receive(:puts).with("repo_dir, stopped")
161
+ @project.print_status_msg
162
+ end
163
+ end
164
+
165
+ context "when cijoe is running" do
166
+ before do
167
+ @project.stub(:running?).and_return(true)
168
+ @project.stub(:port).and_return(4567)
169
+ @project.stub(:pid).and_return(666)
170
+ end
171
+
172
+ context "and has yet to be built" do
173
+ it "puts the repo_dir, port and pid of cijoe" do
174
+ @project.stub(:building?).and_return(false)
175
+ @project.stub(:built?).and_return(false)
176
+ @project.should_receive(:puts).with("repo_dir, port 4567, pid 666")
177
+ @project.print_status_msg
178
+ end
179
+ end
180
+
181
+ context "and is building" do
182
+ it "puts the repo_dir, port, pid and a building status" do
183
+ @project.stub(:building?).and_return(true)
184
+ @project.should_receive(:puts).with("repo_dir, port 4567, pid 666, building...")
185
+ @project.print_status_msg
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ describe "#build" do
192
+ it "sends a POST to cijoe in order to start a build" do
193
+ Duke::Config.stub(:host).and_return('localhost')
194
+ URI.stub(:parse).with("http://localhost:4567/").and_return('uri')
195
+ Net::HTTP.should_receive(:post_form).with('uri', {})
196
+ @project.stub(:port).and_return(4567)
197
+ @project.build
198
+ end
199
+ end
200
+
201
+ describe "#find" do
202
+ context "searching for a project that exists" do
203
+ it "instantiates a Project for the given repo_dir" do
204
+ project = double("project", :repo_dir? => true)
205
+ Project.should_receive(:new).with(:repo_dir => 'repo_dir').and_return(project)
206
+ Project.find(:repo_dir => 'repo_dir').should == project
207
+ end
208
+ end
209
+ end
210
+
211
+ describe "#cijoe" do
212
+ context "when project has a repo_dir?" do
213
+ it "instantiates a CIJoe with repo_dir" do
214
+ @project.stub(:repo_dir?).and_return(true)
215
+ cijoe = double("cijoe", :restore => nil)
216
+ CIJoe.should_receive(:new).with('repo_dir').and_return(cijoe)
217
+ @project.cijoe.should == cijoe
218
+ end
219
+ end
220
+
221
+ context "when project does not have a repo_dir?" do
222
+ it "returns nil" do
223
+ @project.stub(:repo_dir?).and_return(false)
224
+ @project.cijoe.should be_nil
225
+ end
226
+ end
227
+ end
228
+
229
+ describe "#built?" do
230
+ context "when cijoe.last_build is nil" do
231
+ it "is false" do
232
+ @project.stub(:cijoe).and_return(double("cijoe", :last_build => nil))
233
+ @project.built?.should be_false
234
+ end
235
+ end
236
+
237
+ context "when cijoe.last_build is not nil" do
238
+ it "is true" do
239
+ @project.stub(:cijoe).and_return(double("cijoe", :last_build => true))
240
+ @project.built?.should be_true
241
+ end
242
+ end
243
+ end
244
+
245
+ describe "#passing?" do
246
+ context "when cijoe.last_build.failed? is true" do
247
+ it "is false" do
248
+ last_build = double("last_build", :failed? => true)
249
+ cijoe = double("cijoe", :last_build => last_build)
250
+ @project.stub(:cijoe).and_return(cijoe)
251
+ @project.passing?.should be_false
252
+ end
253
+ end
254
+
255
+ context "when cijoe.last_build.failed? is false" do
256
+ it "is true" do
257
+ last_build = double("last_build", :failed? => false)
258
+ cijoe = double("cijoe", :last_build => last_build)
259
+ @project.stub(:cijoe).and_return(cijoe)
260
+ @project.passing?.should be_true
261
+ end
262
+ end
263
+ end
264
+
265
+ describe "#url" do
266
+ it "contains the configured host along with the cijoe port" do
267
+ @project.stub(:port).and_return(4567)
268
+ Duke::Config.stub(:host).and_return('localhost')
269
+ @project.url.should == 'http://localhost:4567'
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rspec'
5
+
6
+ unless ENV['SIMPLECOV'].nil?
7
+ require 'simplecov'
8
+ SimpleCov.start do
9
+ add_filter '/spec/'
10
+ end
11
+ end
12
+
13
+ require 'duke'
14
+ include Duke
15
+
16
+ Rspec.configure do |c|
17
+ c.mock_with :rspec
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'duke'
2
+
3
+ Duke::App.configure do |config|
4
+ config.set :app_file, __FILE__
5
+ config.set :show_exceptions, true
6
+ config.set :method_override, true
7
+ end
8
+
9
+ run Duke::App
File without changes