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

(SDK-313) Update acceptance tests following audit #193

Merged
merged 5 commits into from
Aug 8, 2017
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
17 changes: 0 additions & 17 deletions spec/acceptance/bundle_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@
describe 'Managing Gemfile dependencies' do
include_context 'in a new module', 'bundle_management'

context 'when there is no Gemfile.lock' do
before(:all) do
File.delete('Gemfile.lock') if File.exist?('Gemfile.lock')
# TODO: come up with a way to invoke only the bundler stuff without trying to run unit tests
# @result = shell_ex("#{path_to_pdk} ", chdir: target_dir)
end

describe command('pdk test unit --debug') do
its(:exit_status) { is_expected.to eq 0 }
its(:stderr) { is_expected.to match(%r{Checking for missing Gemfile dependencies}i) }

describe file('Gemfile.lock') do
it { is_expected.to be_file }
end
end
end

context 'when there is an invalid Gemfile' do
before(:all) do
FileUtils.mv('Gemfile', 'Gemfile.old', force: true)
Expand Down
9 changes: 0 additions & 9 deletions spec/acceptance/cli_spec.rb

This file was deleted.

21 changes: 0 additions & 21 deletions spec/acceptance/test_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/acceptance/validate_all_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ class validate_all { }
end
end

describe command('pdk validate --list') do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(%r{Available validators: metadata, puppet, ruby}i) }
end

describe command('pdk validate') do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(%r{Running all available validators}i) }
Expand Down
38 changes: 4 additions & 34 deletions spec/acceptance/validate_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,6 @@
describe 'Running metadata validation' do
let(:spinner_text) { %r{checking metadata}i }

context 'with a fresh module' do
include_context 'in a new module', 'metadata_validation_module'

describe command('pdk validate metadata') do
its(:exit_status) { is_expected.to eq(0) }
its(:stdout) { is_expected.to match(%r{\A\Z}) }
its(:stderr) { is_expected.to match(spinner_text) }
end

describe command('pdk validate metadata --format junit') do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(spinner_text) }
it_behaves_like :it_generates_valid_junit_xml

its(:stdout) do
is_expected.to have_junit_testsuite('metadata-json-lint').with_attributes(
'failures' => eq(0),
'tests' => eq(1),
)
end

its(:stdout) do
is_expected.to have_junit_testcase.in_testsuite('metadata-json-lint').with_attributes(
'classname' => 'metadata-json-lint',
'name' => 'metadata.json',
).that_passed
end
end
end

context 'with a metadata violation' do
include_context 'in a new module', 'metadata_violation_module'

Expand Down Expand Up @@ -123,17 +93,17 @@
its(:stderr) { is_expected.to match(spinner_text) }

its(:stdout) do
is_expected.to have_xpath('/testsuites/testsuite[@name="metadata-json-lint"]/testcase').with_attributes(
is_expected.to have_junit_testcase.in_testsuite('metadata-json-lint').with_attributes(
'classname' => 'metadata-json-lint',
'name' => 'metadata.json',
)
).that_passed
end

its(:stdout) do
is_expected.to have_xpath('/testsuites/testsuite[@name="metadata-json-lint"]/testcase').with_attributes(
is_expected.to have_junit_testcase.in_testsuite('metadata-json-lint').with_attributes(
'classname' => 'metadata-json-lint.dependencies',
'name' => 'broken.json',
)
).that_failed
end
end
end
Expand Down
45 changes: 0 additions & 45 deletions spec/acceptance/validate_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,6 @@
describe 'pdk validate ruby', module_command: true do
let(:junit_xsd) { File.join(RSpec.configuration.fixtures_path, 'JUnit.xsd') }

context 'with a fresh module' do
include_context 'in a new module', 'validate_ruby_module'

example_rb = File.join('spec', 'example.rb')

before(:all) do
File.open(example_rb, 'w') do |f|
f.puts "require 'filepath'"
end
end

describe command('pdk validate ruby') do
its(:exit_status) { is_expected.to eq(0) }
its(:stdout) { is_expected.to match(%r{\A\Z}) }
its(:stderr) { is_expected.to match(%r{Checking Ruby code style}i) }
end

describe command('pdk validate ruby --format junit') do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(%r{checking ruby code style}i) }
it_behaves_like :it_generates_valid_junit_xml

its(:stdout) do
is_expected.to have_junit_testsuite('rubocop').with_attributes(
'failures' => eq(0),
'tests' => a_value > 0,
)
end

