Class: PDK::Validate::Puppet::PuppetSyntaxValidator
Constant Summary
collapse
- ERROR_CONTEXT =
In Puppet >= 5.3.4, the error context formatting was changed to facilitate localization
/(?:file:\s(?<file>.+?)|line:\s(?<line>.+?)|column:\s(?<column>.+?))/.freeze
- ERROR_CONTEXT_LEGACY =
In Puppet < 5.3.3, the error context was formatted in these variations:
- "at file_path:line_num:col_num"
- "at file_path:line_num"
- "at line line_num"
- "in file_path"
/(?:at\sline\s(?<line>\d+)|at\s(?<file>.+?):(?<line>\d+):(?<column>\d+)|at\s(?<file>.+?):(?<line>\d+)|in\s(?<file>.+?))/.freeze
- PUPPET_LOGGER_PREFIX =
/^(debug|info|notice|warning|error|alert|critical):\s.+?$/i.freeze
- PUPPET_SYNTAX_PATTERN =
/^
(?<severity>.+?):\s
(?<message>.+?)
(?:
\s\(#{ERROR_CONTEXT}(,\s#{ERROR_CONTEXT})*\)| # attempt to match the new localisation friendly location
\s#{ERROR_CONTEXT_LEGACY}| # attempt to match the old " at file:line:column" location
$ # handle cases where the output has no location
)
$/x.freeze
Instance Attribute Summary
#commands
Attributes inherited from Validator
#context, #options, #prepared
Instance Method Summary
collapse
#alternate_bin_paths, #cmd_path, #prepare_invoke!, #spinner
#allow_empty_targets?, #fnmatch?, #ignore_dotfiles?, #invoke_style, #parse_targets, #prepare_invoke!, #process_invalid, #process_skipped, #spinner, #spinner_text, #valid_in_context?
Methods inherited from Validator
#initialize, #prepare_invoke!, #spinner, #spinner_text, #spinners_enabled?, #start_spinner, #stop_spinner, #valid_in_context?
Instance Method Details
#cmd ⇒ Object
31
32
33
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 31
def cmd
'puppet'
end
|
#invoke(report) ⇒ Object
56
57
58
59
60
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 56
def invoke(report)
super
ensure
remove_validate_tmpdir
end
|
#name ⇒ Object
27
28
29
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 27
def name
'puppet-syntax'
end
|
#null_file ⇒ Object
76
77
78
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 76
def null_file
Gem.win_platform? ? 'NUL' : '/dev/null'
end
|
#parse_offense(offense) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 108
def parse_offense(offense)
sanitize_console_output(offense)
offense_data = {
source: name,
state: :failure
}
if offense.match(PUPPET_LOGGER_PREFIX)
attributes = offense.match(PUPPET_SYNTAX_PATTERN)
attributes&.names&.each do |name|
offense_data[name.to_sym] = attributes[name] unless attributes[name].nil?
end
else
offense_data[:message] = offense
end
offense_data
end
|
#parse_options(targets) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 47
def parse_options(targets)
['parser', 'validate', '--config', null_file, '--modulepath', validate_tmpdir].concat(targets)
end
|
#parse_output(report, result, targets) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 80
def parse_output(report, result, targets)
output = result[:stderr].split(/\r?\n/).reject(&:empty?)
results_data = []
output.each do |offense|
offense_data = parse_offense(offense)
results_data << offense_data
end
targets.reject { |target| results_data.any? { |j| j[:file] =~ /#{target}/ } }.each do |target|
report.add_event(
file: target,
source: name,
severity: :ok,
state: :passed
)
end
results_data.each do |offense|
report.add_event(offense)
end
end
|
#pattern ⇒ Object
35
36
37
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 35
def pattern
contextual_pattern('**/*.pp')
end
|
#pattern_ignore ⇒ Object
39
40
41
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 39
def pattern_ignore
contextual_pattern('plans/**/*.pp')
end
|
#remove_validate_tmpdir ⇒ Object
#sanitize_console_output(line) ⇒ Object
129
130
131
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 129
def sanitize_console_output(line)
line.gsub!(/\e\[([;\d]+)?m/, '')
end
|
#spinner_text_for_targets(_targets) ⇒ Object
43
44
45
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 43
def spinner_text_for_targets(_targets)
format('Checking Puppet manifest syntax (%{pattern}).', pattern: pattern.join(' '))
end
|
#validate_tmpdir ⇒ Object
62
63
64
65
66
|
# File 'lib/pdk/validate/puppet/puppet_syntax_validator.rb', line 62
def validate_tmpdir
require 'tmpdir'
@validate_tmpdir ||= Dir.mktmpdir('puppet-parser-validate')
end
|