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

DSL tests #695

Merged
merged 7 commits into from
Aug 19, 2021
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ test_env/deploy
.rvmrc
.ruby-gemset
.ruby-version
Gemfile.lock
pkg/
support/helpers.md
support/modules.md
Expand Down
53 changes: 53 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
PATH
remote: .
specs:
mina (1.2.4)
rake

GEM
remote: https://rubygems.org/
specs:
awesome_print (1.9.2)
codeclimate-test-reporter (1.0.7)
simplecov
coderay (1.1.3)
diff-lcs (1.4.4)
docile (1.4.0)
method_source (1.0.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.0.6)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)

PLATFORMS
ruby

DEPENDENCIES
awesome_print
codeclimate-test-reporter
mina!
pry
rspec (~> 3.5.0)
simplecov

BUNDLED WITH
2.2.25
6 changes: 6 additions & 0 deletions spec/configs/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set :simulate, true
set :domain, 'localhost'
set :deploy_to, "#{Dir.pwd}/deploy"
set :repository, "#{Mina.root_path}"
set :shared_paths, ['config/database.yml', 'log']
set :keep_releases, 2
53 changes: 53 additions & 0 deletions spec/configs/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
task :reset_task do
command "echo Hello there"
reset!
command "echo New command"
end

task :one_run_inside_another do
run :local do
run :remote do
puts "I shouldn't execute"
end
end
end

task :local_run do
run :local do
comment "I am a comment"
command "echo Hello there"
end
end

task :remote_run do
run :remote do
comment "I am a comment"
command "echo Hello there"
end
end

task :nonexistent_run do
run :nonexistent do
comment "I shouldn't run"
end
end

task :on_stage_task do
deploy do
on :launch do
command "echo Hello there"
end
end
end

task :in_path_task do
in_path '/path' do
command "echo Hello there"
end
end

task :deploy_block_task do
deploy do
command "echo Hello there"
end
end
24 changes: 0 additions & 24 deletions spec/lib/mina/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@

['--verbose', '-v'].each do |option|
describe option do
around do |example|
original_flag = application.fetch(:verbose)
example.run
application.set(:verbose, original_flag)
end

it 'sets verbose flag to true' do
expect do
application.handle_options([option])
Expand All @@ -53,12 +47,6 @@

['--simulate', '-s'].each do |option|
describe option do
around do |example|
original_flag = application.fetch(:simulate)
example.run
application.set(:simulate, original_flag)
end

it 'sets simulate flag to true' do
expect do
application.handle_options([option])
Expand All @@ -69,12 +57,6 @@

['--debug-configuration-variables', '-d'].each do |option|
describe option do
around do |example|
original_flag = application.fetch(:debug_configuration_variables)
example.run
application.set(:debug_configuration_variables, original_flag)
end

it 'sets debug_configuration_variables flag to true' do
expect do
application.handle_options([option])
Expand All @@ -84,12 +66,6 @@
end

describe '--no-report-time' do
around do |example|
original_flag = application.fetch(:skip_report_time)
example.run
application.set(:skip_report_time, original_flag)
end

it 'sets skip_report_time flag to true' do
expect do
application.handle_options(['--no-report-time'])
Expand Down
92 changes: 92 additions & 0 deletions spec/lib/mina/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require 'spec_helper'

describe Mina::DSL, type: :rake do
before do
load_default_config
load_config 'dsl'
end

describe '#reset!' do
let(:task_name) { 'reset_task' }

it "clears all commands before it" do
expect do
invoke_all
end.to output(output_file('dsl_reset')).to_stdout
end
end

describe '#run' do
context 'when one run block is inside another' do
let(:task_name) { 'one_run_inside_another' }

