puppet-syntax 4.0.1 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00b51658a2b442427a7acac7c712fb3188d90a66c3d328c5d719b2034b11ad3c
4
- data.tar.gz: 33d7cea4918830fd06b91ce02feab1617eaf50a5554cec8d28e59deaff836cd5
3
+ metadata.gz: 45c8f3b9275ea2f9c7cccb72c2b930a15079ff0618ee55d62d8b3d393ec3f8a1
4
+ data.tar.gz: 11d6e05a07d6932b9180a9e09eb1e5c7cf47b209c7c935bd4d5635e51ce11bba
5
5
  SHA512:
6
- metadata.gz: daf199345270be08621d083a76ce169a089dee282a74ebc6de9475c89c36dbef95da3ccf9aac8e4121c267c3d079064e1d0de6f7701a74a4d4d9132b9bb1da72
7
- data.tar.gz: 395e92778f90d03e941bf9cd67957cf65fc7ce47cac3816a951f9752e569f6056600a3fa3f42944b086d6e8256ad2596af2bf534f7f3e59fb0d8925f4ba36b3f
6
+ metadata.gz: 87e65a0f0e244ee179926e7e9b3904a5f0c77b87b6789a24f98efa18e78a8345312b434c5fbd051c92b9332572b19346a0bddad3623c341b6d9479ec6f55e094
7
+ data.tar.gz: 2bfd3fae9730595def3f1892a753279a7a081ab9ba4f09f8dafc1c798e7aaf67c469886c1fca22a994274d02343a9d838d90a02c56f16dd0c4b65099c28a9404
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2024-03-08 12:59:05 UTC using RuboCop version 1.61.0.
3
+ # on 2024-03-08 16:09:27 UTC using RuboCop version 1.61.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -86,7 +86,7 @@ RSpec/DescribedClass:
86
86
  # Offense count: 7
87
87
  # Configuration parameters: CountAsOne.
88
88
  RSpec/ExampleLength:
89
- Max: 14
89
+ Max: 16
90
90
 
91
91
  # Offense count: 4
92
92
  # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
@@ -100,7 +100,7 @@ RSpec/FilePath:
100
100
 
101
101
  # Offense count: 29
102
102
  RSpec/MultipleExpectations:
103
- Max: 8
103
+ Max: 10
104
104
 
105
105
  # Offense count: 30
106
106
  # Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
@@ -244,7 +244,7 @@ Style/SymbolProc:
244
244
  - 'lib/puppet-syntax/manifests.rb'
245
245
  - 'lib/puppet-syntax/templates.rb'
246
246
 
247
- # Offense count: 2
247
+ # Offense count: 3
248
248
  # This cop supports safe autocorrection (--autocorrect).
