Skip to content

Commit

Permalink
Internal helpers tests (#689)
Browse files Browse the repository at this point in the history
* Fix typo

* Test remaining internal helpers
  • Loading branch information
lovro-bikic authored Aug 17, 2021
1 parent 5b33620 commit 877ecf5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/mina/helpers/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def next_version
when :sequence
"$((`ls -1 #{fetch(:releases_path)} | sort -n | tail -n 1`+1))"
else
error! 'Unrecognizes version scheme. Use :datetime or :sequence'
error! 'Unrecognized version scheme. Use :datetime or :sequence'
end
end

Expand Down
87 changes: 86 additions & 1 deletion spec/lib/mina/helpers/internal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class DummyInternalHelper

describe '#echo_cmd' do
context 'when not verbose' do
it 'reuturns unedited code' do
before { Mina::Configuration.instance.set(:verbose, false) }
after { Mina::Configuration.instance.remove(:verbose) }

it 'returns unedited code' do
expect(helper.echo_cmd('ls -al')).to eq('ls -al')
end
end
Expand All @@ -54,4 +57,86 @@ class DummyInternalHelper
expect(helper.indent(4, 'ls -al')).to eq(' ls -al')
end
end

describe '#unindent' do
it 'unindents code' do
expect(helper.unindent(" ls -al\n")).to eq('ls -al')
end
end

describe '#report_time' do
context 'when :skip_report_time is true' do
before { Mina::Configuration.instance.set(:skip_report_time, true) }
after { Mina::Configuration.instance.remove(:skip_report_time) }

it "doesn't output report time" do
expect do
helper.report_time {}
end.not_to output.to_stdout
end
end

context 'when :skip_report_time is false' do
before { Mina::Configuration.instance.set(:skip_report_time, false) }
after { Mina::Configuration.instance.remove(:skip_report_time) }

it 'outputs report time' do
expect do
helper.report_time {}
end.to output(/Elapsed time: \d+\.\d\d seconds/).to_stdout
end
end
end

describe '#next_version' do
around do |example|
original_releases_path = Mina::Configuration.instance.remove(:releases_path)
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
before do
Mina::Configuration.instance.set(:version_scheme, :datetime)

allow(Time).to receive(:now).and_return(Time.parse('2020-05-01 12:34:56 UTC'))
end

after { Mina::Configuration.instance.remove(:version_scheme) }

it 'formats current UTC time' do
expect(helper.next_version).to eq('20200501123456')
end
end

context 'when :version_scheme is :sequence' do
before { Mina::Configuration.instance.set(:version_scheme, :sequence) }
after { Mina::Configuration.instance.remove(:version_scheme) }

it 'generates a command to calculate the next version' do
expect(helper.next_version).to eq('$((`ls -1 /releases | sort -n | tail -n 1`+1))')
end
end

context 'when :version_scheme is unknown' do
before { Mina::Configuration.instance.set(:version_scheme, :foobar) }
after { Mina::Configuration.instance.remove(:version_scheme) }

it 'exits with an error message' do
expect do
helper.next_version
end.to raise_error(SystemExit)
.and output(/Unrecognized version scheme\. Use :datetime or :sequence/).to_stdout
end
end
end

describe '#error!' do
it 'exits with an error message' do
expect do
helper.error!('foobar')
end.to raise_error(SystemExit).and output(/foobar/).to_stdout
end
end
end

0 comments on commit 877ecf5

Please sign in to comment.