Skip to content

Commit e294982

Browse files
authored
DSL tests (#695)
* Turn on coverage by branch and exclude spec folder * Refactor tests so that they don't share state * Refactor rake example group using rspec shared context * Fix how tasks get reset * Invoke reset! after each spec * Add DSL tests * Add gemfile.lock to version control
1 parent e63380d commit e294982

25 files changed

+304
-90
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ test_env/deploy
33
.rvmrc
44
.ruby-gemset
55
.ruby-version
6-
Gemfile.lock
76
pkg/
87
support/helpers.md
98
support/modules.md

Gemfile.lock

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
PATH
2+
remote: .
3+
specs:
4+
mina (1.2.4)
5+
rake
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
awesome_print (1.9.2)
11+
codeclimate-test-reporter (1.0.7)
12+
simplecov
13+
coderay (1.1.3)
14+
diff-lcs (1.4.4)
15+
docile (1.4.0)
16+
method_source (1.0.0)
17+
pry (0.13.1)
18+
coderay (~> 1.1)
19+
method_source (~> 1.0)
20+
rake (13.0.6)
21+
rspec (3.5.0)
22+
rspec-core (~> 3.5.0)
23+
rspec-expectations (~> 3.5.0)
24+
rspec-mocks (~> 3.5.0)
25+
rspec-core (3.5.4)
26+
rspec-support (~> 3.5.0)
27+
rspec-expectations (3.5.0)
28+
diff-lcs (>= 1.2.0, < 2.0)
29+
rspec-support (~> 3.5.0)
30+
rspec-mocks (3.5.0)
31+
diff-lcs (>= 1.2.0, < 2.0)
32+
rspec-support (~> 3.5.0)
33+
rspec-support (3.5.0)
34+
simplecov (0.21.2)
35+
docile (~> 1.1)
36+
simplecov-html (~> 0.11)
37+
simplecov_json_formatter (~> 0.1)
38+
simplecov-html (0.12.3)
39+
simplecov_json_formatter (0.1.3)
40+
41+
PLATFORMS
42+
ruby
43+
44+
DEPENDENCIES
45+
awesome_print
46+
codeclimate-test-reporter
47+
mina!
48+
pry
49+
rspec (~> 3.5.0)
50+
simplecov
51+
52+
BUNDLED WITH
53+
2.2.25

spec/configs/default.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set :simulate, true
2+
set :domain, 'localhost'
3+
set :deploy_to, "#{Dir.pwd}/deploy"
4+
set :repository, "#{Mina.root_path}"
5+
set :shared_paths, ['config/database.yml', 'log']
6+
set :keep_releases, 2

spec/configs/dsl.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
task :reset_task do
2+
command "echo Hello there"
3+
reset!
4+
command "echo New command"
5+
end
6+
7+
task :one_run_inside_another do
8+
run :local do
9+
run :remote do
10+
puts "I shouldn't execute"
11+
end
12+
end
13+
end
14+
15+
task :local_run do
16+
run :local do
17+
comment "I am a comment"
18+
command "echo Hello there"
19+
end
20+
end
21+
22+
task :remote_run do
23+
run :remote do
24+
comment "I am a comment"
25+
command "echo Hello there"
26+
end
27+
end
28+
29+
task :nonexistent_run do
30+
run :nonexistent do
31+
comment "I shouldn't run"
32+
end
33+
end
34+
35+
task :on_stage_task do
36+
deploy do
37+
on :launch do
38+
command "echo Hello there"
39+
end
40+
end
41+
end
42+
43+
task :in_path_task do
44+
in_path '/path' do
45+
command "echo Hello there"
46+
end
47+
end
48+
49+
task :deploy_block_task do
50+
deploy do
51+
command "echo Hello there"
52+
end
53+
end

spec/lib/mina/application_spec.rb

-24
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737

3838
['--verbose', '-v'].each do |option|
3939
describe option do
40-
around do |example|
41-
original_flag = application.fetch(:verbose)
42-
example.run
43-
application.set(:verbose, original_flag)
44-
end
45-
4640
it 'sets verbose flag to true' do
4741
expect do
4842
application.handle_options([option])
@@ -53,12 +47,6 @@
5347

5448
['--simulate', '-s'].each do |option|
5549
describe option do
56-
around do |example|
57-
original_flag = application.fetch(:simulate)
58-
example.run
59-
application.set(:simulate, original_flag)
60-
end
61-
6250
it 'sets simulate flag to true' do
6351
expect do
6452
application.handle_options([option])
@@ -69,12 +57,6 @@
6957

7058
['--debug-configuration-variables', '-d'].each do |option|
7159
describe option do
72-
around do |example|
73-
original_flag = application.fetch(:debug_configuration_variables)
74-
example.run
75-
application.set(:debug_configuration_variables, original_flag)
76-
end
77-
7860
it 'sets debug_configuration_variables flag to true' do
7961
expect do
8062
application.handle_options([option])
@@ -84,12 +66,6 @@
8466
end
8567

8668
describe '--no-report-time' do
87-
around do |example|
88-
original_flag = application.fetch(:skip_report_time)
89-
example.run
90-
application.set(:skip_report_time, original_flag)
91-
end
92-
9369
it 'sets skip_report_time flag to true' do
9470
expect do
9571
application.handle_options(['--no-report-time'])

spec/lib/mina/dsl_spec.rb

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
require 'spec_helper'
2+
3+
describe Mina::DSL, type: :rake do
4+
before do
5+
load_default_config
6+
load_config 'dsl'
7+
end
8+
9+
describe '#reset!' do
10+
let(:task_name) { 'reset_task' }
11+
12+
it "clears all commands before it" do
13+
expect do
14+
invoke_all
15+
end.to output(output_file('dsl_reset')).to_stdout
16+
end
17+
end
18+
19+
describe '#run' do
20+
context 'when one run block is inside another' do
21+
let(:task_name) { 'one_run_inside_another' }
22+
23+
it 'exits with an error message' do
24+
expect do
25+
invoke_all
26+
end.to raise_error(SystemExit)
27+
.and output(/Can't use run block inside another run block./).to_stdout
28+
end
29+
end
30+
31+
context 'when backend is :local' do
32+
let(:task_name) { 'local_run' }
33+
34+
it 'executes commands locally' do
35+
expect do
36+
invoke_all
37+
end.to output(output_file('dsl_local_run')).to_stdout
38+
end
39+
end
40+
41+
context 'when backend is :remote' do
42+
let(:task_name) { 'remote_run' }
43+
44+
it 'executes commands on the server' do
45+
expect do
46+
invoke_all
47+
end.to output(output_file('dsl_remote_run')).to_stdout
48+
end
49+
end
50+
51+
context "when backend doesn't exist" do
52+
let(:task_name) { 'nonexistent_run' }
53+
54+
# TODO: refactor for more user-friendly error handling
55+
it 'raises a runtime error' do
56+
expect do
57+
invoke_all
58+
end.to raise_error(RuntimeError, /Don't know how to build task 'nonexistent_environment'/)
59+
end
60+
end
61+
end
62+
63+
describe '#on' do
64+
let(:task_name) { 'on_stage_task' }
65+
66+
it 'executes the command in the given stage' do
67+
expect do
68+
invoke_all
69+
end.to output(output_file('dsl_on')).to_stdout
70+
end
71+
end
72+
73+
describe '#in_path' do
74+
let(:task_name) { 'in_path_task' }
75+
76+
it 'executes the command in the given path' do
77+
expect do
78+
invoke_all
79+
end.to output(output_file('dsl_in_path')).to_stdout
80+
end
81+
end
82+
83+
describe '#deploy' do
84+
let(:task_name) { 'deploy_block_task' }
85+
86+
it 'executes the deploy script and commands on remote' do
87+
expect do
88+
invoke_all
89+
end.to output(output_file('dsl_deploy')).to_stdout
90+
end
91+
end
92+
end

spec/lib/mina/helpers/internal_spec.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ class DummyInternalHelper
8989
end
9090

9191
describe '#next_version' do
92-
around do |example|
93-
original_releases_path = Mina::Configuration.instance.remove(:releases_path)
92+
before do
9493
Mina::Configuration.instance.set(:releases_path, '/releases')
95-
example.run
96-
Mina::Configuration.instance.set(:releases_path, original_releases_path)
9794
end
9895

9996
context 'when :version_scheme is :datetime' do

spec/spec_helper.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
require 'simplecov'
22

3-
SimpleCov.start
3+
SimpleCov.start do
4+
add_filter '/spec/'
45

5-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6-
$LOAD_PATH.unshift(File.dirname(__FILE__))
6+
enable_coverage :branch
7+
primary_coverage :branch
8+
end
79

810
require 'mina'
911
require 'rspec'
1012
require 'pry'
13+
require 'set'
1114

1215
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
1316
Rake.application = Mina::Application.new
@@ -19,17 +22,16 @@
1922
config.raise_errors_for_deprecations!
2023
config.order = 'random'
2124

22-
config.before(:all, type: :rake) do
23-
Mina::Configuration.instance.set :simulate, true
24-
Mina::Configuration.instance.set :domain, 'localhost'
25-
Mina::Configuration.instance.set :deploy_to, "#{Dir.pwd}/deploy"
26-
Mina::Configuration.instance.set :repository, "#{Mina.root_path}"
27-
Mina::Configuration.instance.set :shared_paths, ['config/database.yml', 'log']
28-
Mina::Configuration.instance.set :keep_releases, 2
25+
initial_task_names = Rake.application.tasks.to_set(&:name)
26+
initial_variables = Mina::Configuration.instance.variables
27+
28+
config.before(:each) do
29+
Mina::Configuration.instance.instance_variable_set(:@variables, initial_variables.clone)
2930
end
3031

31-
config.after(:all, type: :rake) do
32-
Mina::Configuration.instance.remove :simulate
32+
config.after(:each) do
33+
Rake.application.instance_variable_get(:@tasks).keep_if { |task_name, _| initial_task_names.include?(task_name) }
34+
Rake.application.tasks.each(&:reenable)
3335
end
3436

3537
config.around(:each, :suppressed_output) do |example|

spec/support/outputs/dsl_deploy.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(?m)#!/usr/bin/env bash
2+
# Executing the following via 'ssh localhost -p 22 -tt':
3+
.*
4+
echo Hello there

spec/support/outputs/dsl_in_path.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\(cd /path && echo Hello there && cd -\)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
# Executing the following:
3+
#
4+
echo "-----> I am a comment"
5+
echo Hello there

spec/support/outputs/dsl_on.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ============================
2+
# === Start up server => \(in deployer\)
3+
\(
4+
cd ".*/deploy/current"
5+
echo Hello there
6+
\) &&
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
# Executing the following via 'ssh localhost -p 22 -tt':
3+
#
4+
echo "-----> I am a comment"
5+
echo Hello there

spec/support/outputs/dsl_reset.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^((?!echo Hello there).)*$
2+
echo New command

spec/support/rake_example_group.rb

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
module RakeExampleGroup
2-
extend RSpec::Matchers::DSL
2+
extend RSpec::SharedContext
33

4-
def self.included(klass)
5-
klass.instance_eval do
6-
let(:rake) { Rake.application }
7-
let(:task_name) { self.class.description }
8-
let(:run_commands) { rake['run_commands']}
9-
let(:reset!) { rake['reset!'] }
10-
subject { rake[task_name] }
4+
let(:rake) { Rake.application }
5+
let(:task_name) { self.class.description }
6+
let(:run_commands) { rake['run_commands'] }
7+
let(:reset!) { rake['reset!'] }
8+
subject { rake[task_name] }
119

12-
after do
13-
subject.reenable
14-
run_commands.reenable
15-
reset!.invoke
16-
reset!.reenable
17-
end
18-
end
10+
after(:each) do
11+
reset!.invoke
12+
end
13+
14+
def load_default_config
15+
load_config 'default'
16+
end
17+
18+
def load_config(config_name)
19+
Rake.load_rakefile(Dir.pwd + "/spec/configs/#{config_name}.rb")
1920
end
2021

2122
def invoke_all(*args)

0 commit comments

Comments
 (0)