Skip to content

Commit d07b66f

Browse files
Merge pull request #1114 from jpogran/gh-1111-block-files
(GH-1118) Add ability to skip validating files
2 parents 9d9f6ea + d7ee603 commit d07b66f

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

docs/pdk_testing.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ send your validation output to a file in either JUnit or text format.
125125
command [reference](pdk_reference.md#).
126126
127127
128+
### Ignoring files during module validation
129+
130+
There are times when certain files can be ignored when validating the contents of a Puppet module. PDK provides a configuration option to list sets of files to ignore when running any of the validators.
131+
132+
To configure PDK to ignore a file, use `pdk set config project.validate.ignore`.
133+
The `project.validate.ignore` setting accepts multiple files. To add one or more files, run the command for each file or pattern you want to add.
134+
135+
For example, to ignore a file called `example.yaml` in the folder called `config`, you would run the following command:
136+
137+
```
138+
pdk set config project.validate.ignore "config/example.yaml"
139+
```
140+
141+
To add a wildcard, use a valid [Git ignore pattern](http://git-scm.com/docs/gitignore):
142+
143+
```
144+
pdk set config project.validate.ignore "config/*.yaml"
145+
```
146+
128147
## Unit testing modules
129148
130149
Create and run unit tests to verify that your Puppet code compiles on supported
@@ -211,4 +230,3 @@ correctly performs the functions you expect it to.
211230
PDK reports what Ruby and Puppet versions it is testing against, and after tests
212231
are completed, test results. For a complete list of command options and usage
213232
information, see the PDK command [reference](pdk_reference.md#).
214-

lib/pdk/config.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def project_config
9494
if context.is_a?(PDK::Context::ControlRepo)
9595
mount :environment, PDK::ControlRepo.environment_conf_as_config(File.join(context.root_path, 'environment.conf'))
9696
end
97+
98+
mount :validate, PDK::Config::YAML.new('validate', file: File.join(context.root_path, 'pdk.yaml'), persistent_defaults: true) do
99+
setting 'ignore' do
100+
default_to { [] }
101+
end
102+
end
97103
end
98104
end
99105

lib/pdk/validate/invokable_validator.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ def ignore_pathspec
213213
end
214214
end
215215

216+
# block will always be [] because it is intialized in config
217+
ignore_files = PDK.config.get_within_scopes('validate.ignore')
218+
unless ignore_files.nil? || ignore_files.empty?
219+
Array(ignore_files).each do |pattern|
220+
ignore_pathspec.add(pattern)
221+
end
222+
end
223+
216224
ignore_pathspec
217225
end
218226
end

spec/unit/pdk/config_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def mock_file(path, content)
1717
end
1818

1919
before(:each) do
20+
allow(PDK::Util::Filesystem).to receive(:file?).with(%r{pdk\.yaml}).and_call_original
2021
# Allow the JSON Schema documents to actually be read. Rspec matchers are LIFO
2122
allow(PDK::Util::Filesystem).to receive(:file?).with(%r{_schema\.json}).and_call_original
2223
end

spec/unit/pdk/validate/invokable_validator_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require 'pdk/validate/invokable_validator'
33

44
describe PDK::Validate::InvokableValidator do
5+
include_context 'mock configuration'
6+
57
class PublicInvokableValidator < PDK::Validate::InvokableValidator
68
# Expose protected/private methods for testing.
79
public :contextual_pattern
@@ -59,6 +61,7 @@ class PublicInvokableValidator < PDK::Validate::InvokableValidator
5961

6062
let(:validator_options) { { targets: targets } }
6163
let(:pattern) { '**/**.pp' }
64+
let(:project_config_exists) { false }
6265

6366
RSpec.shared_examples 'a parsed target list in an invalid context' do |expected_skipped_targets = nil|
6467
before(:each) do
@@ -81,6 +84,7 @@ class PublicInvokableValidator < PDK::Validate::InvokableValidator
8184
allow(PDK::Util).to receive(:canonical_path).and_wrap_original do |_m, *args|
8285
args[0]
8386
end
87+
allow(PDK::Util::Filesystem).to receive(:file?).with(%r{.*pdk\.yaml}).and_return(project_config_exists)
8488
end
8589

8690
context 'when given no targets' do
@@ -340,6 +344,43 @@ class PublicInvokableValidator < PDK::Validate::InvokableValidator
340344
expect(target_files[0]).to eq([File.join('manifests', 'init.pp')])
341345
end
342346
end
347+
348+
context 'when a project level configuration file contains a validate.ignore path' do
349+
before(:each) do
350+
allow(PDK::Util::Filesystem).to receive(:directory?).and_return(true)
351+
allow(PDK::Util::Filesystem).to receive(:glob).with(glob_pattern, anything).and_return(globbed_files)
352+
allow(PDK::Util::Filesystem).to receive(:expand_path).with(context_root).and_return(context_root)
353+
allow(PDK::Util::Filesystem).to receive(:read_file).with(%r{pdk.yaml}).and_return(project_ignore_yaml)
354+
end
355+
356+
let(:project_config_exists) { true }
357+
let(:targets) { [] }
358+
let(:glob_pattern) { File.join(context_root, validator.pattern) }
359+
let(:files) do
360+
[
361+
'dodgy.yaml',
362+
File.join('manifests', 'init.pp'),
363+
File.join('vendor', 'bad.yaml'),
364+
File.join('plans', 'foo.pp'),
365+
File.join('plans', 'fine.yaml'),
366+
File.join('plans', 'nested', 'thing.pp'),
367+
]
368+
end
369+
let(:globbed_files) { files.map { |file| File.join(context_root, file) } }
370+
let(:project_ignore_yaml) do
371+
<<CONTENTS
372+
---
373+
ignore:
374+
- "/*.yaml"
375+
- vendor/*.yaml
376+
CONTENTS
377+
end
378+
379+
it 'does not match the ignored files' do
380+
expect(target_files[0].count).to eq(4)
381+
expect(target_files[0]).to eq(['manifests/init.pp', 'plans/foo.pp', 'plans/fine.yaml', 'plans/nested/thing.pp'])
382+
end
383+
end
343384
end
344385

345386
it 'has an ignore_dotfiles? of true' do
@@ -429,6 +470,12 @@ class PublicInvokableValidator < PDK::Validate::InvokableValidator
429470
describe '.contextual_pattern' do
430471
subject(:context_pattern) { validator.contextual_pattern(pattern_for_a_module) }
431472

473+
before(:each) do
474+
allow(PDK::Util::Filesystem).to receive(:file?).with(%r{.*environment\.conf}).and_call_original
475+
allow(PDK::Util::Filesystem).to receive(:read_file).with(%r{.*environment\.conf}).and_call_original
476+
allow(PDK::Util::Filesystem).to receive(:file?).with(%r{.*pdk\.yaml}).and_call_original
477+
end
478+
432479
let(:pattern_for_a_module) { '*.pp' }
433480

434481
context 'given a Module context' do

0 commit comments

Comments
 (0)