Skip to content

Commit

Permalink
(PDK-402, PDK-403, PDK-404) Updates spec and acceptance tests...
Browse files Browse the repository at this point in the history
... to work with new validation target handling.
  • Loading branch information
bmjen committed Aug 14, 2017
1 parent 97a9a66 commit 63093e3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 34 deletions.
10 changes: 5 additions & 5 deletions spec/acceptance/validate_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
end

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

its(:stdout) do
is_expected.not_to have_xpath('/testsuites/testsuite[@name="metadata-json-lint"]/testcase').with_attributes(
Expand All @@ -81,15 +81,15 @@
end

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

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

its(:stdout) do
Expand All @@ -100,7 +100,7 @@
end

its(:stdout) do
is_expected.to have_junit_testcase.in_testsuite('metadata-json-lint').with_attributes(
is_expected.not_to have_junit_testcase.in_testsuite('metadata-json-lint').with_attributes(
'classname' => 'metadata-json-lint.dependencies',
'name' => 'broken.json',
).that_failed
Expand Down
6 changes: 3 additions & 3 deletions spec/acceptance/validate_puppet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.not_to match(syntax_spinner_text) }
its(:stderr) { is_expected.not_to match(lint_spinner_text) }
its(:stdout) { is_expected.to match(empty_string) }
its(:stdout) { is_expected.to match(%r{Target skipped}) }
end

describe command('pdk validate puppet --format junit') do
Expand All @@ -25,8 +25,8 @@
its(:stderr) { is_expected.not_to match(lint_spinner_text) }

its(:stdout) { is_expected.to pass_validation(junit_xsd) }
its(:stdout) { is_expected.not_to have_junit_testsuite('puppet-syntax') }
its(:stdout) { is_expected.not_to have_junit_testsuite('puppet-lint') }
its(:stdout) { is_expected.to have_junit_testcase.in_testsuite('puppet-syntax').that_was_skipped }
its(:stdout) { is_expected.to have_junit_testcase.in_testsuite('puppet-lint').that_was_skipped }
end
end

Expand Down
15 changes: 8 additions & 7 deletions spec/support/it_accepts_metadata_json_targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@
let(:globbed_files) { [module_metadata_json] }

it 'returns the path to metadata.json in the module' do
expect(parsed_targets).to eq(globbed_files)
expect(parsed_targets.first).to eq(globbed_files)
end
end

context 'and the module does not contain a metadata.json file' do
it 'returns no targets' do
expect(parsed_targets).to eq([])
expect(parsed_targets.first).to eq([])
end
end
end

context 'when given specific target files' do
context 'when given unmatched target files' do
let(:targets) { ['target1', 'target2.json'] }

before(:each) do
targets.each do |target|
allow(File).to receive(:directory?).with(target).and_return(false)
allow(File).to receive(:file?).with(target).and_return(true)
end
end

it 'returns the targets' do
expect(parsed_targets).to eq(targets)
it 'skips the targets' do
expect(parsed_targets.first).to eq([])
end
end

Expand All @@ -56,13 +57,13 @@
let(:globbed_files) { [File.join(targets.first, 'metadata.json')] }

it 'returns the path to the metadata.json file in the target directory' do
expect(parsed_targets).to eq(globbed_files)
expect(parsed_targets.first).to eq(globbed_files)
end
end

context 'and the directory does not contain a metadata.json file' do
it 'returns no targets' do
expect(parsed_targets).to eq([])
expect(parsed_targets.first).to eq([])
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions spec/support/it_accepts_pp_targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
end

it 'returns the paths to all the .pp files in the module' do
expect(parsed_targets).to eq(globbed_files)
expect(parsed_targets.first).to eq(globbed_files)
end
end

context 'and the module contains no .pp files' do
it 'returns no targets' do
expect(parsed_targets).to eq([])
expect(parsed_targets.first).to eq([])
end
end
end
Expand All @@ -38,11 +38,12 @@
before(:each) do
targets.each do |target|
allow(File).to receive(:directory?).with(target).and_return(false)
allow(File).to receive(:file?).with(target).and_return(true)
end
end

it 'returns the targets' do
expect(parsed_targets).to eq(targets)
expect(parsed_targets.first).to eq(targets)
end
end

Expand All @@ -58,13 +59,13 @@
let(:globbed_files) { [File.join(targets.first, 'test.pp')] }

it 'returns the paths to the .pp files in the directory' do
expect(parsed_targets).to eq(globbed_files)
expect(parsed_targets.first).to eq(globbed_files)
end
end

context 'and the directory contains no .pp files' do
it 'returns no targets' do
expect(parsed_targets).to eq([])
expect(parsed_targets.first).to eq([])
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions spec/unit/pdk/validate/metadata_json_lint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,15 @@
allow(PDK::Util::Bundler).to receive(:ensure_binstubs!).with(described_class.cmd)
targets.each do |target|
allow(File).to receive(:directory?).with(target).and_return(false)
allow(File).to receive(:file?).with(target).and_return(true)
end
end

it 'invokes metadata-json-lint once per target' do
targets.each do |target|
cmd_args = expected_args.dup.flatten << target
expect(PDK::CLI::Exec::Command).to receive(:new).with(*cmd_args).and_return(command_double)
end
it 'invokes metadata-json-lint only once on valid target' do
valid_cmd_args = expected_args.dup.flatten << 'metadata.json'
invalid_cmd_args = expected_args.dup.flatten << 'test.json'
expect(PDK::CLI::Exec::Command).to receive(:new).with(*valid_cmd_args).and_return(command_double)
expect(PDK::CLI::Exec::Command).not_to receive(:new).with(*invalid_cmd_args)

described_class.invoke(PDK::Report.new, targets: targets)
end
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/pdk/validate/metadata_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
context 'when a target is provided that is an unreadable file' do
let(:targets) do
[
{ name: 'not_readable', readable: false },
{ name: 'metadata.json', readable: false },
]
end

Expand All @@ -73,7 +73,7 @@
context 'when a target is provided that contains valid JSON' do
let(:targets) do
[
{ name: 'valid_file', content: '{"test": "value"}' },
{ name: 'metadata.json', content: '{"test": "value"}' },
]
end

Expand All @@ -91,7 +91,7 @@
context 'when a target is provided that contains invalid JSON' do
let(:targets) do
[
{ name: 'invalid_file', content: '{"test"": "value"}' },
{ name: 'metadata.json', content: '{"test"": "value"}' },
]
end

Expand All @@ -110,21 +110,21 @@
context 'when targets are provided that contain valid and invalid JSON' do
let(:targets) do
[
{ name: 'invalid_file', content: '{"test": "value",}' },
{ name: 'valid_file', content: '{"test": "value"}' },
{ name: 'invalid/metadata.json', content: '{"test": "value",}' },
{ name: 'valid/metadata.json', content: '{"test": "value"}' },
]
end

it 'adds events for all targets to the report' do
expect(report).to receive(:add_event).with(
file: 'invalid_file',
file: 'invalid/metadata.json',
source: 'metadata-syntax',
state: :failure,
severity: 'error',
message: a_string_matching(%r{\Aexpected next name}),
)
expect(report).to receive(:add_event).with(
file: 'valid_file',
file: 'valid/metadata.json',
source: 'metadata-syntax',
state: :passed,
severity: 'ok',
Expand Down
28 changes: 26 additions & 2 deletions spec/unit/pdk/validate/rubocop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

describe PDK::Validate::Rubocop do
let(:module_root) { File.join('path', 'to', 'test', 'module') }
let(:glob_pattern) { File.join(module_root, described_class.pattern) }

before(:each) do
allow(PDK::Util).to receive(:module_root).and_return(module_root)
Expand All @@ -38,16 +39,39 @@
context 'when given no targets' do
let(:targets) { [] }

let(:globbed_files) do
[
File.join(module_root, 'spec', 'spec_helper.rb'),
]
end

before(:each) do
allow(Dir).to receive(:glob).with(glob_pattern).and_return(globbed_files)
end

it 'returns the module root' do
expect(target_files).to eq([module_root])
expect(target_files.first).to eq(globbed_files)
end
end

context 'when given specific targets' do
let(:targets) { ['target1.rb', 'target2/'] }

let(:globbed_target2) do
[
File.join('target2', 'target.rb'),
]
end

before(:each) do
allow(Dir).to receive(:glob).with(File.join('target2', described_class.pattern)).and_return(globbed_target2)
allow(File).to receive(:directory?).with('target1.rb').and_return(false)
allow(File).to receive(:directory?).with('target2/').and_return(true)
allow(File).to receive(:file?).with('target1.rb').and_return(true)
end

it 'returns the targets' do
expect(target_files).to eq(targets)
expect(target_files.first).to eq(['target1.rb'].concat(globbed_target2))
end
end
end
Expand Down

0 comments on commit 63093e3

Please sign in to comment.