Skip to content

Commit

Permalink
(PDK-1266) Clear modulepath value when validating manifest syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Feb 28, 2019
1 parent 9db03da commit 02228b1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/pdk/validate/puppet/puppet_syntax.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'pdk'
require 'pdk/cli/exec'
require 'pdk/validate/base_validator'
require 'fileutils'
require 'tmpdir'

module PDK
module Validate
Expand Down Expand Up @@ -46,7 +48,25 @@ def self.spinner_text(_targets = nil)
end

def self.parse_options(_options, targets)
['parser', 'validate', '--config', null_file].concat(targets)
['parser', 'validate', '--config', null_file, '--modulepath', validate_tmpdir].concat(targets)
end

def self.invoke(report, options)
super
ensure
remove_validate_tmpdir
end

def self.validate_tmpdir
@validate_tmpdir ||= Dir.mktmpdir('puppet-parser-validate')
end

def self.remove_validate_tmpdir
return unless @validate_tmpdir
return unless File.directory?(@validate_tmpdir)

FileUtils.remove_entry_secure(@validate_tmpdir)
@validate_tmpdir = nil
end

def self.null_file
Expand Down
74 changes: 73 additions & 1 deletion spec/unit/pdk/validate/puppet_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

describe PDK::Validate::PuppetSyntax do
let(:module_root) { File.join('path', 'to', 'test', 'module') }
let(:tmpdir) { File.join('/', 'tmp', 'puppet-parser-validate') }

before(:each) do
allow(PDK::Util).to receive(:module_root).and_return(module_root)
allow(File).to receive(:directory?).with(module_root).and_return(true)
allow(Dir).to receive(:mktmpdir).with('puppet-parser-validate').and_return(tmpdir)
allow(FileUtils).to receive(:remove_entry_secure).with(tmpdir)
end

it 'defines the base validator attributes' do
Expand All @@ -18,6 +21,75 @@

it_behaves_like 'it accepts .pp targets'

describe '.invoke' do
context 'when the validator runs correctly' do
before(:each) do
allow(described_class).to receive(:parse_targets).with(anything).and_return([[], [], []])
end

it 'cleans up the temp dir after invoking' do
expect(described_class).to receive(:remove_validate_tmpdir)
described_class.invoke(PDK::Report.new, {})
end
end

context 'when the validator raises an exception' do
before(:each) do
allow(described_class).to receive(:parse_targets).with(anything).and_raise(PDK::CLI::FatalError)
end

it 'cleans up the temp dir after invoking' do
expect(described_class).to receive(:remove_validate_tmpdir)
expect {
described_class.invoke(PDK::Report.new, {})
}.to raise_error(PDK::CLI::FatalError)
end
end
end

describe '.remove_validate_tmpdir' do
after(:each) do
described_class.remove_validate_tmpdir
end

context 'when no temp dir has been created' do
before(:each) do
described_class.instance_variable_set('@validate_tmpdir', nil)
end

it 'does not attempt to remove the directory' do
expect(FileUtils).not_to receive(:remove_entry_secure)
end
end

context 'when a temp dir has been created' do
before(:each) do
described_class.validate_tmpdir
allow(File).to receive(:directory?).and_call_original
end

context 'and the path is a directory' do
before(:each) do
allow(File).to receive(:directory?).with(tmpdir).and_return(true)
end

it 'removes the directory' do
expect(FileUtils).to receive(:remove_entry_secure).with(tmpdir)
end
end

context 'but the path is not a directory' do
before(:each) do
allow(File).to receive(:directory?).with(tmpdir).and_return(false)
end

it 'does not attempt to remove the directory' do
expect(FileUtils).not_to receive(:remove_entry_secure)
end
end
end
end

describe '.parse_targets' do
context 'when the module contains task .pp files' do
subject(:parsed_targets) { described_class.parse_targets(targets: targets) }
Expand Down Expand Up @@ -67,7 +139,7 @@
let(:options) { { auto_correct: true } }

it 'has no effect' do
expect(command_args).to eq(%w[parser validate --config /dev/null].concat(targets))
expect(command_args).to eq(%w[parser validate --config /dev/null --modulepath].push(tmpdir).concat(targets))
end
end
end
Expand Down

0 comments on commit 02228b1

Please sign in to comment.