Skip to content

Commit

Permalink
(PDK-997) Remove Dir.chdir call from check:test_file task
Browse files Browse the repository at this point in the history
This `chdir` breaks the isolation of the rake task and affects the
behaviour of other tasks that are invoked in it in the same `rake`
execution.

Rather than modify the task so that it changes back to the parent
directory after the task has finished, we can just remove the `chdir`
entirely and just change the glob to look for files in the directory.

This PR also adds some basic unit tests for the `check:test_file` task.
  • Loading branch information
rodjek committed Dec 6, 2018
1 parent 879ce61 commit b423f07
Show file tree
Hide file tree
Showing 3 changed files with 40 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
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@
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] }

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
28 changes: 28 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,28 @@
require 'spec_helper'

describe 'rake check:test_file', type: :task do
before(:each) do
test_pattern = File.join('tests', '**', '*.pp')
allow(Dir).to receive(:[]).with(test_pattern).and_return(test_files)
end

context 'when there are .pp files under tasks/' do
let(:test_files) { [File.join('tests', 'an_example.pp')] }

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 tasks/' do
let(:test_files) { [] }

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

0 comments on commit b423f07

Please sign in to comment.