Skip to content

Commit 664c914

Browse files
committed
Implement allowlist for puppet module content
This implements puppetlabs/puppet-specifications#157 * By default every file is ignored * Only files from the official specification for puppet modules are added to the allowlist * support for .pdkignore, .pmtignore and .gitignore is removed
1 parent 1a7a0f0 commit 664c914

File tree

3 files changed

+32
-101
lines changed

3 files changed

+32
-101
lines changed

.rubocop_todo.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-06-28 12:03:58 UTC using RuboCop version 1.64.1.
3+
# on 2024-07-05 11:25:28 UTC using RuboCop version 1.64.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -11,7 +11,7 @@ Lint/MixedRegexpCaptureTypes:
1111
Exclude:
1212
- 'Gemfile'
1313

14-
# Offense count: 4
14+
# Offense count: 2
1515
# Configuration parameters: Prefixes, AllowedPatterns.
1616
# Prefixes: when, with, without
1717
RSpec/ContextWording:
@@ -34,24 +34,24 @@ RSpec/MessageSpies:
3434
RSpec/MultipleExpectations:
3535
Max: 11
3636

37-
# Offense count: 13
37+
# Offense count: 8
3838
# Configuration parameters: AllowSubject.
3939
RSpec/MultipleMemoizedHelpers:
4040
Max: 8
4141

42-
# Offense count: 8
42+
# Offense count: 5
4343
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
4444
# SupportedStyles: always, named_only
4545
RSpec/NamedSubject:
4646
Exclude:
4747
- 'spec/unit/puppet/modulebuilder/builder_spec.rb'
4848

49-
# Offense count: 3
49+
# Offense count: 1
5050
# Configuration parameters: AllowedGroups.
5151
RSpec/NestedGroups:
52-
Max: 5
52+
Max: 4
5353

54-
# Offense count: 32
54+
# Offense count: 28
5555
RSpec/SubjectStub:
5656
Exclude:
5757
- 'spec/unit/puppet/modulebuilder/builder_spec.rb'

lib/puppet/modulebuilder/builder.rb

+25-36
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,29 @@
55
module Puppet::Modulebuilder
66
# Class to build Puppet Modules from source
77
class Builder
8-
DEFAULT_IGNORED = [
8+
# Due to the way how PathSpec generates the regular expression,
9+
# `/*` doesn't match directories starting with a dot,
10+
# so we need `/.*` as well.
11+
IGNORED = [
12+
'/**',
913
'/.*',
10-
'/pkg/',
11-
'~*',
12-
'/coverage',
13-
'/checksums.json',
14-
'/REVISION',
15-
'/spec/fixtures/modules/',
16-
'/vendor/',
14+
'!/CHANGELOG*',
15+
'!/LICENSE',
16+
'!/README*',
17+
'!/REFERENCE.md',
18+
'!/bolt_plugin.json',
19+
'!/data/**',
20+
'!/docs/**',
21+
'!/files/**',
22+
'!/hiera.yaml',
23+
'!/locales/**',
24+
'!/manifests/**',
25+
'!/metadata.json',
26+
'!/plans/**',
27+
'!/scripts/**',
28+
'!/tasks/**',
29+
'!/templates/**',
30+
'!/types/**',
1731
].freeze
1832

1933
attr_reader :destination, :logger
@@ -168,21 +182,6 @@ def warn_symlink(path)
168182
from: symlink_path.relative_path_from(module_path), to: symlink_path.realpath.relative_path_from(module_path))
169183
end
170184

171-
# Select the most appropriate ignore file in the module directory.
172-
#
173-
# In order of preference, we first try `.pdkignore`, then `.pmtignore`
174-
# and finally `.gitignore`.
175-
#
176-
# @return [String] The path to the file containing the patterns of file
177-
# paths to ignore.
178-
def ignore_file
179-
@ignore_file ||= [
180-
File.join(source, '.pdkignore'),
181-
File.join(source, '.pmtignore'),
182-
File.join(source, '.gitignore'),
183-
].find { |file| file_exists?(file) && file_readable?(file) }
184-
end
185-
186185
# Checks if the path contains any non-ASCII characters.
187186
#
188187
# Java will throw an error when it encounters a path containing
@@ -251,20 +250,10 @@ def build_package
251250
def ignored_files
252251
require 'pathspec'
253252

254-
@ignored_files ||=
255-
begin
256-
ignored = if ignore_file.nil?
257-
PathSpec.new
258-
else
259-
PathSpec.new(read_file(ignore_file, open_args: 'rb:UTF-8'))
260-
end
261-
262-
ignored = ignored.add("/#{File.basename(destination)}/") if File.realdirpath(destination).start_with?(File.realdirpath(source))
253+
ignored = PathSpec.new(IGNORED)
254+
ignored.add("/#{File.basename(destination)}/") if File.realdirpath(destination).start_with?(File.realdirpath(source))
263255

264-
DEFAULT_IGNORED.each { |r| ignored.add(r) }
265-
266-
ignored
267-
end
256+
ignored
268257
end
269258

270259
# Create a temporary build directory where the files to be included in

spec/unit/puppet/modulebuilder/builder_spec.rb

-58
Original file line numberDiff line numberDiff line change
@@ -300,64 +300,6 @@
300300
end
301301
end
302302

303-
describe '#ignore_file' do
304-
subject { builder.ignore_file }
305-
306-
let(:module_source) { File.join(root_dir, 'tmp', 'my-module') }
307-
let(:possible_files) do
308-
[
309-
'.pdkignore',
310-
'.pmtignore',
311-
'.gitignore',
312-
]
313-
end
314-
let(:available_files) { [] }
315-
316-
before do
317-
available_files.each do |file|
318-
file_path = File.join(module_source, file)
319-
320-
allow(builder).to receive(:file_exists?).with(file_path).and_return(true)
321-
allow(builder).to receive(:file_readable?).with(file_path).and_return(true)
322-
end
323-
324-
(possible_files - available_files).each do |file|
325-
file_path = File.join(module_source, file)
326-
327-
allow(builder).to receive(:file_exists?).with(file_path).and_return(false)
328-
allow(builder).to receive(:file_readable?).with(file_path).and_return(false)
329-
end
330-
end
331-
332-
context 'when none of the possible ignore files are present' do
333-
it { is_expected.to be_nil }
334-
end
335-
336-
context 'when .gitignore is present' do
337-
let(:available_files) { ['.gitignore'] }
338-
339-
it 'returns the path to the .gitignore file' do
340-
expect(subject).to eq(File.join(module_source, '.gitignore'))
341-
end
342-
343-
context 'and .pmtignore is present' do
344-
let(:available_files) { ['.gitignore', '.pmtignore'] }
345-
346-
it 'returns the path to the .pmtignore file' do
347-
expect(subject).to eq(File.join(module_source, '.pmtignore'))
348-
end
349-
350-
context 'and .pdkignore is present' do
351-
let(:available_files) { possible_files }
352-
353-
it 'returns the path to the .pdkignore file' do
354-
expect(subject).to eq(File.join(module_source, '.pdkignore'))
355-
end
356-
end
357-
end
358-
end
359-
end
360-
361303
describe '#ignored_files' do
362304
subject { builder.ignored_files }
363305

0 commit comments

Comments
 (0)