its(:stdout) do
is_expected.to have_junit_testcase.in_testsuite('rubocop').with_attributes(
'classname' => 'rubocop',
'name' => example_rb,
).that_passed
end

its(:stdout) do
is_expected.to have_junit_testcase.in_testsuite('rubocop').with_attributes(
'classname' => 'rubocop',
'name' => File.join('spec', 'spec_helper.rb'),
).that_passed
end
end
end

context 'with a style violation' do
include_context 'in a new module', 'foo'

Expand Down
4 changes: 0 additions & 4 deletions spec/acceptance/validate_spec.rb

This file was deleted.

89 changes: 89 additions & 0 deletions spec/unit/cli/test/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'spec_helper'
require 'pdk/tests/unit'

describe '`pdk test unit`' do
subject(:test_unit_cmd) { PDK::CLI.instance_variable_get(:@test_unit_cmd) }

it { is_expected.not_to be_nil }

context 'with --help' do
it do
expect {
PDK::CLI.run(['test', 'unit', '--help'])
}.to raise_error(SystemExit) { |e|
expect(e.status).to eq 0
}.and output(%r{^USAGE\s+pdk test unit}m).to_stdout
end
end

context 'when executing' do
before(:each) do
expect(PDK::CLI::Util).to receive(:ensure_in_module!).with(no_args).once
end

context 'when listing tests' do
let(:args) { ['--list'] }

context 'when no tests are found' do
before(:each) do
expect(PDK::Test::Unit).to receive(:list).with(no_args).once.and_return([])
end

it { expect { test_unit_cmd.run_this(args) }.to output(%r{No examples found}m).to_stdout }
end

context 'when some tests are found' do
let(:test_list) { [{ id: 'first_id', full_description: 'first_description' }, { id: 'second_id', full_description: 'second_description' }] }

before(:each) do
expect(PDK::Test::Unit).to receive(:list).with(no_args).once.and_return(test_list)
end

it { expect { test_unit_cmd.run_this(args) }.to output(%r{Examples:\nfirst_id\tfirst_description\nsecond_id\tsecond_description}m).to_stdout }
end
end

context 'when running tests' do
context 'when tests pass' do
before(:each) do
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(0)
end

it do
expect {
test_unit_cmd.run_this([])
}.to raise_error(SystemExit) { |e|
expect(e.status).to eq 0
}
end

context 'with a format option' do
before(:each) do
expect(PDK::CLI::Util::OptionNormalizer).to receive(:report_formats).with(['text:results.txt']).and_return([{ method: :write_text, target: 'results.txt' }]).twice
end
it do
expect {
test_unit_cmd.run_this(['--format=text:results.txt'])
}.to raise_error(SystemExit) { |e|
expect(e.status).to eq 0
}
end
end
end

context 'when tests fail' do
before(:each) do
expect(PDK::Test::Unit).to receive(:invoke).with(instance_of(PDK::Report), hash_including(:tests)).once.and_return(1)
end

it do
expect {
test_unit_cmd.run_this([])
}.to raise_error(SystemExit) { |e|
expect(e.status).not_to eq 0
}
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/unit/cli/test_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe 'Running `pdk test`' do
subject { PDK::CLI.instance_variable_get(:@test_cmd) }

it { is_expected.not_to be_nil }

context 'when no arguments or options are provided' do
it do
expect {
PDK::CLI.run(['test'])
}.to output(%r{^USAGE\s+pdk test}m).to_stdout
end
end
end
24 changes: 24 additions & 0 deletions spec/unit/pdk/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
require 'spec_helper'

describe PDK::CLI do
context 'when invoking help' do
it 'outputs basic help' do
expect($stdout).to receive(:puts).with(a_string_matching(%r{NAME.*USAGE.*DESCRIPTION.*COMMANDS.*OPTIONS}m))
expect {
described_class.run(['--help'])
}.to raise_error(SystemExit) { |error|
expect(error.status).to eq(0)
}
end
end

['validate', 'test unit', 'bundle'].each do |command|
context "when #{command} command used but not in a module folder" do
it 'informs the user that this is not a module folder' do
expect {
described_class.run(command.split(' '))
}.to raise_error(SystemExit) { |error|
expect(error.status).not_to eq(0)
expect(error.cause.to_s).to match(%r{no metadata\.json found}i)
}
end
end
end

context 'when provided an invalid report format' do
it 'informs the user and exits' do
expect(logger).to receive(:fatal).with(a_string_matching(%r{'non_existant_format'.*valid report format}))
Expand Down