Class: PDK::Validate::PuppetSyntax
Constant Summary
collapse
- ERROR_CONTEXT =
In Puppet >= 5.3.4, the error context formatting was changed to facilitate localization
%r{(?:file:\s(?<file>.+?)|line:\s(?<line>.+?)|column:\s(?<column>.+?))}
- 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"
%r{(?:at\sline\s(?<line>\d+)|at\s(?<file>.+?):(?<line>\d+):(?<column>\d+)|at\s(?<file>.+?):(?<line>\d+)|in\s(?<file>.+?))}
- PUPPET_LOGGER_PREFIX =
%r{^(debug|info|notice|warning|error|alert|critical):\s.+?$}i
- PUPPET_SYNTAX_PATTERN =
%r{^
(?<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
BaseValidator::ALLOW_EMPTY_TARGETS, BaseValidator::IGNORE_DOTFILES, BaseValidator::INVOKE_STYLE
Class Method Summary
collapse
allow_empty_targets?, cmd_path, ignore_dotfiles?, ignore_pathspec, invoke, parse_targets, process_invalid, process_skipped
Class Method Details
.cmd ⇒ Object
32
33
34
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 32
def self.cmd
'puppet'
end
|
.name ⇒ Object
28
29
30
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 28
def self.name
'puppet-syntax'
end
|
.null_file ⇒ Object
52
53
54
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 52
def self.null_file
Gem.win_platform? ? 'NUL' : '/dev/null'
end
|
.parse_offense(offense) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 84
def self.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)
unless attributes.nil?
attributes.names.each do |name|
offense_data[name.to_sym] = attributes[name] unless attributes[name].nil?
end
end
else
offense_data[:message] = offense
end
offense_data
end
|
.parse_options(_options, targets) ⇒ Object
48
49
50
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 48
def self.parse_options(_options, targets)
['parser', 'validate', '--config', null_file].concat(targets)
end
|
.parse_output(report, result, targets) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 56
def self.parse_output(report, result, targets)
output = result[:stderr].split("\n").reject { |entry| entry.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] =~ %r{#{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
36
37
38
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 36
def self.pattern
'**/**.pp'
end
|
.pattern_ignore ⇒ Object
40
41
42
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 40
def self.pattern_ignore
'/plans/**/*.pp'
end
|
.sanitize_console_output(line) ⇒ Object
107
108
109
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 107
def self.sanitize_console_output(line)
line.gsub!(%r{\e\[([;\d]+)?m}, '')
end
|
.spinner_text(_targets = nil) ⇒ Object
44
45
46
|
# File 'lib/pdk/validate/puppet/puppet_syntax.rb', line 44
def self.spinner_text(_targets = nil)
_('Checking Puppet manifest syntax (%{pattern}).') % { pattern: pattern }
end
|