Skip to content

Commit

Permalink
Merge pull request #266 from rodjek/pdk-997
Browse files Browse the repository at this point in the history
(PDK-997) Remove Dir.chdir call from check:test_file task
  • Loading branch information
rodjek authored Dec 8, 2018
2 parents 879ce61 + 4a85db6 commit 133d4ac
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/puppetlabs_spec_helper/rake_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,10 @@

desc 'Fails if .pp files present in tests folder'
task :test_file do
if Dir.exist?('tests')
Dir.chdir('tests')
ppfiles = Dir['*.pp']
unless ppfiles.empty?
puts ppfiles
raise '.pp files present in tests folder; Move them to an examples folder following the new convention'
end
ppfiles = Dir[File.join('tests', '**', '*.pp')]
unless ppfiles.empty?
puts ppfiles
raise '.pp files present in tests folder; Move them to an examples folder following the new convention'
end
end

Expand Down
1 change: 1 addition & 0 deletions puppetlabs_spec_helper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'yard'
spec.add_development_dependency 'gettext-setup', '~> 0.29'
spec.add_development_dependency 'fakefs', '~> 0.13.3'
end
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@
end
end

require 'fakefs/spec_helpers'

require 'puppetlabs_spec_helper/puppet_spec_helper'
require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'
require 'puppetlabs_spec_helper/rake_tasks'

RSpec.shared_context 'rake task', type: :task do
subject(:task) { Rake::Task[task_name] }

include FakeFS::SpecHelpers

let(:task_name) { self.class.top_level_description.sub(%r{\Arake }, '') }
end

# configure RSpec after including all the code
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.mock_with :rspec

config.include_context 'rake task', type: :task
end
43 changes: 43 additions & 0 deletions spec/unit/puppetlabs_spec_helper/tasks/check_test_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

describe 'rake check:test_file', type: :task do
context 'when there are .pp files under tests/' do
before(:each) do
test_files.each do |f|
FileUtils.mkdir_p(File.dirname(f))
FileUtils.touch(f)
end
end

let(:test_files) do
[
File.join(Dir.pwd, 'tests', 'an_example.pp'),
File.join(Dir.pwd, 'tests', 'deep', 'directory', 'structure', 'another_example.pp'),
]
end

it 'raises an error' do
expected_output = test_files.join("\n")

expect { task.execute }
.to raise_error(%r{pp files present in tests folder})
.and output(a_string_including(expected_output)).to_stdout
end
end

context 'when there are no .pp files under tests/' do
before(:each) do
FileUtils.mkdir('tests')
end

it 'runs without raising an error' do
expect { task.execute }.not_to raise_error
end
end

context 'when there is no tests/ directory' do
it 'runs without raising an error' do
expect { task.execute }.not_to raise_error
end
end
end

0 comments on commit 133d4ac

Please sign in to comment.