it 'exits with an error message' do
expect do
invoke_all
end.to raise_error(SystemExit)
.and output(/Can't use run block inside another run block./).to_stdout
end
end

context 'when backend is :local' do
let(:task_name) { 'local_run' }

it 'executes commands locally' do
expect do
invoke_all
end.to output(output_file('dsl_local_run')).to_stdout
end
end

context 'when backend is :remote' do
let(:task_name) { 'remote_run' }

it 'executes commands on the server' do
expect do
invoke_all
end.to output(output_file('dsl_remote_run')).to_stdout
end
end

context "when backend doesn't exist" do
let(:task_name) { 'nonexistent_run' }

# TODO: refactor for more user-friendly error handling
it 'raises a runtime error' do
expect do
invoke_all
end.to raise_error(RuntimeError, /Don't know how to build task 'nonexistent_environment'/)
end
end
end

describe '#on' do
let(:task_name) { 'on_stage_task' }

it 'executes the command in the given stage' do
expect do
invoke_all
end.to output(output_file('dsl_on')).to_stdout
end
end

describe '#in_path' do
let(:task_name) { 'in_path_task' }

it 'executes the command in the given path' do
expect do
invoke_all
end.to output(output_file('dsl_in_path')).to_stdout
end
end

describe '#deploy' do
let(:task_name) { 'deploy_block_task' }

it 'executes the deploy script and commands on remote' do
expect do
invoke_all
end.to output(output_file('dsl_deploy')).to_stdout
end
end
end
5 changes: 1 addition & 4 deletions spec/lib/mina/helpers/internal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ class DummyInternalHelper
end

describe '#next_version' do
around do |example|
original_releases_path = Mina::Configuration.instance.remove(:releases_path)
before do
Mina::Configuration.instance.set(:releases_path, '/releases')
example.run
Mina::Configuration.instance.set(:releases_path, original_releases_path)
end

context 'when :version_scheme is :datetime' do
Expand Down
26 changes: 14 additions & 12 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
require 'simplecov'

SimpleCov.start
SimpleCov.start do
add_filter '/spec/'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
enable_coverage :branch
primary_coverage :branch
end

require 'mina'
require 'rspec'
require 'pry'
require 'set'

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

config.before(:all, type: :rake) do
Mina::Configuration.instance.set :simulate, true
Mina::Configuration.instance.set :domain, 'localhost'
Mina::Configuration.instance.set :deploy_to, "#{Dir.pwd}/deploy"
Mina::Configuration.instance.set :repository, "#{Mina.root_path}"
Mina::Configuration.instance.set :shared_paths, ['config/database.yml', 'log']
Mina::Configuration.instance.set :keep_releases, 2
initial_task_names = Rake.application.tasks.to_set(&:name)
initial_variables = Mina::Configuration.instance.variables

config.before(:each) do
Mina::Configuration.instance.instance_variable_set(:@variables, initial_variables.clone)
end

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

config.around(:each, :suppressed_output) do |example|
Expand Down
4 changes: 4 additions & 0 deletions spec/support/outputs/dsl_deploy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(?m)#!/usr/bin/env bash
# Executing the following via 'ssh localhost -p 22 -tt':
.*
echo Hello there
1 change: 1 addition & 0 deletions spec/support/outputs/dsl_in_path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\(cd /path && echo Hello there && cd -\)
5 changes: 5 additions & 0 deletions spec/support/outputs/dsl_local_run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Executing the following:
#
echo "-----> I am a comment"
echo Hello there
6 changes: 6 additions & 0 deletions spec/support/outputs/dsl_on.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ============================
# === Start up server => \(in deployer\)
\(
cd ".*/deploy/current"
echo Hello there
\) &&
5 changes: 5 additions & 0 deletions spec/support/outputs/dsl_remote_run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Executing the following via 'ssh localhost -p 22 -tt':
#
echo "-----> I am a comment"
echo Hello there
2 changes: 2 additions & 0 deletions spec/support/outputs/dsl_reset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^((?!echo Hello there).)*$
echo New command
31 changes: 16 additions & 15 deletions spec/support/rake_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module RakeExampleGroup
extend RSpec::Matchers::DSL
extend RSpec::SharedContext

def self.included(klass)
klass.instance_eval do
let(:rake) { Rake.application }
let(:task_name) { self.class.description }
let(:run_commands) { rake['run_commands']}
let(:reset!) { rake['reset!'] }
subject { rake[task_name] }
let(:rake) { Rake.application }
let(:task_name) { self.class.description }
let(:run_commands) { rake['run_commands'] }
let(:reset!) { rake['reset!'] }
subject { rake[task_name] }

after do
subject.reenable
run_commands.reenable
reset!.invoke
reset!.reenable
end
end
after(:each) do
reset!.invoke
end

def load_default_config
load_config 'default'
end

def load_config(config_name)
Rake.load_rakefile(Dir.pwd + "/spec/configs/#{config_name}.rb")
end

def invoke_all(*args)
Expand Down
Loading