249
249
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
250
250
  # URISchemes: http, https
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [v4.1.0](https://github.com/voxpupuli/puppet-syntax/tree/v4.1.0) (2024-03-12)
6
+
7
+ [Full Changelog](https://github.com/voxpupuli/puppet-syntax/compare/v4.0.1...v4.1.0)
8
+
9
+ **Implemented enhancements:**
10
+
11
+ - Hiera: Test for wrong interpolation syntax [\#143](https://github.com/voxpupuli/puppet-syntax/pull/143) ([bastelfreak](https://github.com/bastelfreak))
12
+
5
13
  ## [v4.0.1](https://github.com/voxpupuli/puppet-syntax/tree/v4.0.1) (2024-03-12)
6
14
 
7
15
  [Full Changelog](https://github.com/voxpupuli/puppet-syntax/compare/v4.0.0...v4.0.1)
data/README.md CHANGED
@@ -85,6 +85,13 @@ This reports common mistakes in key names in Hiera files, such as:
85
85
  * Invalid camel casing, such as: `noCamelCase::warning3: true`.
86
86
  * Use of hyphens, such as: `no-hyphens::warning4: true`.
87
87
 
88
+
89
+ * To enable a syntax check on Hiera values, set:
90
+
91
+ ```ruby
92
+ PuppetSyntax.check_hiera_data = true
93
+ ```
94
+
88
95
  ## Usage
89
96
 
90
97
  * To enable Puppet::Syntax, include the following in your module's `Rakefile`:
@@ -139,6 +146,21 @@ By default, this rake task looks for all `.yaml` files in a single module under:
139
146
  * `hieradata/**/*.yaml`
140
147
  * `hiera*.yaml`
141
148
 
149
+ It will validate the syntax of each Hiera *key*. for values, it will check if
150
+ the interpolation function syntax is correct. Wrong:
151
+
152
+ ```
153
+ foo:
154
+ "%{lookup('baz'):3306}": []
155
+ ```
156
+
157
+ correct would be:
158
+
159
+ ```
160
+ foo:
161
+ "%{lookup('baz')}:3306": []
162
+ ```
163
+
142
164
  ### manifests
143
165
 
144
166
  Checks all `.pp` files in the module for syntax errors.
@@ -20,6 +20,16 @@ module PuppetSyntax
20
20
  end
21
21
  end
22
22
 
23
+ def check_hiera_data(_key, value)
24
+ # using filter_map to remove nil values
25
+ # there will be nil values if check_broken_function_call didn't return a string
26
+ # this is a shorthand for filter.compact
27
+ # https://blog.saeloun.com/2019/05/25/ruby-2-7-enumerable-filter-map/
28
+ keys_and_values(value).filter_map do |element|
29
+ check_broken_function_call(element)
30
+ end
31
+ end
32
+
23
33
  # Recurse through complex data structures. Return on first error.
24
34
  def check_eyaml_data(name, val)
25
35
  error = nil
@@ -87,6 +97,11 @@ module PuppetSyntax
87
97
  key_msg = check_hiera_key(k)
88
98
  errors << "WARNING: #{hiera_file}: Key :#{k}: #{key_msg}" if key_msg
89
99
  end
100
+ if PuppetSyntax.check_hiera_data
101
+ check_hiera_data(k, v).each do |value_msg|
102
+ errors << "WARNING: #{hiera_file}: Key :#{k}: #{value_msg}"
103
+ end
104
+ end
90
105
  eyaml_msg = check_eyaml_data(k, v)
91
106
  errors << "WARNING: #{hiera_file}: #{eyaml_msg}" if eyaml_msg
92
107
  end
@@ -96,5 +111,51 @@ module PuppetSyntax
96
111
 
97
112
  errors
98
113
  end
114
+
115
+ private
116
+
117
+ # you can call functions in hiera, like this:
118
+ # "%{lookup('this_is_ok')}"
119
+ # you can do this in everywhere in a hiera value
120
+ # you cannot do string concatenation within {}:
121
+ # "%{lookup('this_is_ok'):3306}"
122
+ # You can do string concatenation outside of {}:
123
+ # "%{lookup('this_is_ok')}:3306"
124
+ def check_broken_function_call(element)
125
+ 'string after a function call but before `}` in the value' if element.is_a?(String) && /%{.+\('.*'\).+}/.match?(element)
126
+ end
127
+
128
+ # gets a hash or array, returns all keys + values as array
129
+ def flatten_data(data, parent_key = [])
130
+ if data.is_a?(Hash)
131
+ data.flat_map do |key, value|
132
+ current_key = parent_key + [key.to_s]
133
+ if value.is_a?(Hash) || value.is_a?(Array)
134
+ flatten_data(value, current_key)
135
+ else
136
+ [current_key.join('.'), value]
137
+ end
138
+ end
139
+ elsif data.is_a?(Array) && !data.empty?
140
+ data.flat_map do |value|
141
+ if value.is_a?(Hash) || value.is_a?(Array)
142
+ flatten_data(value, parent_key)
143
+ else
144
+ [parent_key.join('.'), value]
145
+ end
146
+ end
147
+ else
148
+ [parent_key.join('.')]
149
+ end
150
+ end
151
+
152
+ # gets a string, hash or array, returns all keys + values as flattened + unique array
153
+ def keys_and_values(value)
154
+ if value.is_a?(Hash) || value.is_a?(Array)
155
+ flatten_data(value).flatten.uniq
156
+ else
157
+ [value]
158
+ end
159
+ end
99
160
  end
100
161
  end
@@ -1,3 +1,3 @@
1
1
  module PuppetSyntax
2
- VERSION = '4.0.1'
2
+ VERSION = '4.1.0'
3
3
  end
data/lib/puppet-syntax.rb CHANGED
@@ -20,6 +20,7 @@ module PuppetSyntax
20
20
  ]
21
21
  @fail_on_deprecation_notices = true
22
22
  @check_hiera_keys = false
23
+ @check_hiera_data = false
23
24
 
24
25
  class << self
25
26
  attr_accessor :exclude_paths,
@@ -28,6 +29,7 @@ module PuppetSyntax
28
29
  :templates_paths,
29
30
  :fail_on_deprecation_notices,
30
31
  :epp_only,
31
- :check_hiera_keys
32
+ :check_hiera_keys,
33
+ :check_hiera_data
32
34
  end
33
35
  end
@@ -9,3 +9,9 @@ typical:typo::warning1: true
9
9
  noCamelCase::warning3: true
10
10
  no-hyphens::warning4: true
11
11
  :picky::warning5: true
12
+ this_is::warning6:
13
+ "%{lookup('foobar'):3306}": []
14
+ this_is::warning7:
15
+ - "%{lookup('foobar'):3306}"
16
+ this_is::warning8:
17
+ foo: "%{lookup('foobar'):3306}"
@@ -24,11 +24,12 @@ describe PuppetSyntax::Hiera do
24
24
  context 'check_hiera_keys = true' do
25
25
  before do
26
26
  PuppetSyntax.check_hiera_keys = true
27
+ PuppetSyntax.check_hiera_data = true
27
28
  end
28
29
 
29
30
  it 'returns warnings for invalid keys' do
30
31
  hiera_yaml = 'hiera_badkey.yaml'
31
- examples = 5
32
+ examples = 8
32
33
  files = fixture_hiera(hiera_yaml)
33
34
  res = subject.check(files)
34
35
  (1..examples).each do |n|
@@ -40,6 +41,9 @@ describe PuppetSyntax::Hiera do
40
41
  expect(res[2]).to match('Key :noCamelCase::warning3: Not a valid Puppet variable name for automatic lookup')
41
42
  expect(res[3]).to match('Key :no-hyphens::warning4: Not a valid Puppet variable name for automatic lookup')
42
43
  expect(res[4]).to match('Key :picky::warning5: Puppet automatic lookup will not look up symbols')
44
+ expect(res[5]).to match('Key :this_is::warning6: string after a function call but before `}` in the value')
45
+ expect(res[6]).to match('Key :this_is::warning7: string after a function call but before `}` in the value')
46
+ expect(res[7]).to match('Key :this_is::warning8: string after a function call but before `}` in the value')
43
47
  end
44
48
 
45
49
  it 'returns warnings for bad eyaml values' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-syntax
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vox Pupuli