pdk 2.7.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +29 -0
  3. data/README.md +2 -48
  4. data/lib/pdk/analytics/client/google_analytics.rb +22 -26
  5. data/lib/pdk/analytics/util.rb +0 -1
  6. data/lib/pdk/analytics.rb +1 -1
  7. data/lib/pdk/bolt.rb +1 -0
  8. data/lib/pdk/cli/build.rb +53 -56
  9. data/lib/pdk/cli/bundle.rb +34 -33
  10. data/lib/pdk/cli/console.rb +136 -134
  11. data/lib/pdk/cli/convert.rb +39 -41
  12. data/lib/pdk/cli/env.rb +49 -47
  13. data/lib/pdk/cli/errors.rb +1 -2
  14. data/lib/pdk/cli/exec/command.rb +23 -29
  15. data/lib/pdk/cli/exec/interactive_command.rb +7 -12
  16. data/lib/pdk/cli/exec.rb +4 -11
  17. data/lib/pdk/cli/exec_group.rb +3 -2
  18. data/lib/pdk/cli/get/config.rb +21 -19
  19. data/lib/pdk/cli/get.rb +15 -13
  20. data/lib/pdk/cli/new/class.rb +22 -22
  21. data/lib/pdk/cli/new/defined_type.rb +22 -22
  22. data/lib/pdk/cli/new/fact.rb +19 -19
  23. data/lib/pdk/cli/new/function.rb +20 -20
  24. data/lib/pdk/cli/new/module.rb +40 -38
  25. data/lib/pdk/cli/new/provider.rb +19 -19
  26. data/lib/pdk/cli/new/task.rb +23 -23
  27. data/lib/pdk/cli/new/test.rb +50 -48
  28. data/lib/pdk/cli/new/transport.rb +18 -18
  29. data/lib/pdk/cli/new.rb +11 -9
  30. data/lib/pdk/cli/release/prep.rb +27 -25
  31. data/lib/pdk/cli/release/publish.rb +39 -37
  32. data/lib/pdk/cli/release.rb +152 -149
  33. data/lib/pdk/cli/remove/config.rb +63 -60
  34. data/lib/pdk/cli/remove.rb +15 -13
  35. data/lib/pdk/cli/set/config.rb +91 -89
  36. data/lib/pdk/cli/set.rb +15 -13
  37. data/lib/pdk/cli/test/unit.rb +71 -69
  38. data/lib/pdk/cli/test.rb +9 -7
  39. data/lib/pdk/cli/update.rb +33 -38
  40. data/lib/pdk/cli/util/command_redirector.rb +10 -1
  41. data/lib/pdk/cli/util/interview.rb +11 -4
  42. data/lib/pdk/cli/util/option_normalizer.rb +2 -4
  43. data/lib/pdk/cli/util/option_validator.rb +7 -9
  44. data/lib/pdk/cli/util/update_manager_printer.rb +4 -4
  45. data/lib/pdk/cli/util.rb +32 -48
  46. data/lib/pdk/cli/validate.rb +98 -96
  47. data/lib/pdk/cli.rb +124 -120
  48. data/lib/pdk/config/ini_file.rb +4 -3
  49. data/lib/pdk/config/ini_file_setting.rb +6 -10
  50. data/lib/pdk/config/json.rb +1 -0
  51. data/lib/pdk/config/json_schema_namespace.rb +5 -10
  52. data/lib/pdk/config/json_schema_setting.rb +3 -5
  53. data/lib/pdk/config/json_with_schema.rb +2 -4
  54. data/lib/pdk/config/namespace.rb +19 -13
  55. data/lib/pdk/config/setting.rb +5 -6
  56. data/lib/pdk/config/task_schema.json +116 -0
  57. data/lib/pdk/config/validator.rb +4 -4
  58. data/lib/pdk/config/yaml.rb +3 -8
  59. data/lib/pdk/config/yaml_with_schema.rb +4 -12
  60. data/lib/pdk/config.rb +47 -51
  61. data/lib/pdk/context/control_repo.rb +3 -2
  62. data/lib/pdk/context/module.rb +2 -2
  63. data/lib/pdk/context/none.rb +2 -2
  64. data/lib/pdk/context.rb +4 -5
  65. data/lib/pdk/control_repo.rb +3 -4
  66. data/lib/pdk/generate/defined_type.rb +3 -3
  67. data/lib/pdk/generate/fact.rb +3 -2
  68. data/lib/pdk/generate/function.rb +5 -4
  69. data/lib/pdk/generate/module.rb +91 -106
  70. data/lib/pdk/generate/provider.rb +5 -4
  71. data/lib/pdk/generate/puppet_class.rb +3 -3
  72. data/lib/pdk/generate/puppet_object.rb +9 -12
  73. data/lib/pdk/generate/task.rb +11 -10
  74. data/lib/pdk/generate/transport.rb +8 -7
  75. data/lib/pdk/generate.rb +1 -1
  76. data/lib/pdk/logger.rb +3 -2
  77. data/lib/pdk/module/build.rb +34 -49
  78. data/lib/pdk/module/convert.rb +13 -22
  79. data/lib/pdk/module/metadata.rb +53 -61
  80. data/lib/pdk/module/release.rb +19 -25
  81. data/lib/pdk/module/update.rb +4 -13
  82. data/lib/pdk/module/update_manager.rb +18 -25
  83. data/lib/pdk/module.rb +1 -1
  84. data/lib/pdk/monkey_patches.rb +268 -0
  85. data/lib/pdk/report/event.rb +12 -37
  86. data/lib/pdk/report.rb +4 -4
  87. data/lib/pdk/template/fetcher/git.rb +7 -8
  88. data/lib/pdk/template/fetcher/local.rb +1 -0
  89. data/lib/pdk/template/fetcher.rb +4 -2
  90. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +4 -9
  91. data/lib/pdk/template/renderer/v1/renderer.rb +19 -20
  92. data/lib/pdk/template/renderer/v1/template_file.rb +4 -8
  93. data/lib/pdk/template/renderer/v1.rb +1 -1
  94. data/lib/pdk/template/renderer.rb +1 -0
  95. data/lib/pdk/template/template_dir.rb +3 -3
  96. data/lib/pdk/template.rb +2 -6
  97. data/lib/pdk/tests/unit.rb +36 -25
  98. data/lib/pdk/util/bundler.rb +10 -14
  99. data/lib/pdk/util/changelog_generator.rb +15 -12
  100. data/lib/pdk/util/env.rb +1 -0
  101. data/lib/pdk/util/filesystem.rb +18 -17
  102. data/lib/pdk/util/git.rb +16 -21
  103. data/lib/pdk/util/json_finder.rb +7 -6
  104. data/lib/pdk/util/puppet_strings.rb +1 -1
  105. data/lib/pdk/util/puppet_version.rb +16 -67
  106. data/lib/pdk/util/ruby_version.rb +9 -13
  107. data/lib/pdk/util/template_uri.rb +17 -29
  108. data/lib/pdk/util/vendored_file.rb +5 -18
  109. data/lib/pdk/util/windows/api_types.rb +70 -64
  110. data/lib/pdk/util/windows/file.rb +31 -27
  111. data/lib/pdk/util/windows/process.rb +59 -61
  112. data/lib/pdk/util/windows/string.rb +19 -12
  113. data/lib/pdk/util.rb +12 -20
  114. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +1 -1
  115. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +25 -25
  116. data/lib/pdk/validate/external_command_validator.rb +6 -1
  117. data/lib/pdk/validate/internal_ruby_validator.rb +5 -4
  118. data/lib/pdk/validate/invokable_validator.rb +30 -20
  119. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +12 -14
  120. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +13 -15
  121. data/lib/pdk/validate/metadata/metadata_validator_group.rb +1 -1
  122. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +16 -18
  123. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +14 -14
  124. data/lib/pdk/validate/puppet/puppet_plan_syntax_validator.rb +1 -1
  125. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +16 -18
  126. data/lib/pdk/validate/puppet/puppet_validator_group.rb +1 -1
  127. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +10 -11
  128. data/lib/pdk/validate/ruby/ruby_validator_group.rb +1 -1
  129. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +19 -24
  130. data/lib/pdk/validate/tasks/tasks_name_validator.rb +11 -13
  131. data/lib/pdk/validate/tasks/tasks_validator_group.rb +1 -1
  132. data/lib/pdk/validate/validator.rb +4 -2
  133. data/lib/pdk/validate/validator_group.rb +6 -3
  134. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +27 -36
  135. data/lib/pdk/validate/yaml/yaml_validator_group.rb +1 -1
  136. data/lib/pdk/validate.rb +6 -6
  137. data/lib/pdk/version.rb +2 -2
  138. data/lib/pdk.rb +12 -12
  139. metadata +27 -35
  140. data/lib/pdk/cli/config/get.rb +0 -26
  141. data/lib/pdk/cli/config.rb +0 -22
  142. data/lib/pdk/cli/module/build.rb +0 -12
  143. data/lib/pdk/cli/module/generate.rb +0 -47
  144. data/lib/pdk/cli/module.rb +0 -14
@@ -21,7 +21,7 @@ module PDK
21
21
  template_file = single_item_path(relative_file_path)
22
22
  return nil unless PDK::Util::Filesystem.file?(template_file) && PDK::Util::Filesystem.readable?(template_file)
23
23
 
24
- PDK.logger.debug("Rendering '%{template}'..." % { template: template_file })
24
+ PDK.logger.debug(format("Rendering '%{template}'...", template: template_file))
25
25
  new_template_file(template_file, template_data_hash).render
26
26
  end
27
27
 
@@ -30,34 +30,34 @@ module PDK
30
30
  # @param item_path [String] The path of the single item to render
31
31
  # @return [String]
32
32
  # @api private
33
- #:nocov:
33
+ # :nocov:
34
34
  def single_item_path(item_path)
35
35
  File.join(template_root, 'object_templates', item_path)
36
36
  end
37
- #:nocov:
37
+ # :nocov:
38
38
 
39
39
  # Helper method used during testing
40
- #:nocov:
40
+ # :nocov:
41
41
  # @api private
42
42
  def new_template_file(template_file, template_data_hash)
43
43
  TemplateFile.new(template_file, template_data_hash)
44
44
  end
45
- #:nocov:
45
+ # :nocov:
46
46
 
47
47
  # Helper method used during testing
48
- #:nocov:
48
+ # :nocov:
49
49
  # @api private
50
50
  def new_legacy_template_dir(context, uri, path, module_metadata = {})
51
51
  LegacyTemplateDir.new(context, uri, path, module_metadata)
52
52
  end
53
- #:nocov:
53
+ # :nocov:
54
54
 
55
55
  # Renders a new module
56
56
  #
57
57
  # @param options [Hash{Object => Object}] A list of options to pass through to the renderer. See PDK::Template::TemplateDir helper methods for other options
58
58
  # @see #render
59
59
  # @api private
60
- #:nocov: This is tested in acceptance and packaging tests
60
+ # :nocov: This is tested in acceptance and packaging tests
61
61
  def render_module(options = {})
62
62
  require 'pdk/template/renderer/v1/template_file'
63
63
 
@@ -71,8 +71,8 @@ module PDK
71
71
 
72
72
  files_in_template(dirs).each do |template_file, template_loc|
73
73
  template_file = template_file.to_s
74
- PDK.logger.debug("Rendering '%{template}'..." % { template: template_file })
75
- dest_path = template_file.sub(%r{\.erb\Z}, '')
74
+ PDK.logger.debug(format("Rendering '%{template}'...", template: template_file))
75
+ dest_path = template_file.sub(/\.erb\Z/, '')
76
76
  config = legacy_template_dir.config_for(dest_path)
77
77
 
78
78
  dest_status = if template_loc.start_with?(moduleroot_init)
@@ -88,9 +88,9 @@ module PDK
88
88
  else
89
89
  begin
90
90
  dest_content = new_template_file(File.join(template_loc, template_file), configs: config, template_dir: legacy_template_dir).render
91
- rescue => error
92
- error_msg = "Failed to render template '%{template}'\n" \
93
- '%{exception}: %{message}' % { template: template_file, exception: error.class, message: error.message }
91
+ rescue StandardError => e
92
+ error_msg = format("Failed to render template '%{template}'\n" \
93
+ '%{exception}: %{message}', template: template_file, exception: e.class, message: e.message)
94
94
  raise PDK::CLI::FatalError, error_msg
95
95
  end
96
96
  end
@@ -98,7 +98,7 @@ module PDK
98
98
  yield dest_path, dest_content, dest_status
99
99
  end
100
100
  end
101
- #:nocov:
101
+ # :nocov:
102
102
 
103
103
  # Returns all files in the given template directories
104
104
  #
@@ -111,17 +111,16 @@ module PDK
111
111
  temp_paths = []
112
112
  dirlocs = []
113
113
  dirs.each do |dir|
114
- raise ArgumentError, "The directory '%{dir}' doesn't exist" % { dir: dir } unless PDK::Util::Filesystem.directory?(dir)
114
+ raise ArgumentError, format("The directory '%{dir}' doesn't exist", dir: dir) unless PDK::Util::Filesystem.directory?(dir)
115
+
115
116
  temp_paths += PDK::Util::Filesystem.glob(File.join(dir, *glob_suffix), File::FNM_DOTMATCH).select do |template_path|
116
- if PDK::Util::Filesystem.file?(template_path) && !PDK::Util::Filesystem.symlink?(template_path)
117
- dirlocs << dir
118
- end
117
+ dirlocs << dir if PDK::Util::Filesystem.file?(template_path) && !PDK::Util::Filesystem.symlink?(template_path)
119
118
  end
120
119
  temp_paths.map do |template_path|
121
- template_path.sub!(%r{\A#{Regexp.escape(dir)}#{Regexp.escape(File::SEPARATOR)}}, '')
120
+ template_path.sub!(/\A#{Regexp.escape(dir)}#{Regexp.escape(File::SEPARATOR)}/, '')
122
121
  end
123
122
  end
124
- Hash[temp_paths.zip dirlocs]
123
+ (temp_paths.zip dirlocs).to_h
125
124
  end
126
125
  end
127
126
  end
@@ -20,9 +20,7 @@ module PDK
20
20
  def initialize(template_file, data = {})
21
21
  @template_file = template_file
22
22
 
23
- if data.key?(:configs)
24
- @configs = data[:configs]
25
- end
23
+ @configs = data[:configs] if data.key?(:configs)
26
24
 
27
25
  super(data)
28
26
  end
@@ -65,11 +63,9 @@ module PDK
65
63
  #
66
64
  # @api private
67
65
  def template_content
68
- if PDK::Util::Filesystem.file?(@template_file) && PDK::Util::Filesystem.readable?(@template_file)
69
- return PDK::Util::Filesystem.read_file(@template_file)
70
- end
66
+ return PDK::Util::Filesystem.read_file(@template_file) if PDK::Util::Filesystem.file?(@template_file) && PDK::Util::Filesystem.readable?(@template_file)
71
67
 
72
- raise ArgumentError, "'%{template}' is not a readable file" % { template: @template_file }
68
+ raise ArgumentError, format("'%{template}' is not a readable file", template: @template_file)
73
69
  end
74
70
 
75
71
  # Renders the content of the template file as an ERB template.
@@ -82,7 +78,7 @@ module PDK
82
78
  def render_erb
83
79
  require 'erb'
84
80
 
85
- renderer = ERB.new(template_content, nil, '-')
81
+ renderer = ERB.new(template_content, trim_mode: '-')
86
82
  renderer.filename = @template_file
87
83
  renderer.result(binding)
88
84
  end
@@ -11,7 +11,7 @@ module PDK
11
11
  # Whether the template directory and context are valid for the V1 renderer
12
12
  # @see PDK::Template::Renderer.instance
13
13
  def self.compatible?(template_root, _context)
14
- %w[moduleroot moduleroot_init].all? { |dir| PDK::Util::Filesystem.directory?(File.join(template_root, dir)) }
14
+ ['moduleroot', 'moduleroot_init'].all? { |dir| PDK::Util::Filesystem.directory?(File.join(template_root, dir)) }
15
15
  end
16
16
 
17
17
  # Creates an instance of the V1 Renderer
@@ -14,6 +14,7 @@ module PDK
14
14
  # @return [AbstractRenderer, nil] An instance of an AbstractRenderer subclass. Returns nil if no renderer could be found
15
15
  def self.instance(template_uri, template_root, context)
16
16
  return V1.instance(template_root, template_uri, context) if V1.compatible?(template_root, context)
17
+
17
18
  nil
18
19
  end
19
20
 
@@ -36,7 +36,7 @@ module PDK
36
36
  @metadata = {}
37
37
 
38
38
  @renderer = renderer.nil? ? Renderer.instance(uri, path, context) : renderer
39
- raise 'Could not find a compatible template renderer for %{path}' % { path: path } if @renderer.nil?
39
+ raise format('Could not find a compatible template renderer for %{path}', path: path) if @renderer.nil?
40
40
  end
41
41
 
42
42
  # Later additions may include Control Repo rendering, for example
@@ -48,7 +48,7 @@ module PDK
48
48
  # def render_new_control_repo(name, repo_metadata = {}, options = {})
49
49
  # render(CONTROL_REPO_TEMPLATE_TYPE, name, options.merge(include_first_time: true, control_repo_metadata: repo_metadata)) { |*args| yield(*args) }
50
50
  # end
51
- #:nocov: These are just helper methods and are tested elsewhere.
51
+ # :nocov: These are just helper methods and are tested elsewhere.
52
52
 
53
53
  # Render an existing module
54
54
  # @see PDK::Template::Renderer::AbstractRenderer.render
@@ -61,7 +61,7 @@ module PDK
61
61
  def render_new_module(module_name, module_metadata = {}, options = {})
62
62
  @renderer.render(MODULE_TEMPLATE_TYPE, module_name, options.merge(include_first_time: true, module_metadata: module_metadata)) { |*args| yield(*args) }
63
63
  end
64
- #:nocov:
64
+ # :nocov:
65
65
  end
66
66
  end
67
67
  end
data/lib/pdk/template.rb CHANGED
@@ -37,12 +37,8 @@ module PDK
37
37
  #
38
38
  # @api public
39
39
  def self.with(uri, context)
40
- unless block_given?
41
- raise ArgumentError, '%{class_name}.with must be passed a block.' % { class_name: name }
42
- end
43
- unless uri.is_a? PDK::Util::TemplateURI
44
- raise ArgumentError, '%{class_name}.with must be passed a PDK::Util::TemplateURI, got a %{uri_type}' % { uri_type: uri.class, class_name: name }
45
- end
40
+ raise ArgumentError, format('%{class_name}.with must be passed a block.', class_name: name) unless block_given?
41
+ raise ArgumentError, format('%{class_name}.with must be passed a PDK::Util::TemplateURI, got a %{uri_type}', uri_type: uri.class, class_name: name) unless uri.is_a? PDK::Util::TemplateURI
46
42
 
47
43
  Fetcher.with(uri) do |fetcher|
48
44
  template_dir = TemplateDir.instance(uri, fetcher.path, context)
@@ -48,14 +48,14 @@ module PDK
48
48
 
49
49
  def self.parallel_with_no_tests?(ran_in_parallel, json_result, result)
50
50
  ran_in_parallel && json_result.empty? &&
51
- ((!result[:exit_code].zero? && result[:stderr].strip =~ %r{Pass files or folders to run$}) ||
52
- result[:stderr].strip =~ %r{No files for parallel_spec to run against$})
51
+ ((!result[:exit_code].zero? && result[:stderr].strip =~ /Pass files or folders to run$/) ||
52
+ result[:stderr].strip =~ /No files for parallel_spec to run against$/)
53
53
  end
54
54
 
55
55
  def self.print_failure(result, exception)
56
56
  $stderr.puts ''
57
- result[:stdout].each_line { |line| $stderr.puts line.rstrip } unless result[:stdout].nil?
58
- result[:stderr].each_line { |line| $stderr.puts line.rstrip } unless result[:stderr].nil?
57
+ result[:stdout]&.each_line { |line| $stderr.puts line.rstrip }
58
+ result[:stderr]&.each_line { |line| $stderr.puts line.rstrip }
59
59
  $stderr.puts ''
60
60
  raise PDK::CLI::FatalError, exception
61
61
  end
@@ -95,7 +95,7 @@ module PDK
95
95
  # Ref - https://github.com/puppetlabs/pdk/issues/828
96
96
  tests = tests.tr('\\', '/') unless tests.nil?
97
97
 
98
- environment = { 'CI_SPEC_OPTIONS' => '--format j' }
98
+ environment = {}
99
99
  environment['PUPPET_GEM_VERSION'] = options[:puppet] if options[:puppet]
100
100
  spinner_msg = options[:parallel] ? 'Running unit tests in parallel.' : 'Running unit tests.'
101
101
 
@@ -109,6 +109,17 @@ module PDK
109
109
  return result[:exit_code]
110
110
  end
111
111
 
112
+ # Run interactive_rake for documentation output when utilising verbose option.
113
+ if (options[:verbose]) && (options[:format])
114
+ environment['CI_SPEC_OPTIONS'] = '--format documentation'
115
+ result = interactive_rake(cmd(tests, options), environment)
116
+ # Caveat: Report will not be written to format option(s) file(s) when there are event failures.
117
+ return result[:exit_code] unless (result[:exit_code]).zero?
118
+
119
+ spinner_msg = "Exporting unit tests -> #{options[:format]}"
120
+ end
121
+
122
+ environment['CI_SPEC_OPTIONS'] = '--format j'
112
123
  result = rake(cmd(tests, options), spinner_msg, environment)
113
124
 
114
125
  json_result = if options[:parallel]
@@ -122,7 +133,7 @@ module PDK
122
133
  result[:exit_code] = 0
123
134
  end
124
135
 
125
- raise PDK::CLI::FatalError, 'Unit test output did not contain a valid JSON result: %{output}' % { output: result[:stdout] } if json_result.nil? || json_result.empty?
136
+ raise PDK::CLI::FatalError, format('Unit test output did not contain a valid JSON result: %{output}', output: result[:stdout]) if json_result.nil? || json_result.empty?
126
137
 
127
138
  json_result = merge_json_results(json_result) if options[:parallel]
128
139
 
@@ -135,38 +146,38 @@ module PDK
135
146
 
136
147
  def self.parse_output(report, json_data, duration)
137
148
  # Output messages to stderr.
138
- json_data['messages'] && json_data['messages'].each { |msg| $stderr.puts msg }
149
+ json_data['messages']&.each { |msg| $stderr.puts msg }
139
150
 
140
151
  example_results = {
141
152
  # Only possibilities are passed, failed, pending:
142
153
  # https://github.com/rspec/rspec-core/blob/main/lib/rspec/core/example.rb#L548
143
154
  'passed' => [],
144
155
  'failed' => [],
145
- 'pending' => [],
156
+ 'pending' => []
146
157
  }
147
158
 
148
- json_data['examples'] && json_data['examples'].each do |ex|
159
+ json_data['examples']&.each do |ex|
149
160
  example_results[ex['status']] << ex if example_results.key?(ex['status'])
150
161
  end
151
162
 
152
163
  example_results.each do |result, examples|
153
164
  # Translate rspec example results to JUnit XML testcase results
154
- state = case result
155
- when 'passed' then :passed
156
- when 'failed' then :failure
157
- when 'pending' then :skipped
158
- end
165
+ state_map = {
166
+ 'passed' => :passed,
167
+ 'failed' => :failure,
168
+ 'pending' => :skipped
169
+ }
159
170
 
160
171
  examples.each do |ex|
161
172
  report.add_event(
162
173
  source: 'rspec',
163
- state: state,
174
+ state: state_map[result],
164
175
  file: ex['file_path'],
165
176
  line: ex['line_number'],
166
177
  test: ex['full_description'],
167
178
  severity: ex['status'],
168
179
  message: ex['pending_message'] || (ex['exception'] && ex['exception']['message']) || nil,
169
- trace: (ex['exception'] && ex['exception']['backtrace']) || nil,
180
+ trace: (ex['exception'] && ex['exception']['backtrace']) || nil
170
181
  )
171
182
  end
172
183
  end
@@ -174,12 +185,8 @@ module PDK
174
185
  return unless json_data['summary']
175
186
 
176
187
  # TODO: standardize summary output
177
- $stderr.puts ' ' << 'Evaluated %{total} tests in %{duration} seconds: %{failures} failures, %{pending} pending.' % {
178
- total: json_data['summary']['example_count'],
179
- duration: duration,
180
- failures: json_data['summary']['failure_count'],
181
- pending: json_data['summary']['pending_count'],
182
- }
188
+ $stderr.puts ' ' << (format('Evaluated %{total} tests in %{duration} seconds: %{failures} failures, %{pending} pending.', total: json_data['summary']['example_count'], duration: duration,
189
+ failures: json_data['summary']['failure_count'], pending: json_data['summary']['pending_count'])) # rubocop:disable Layout/LineLength
183
190
  end
184
191
 
185
192
  def self.merge_json_results(json_data)
@@ -191,6 +198,7 @@ module PDK
191
198
  message_set = Set.new
192
199
  json_data.each do |json|
193
200
  next unless json['messages']
201
+
194
202
  message_set |= json['messages']
195
203
  end
196
204
  merged_json_result['messages'] = message_set.to_a
@@ -199,6 +207,7 @@ module PDK
199
207
  all_examples = []
200
208
  json_data.each do |json|
201
209
  next unless json['examples']
210
+
202
211
  all_examples.concat json['examples']
203
212
  end
204
213
  merged_json_result['examples'] = all_examples
@@ -207,10 +216,11 @@ module PDK
207
216
  summary_hash = {
208
217
  'example_count' => 0,
209
218
  'failure_count' => 0,
210
- 'pending_count' => 0,
219
+ 'pending_count' => 0
211
220
  }
212
221
  json_data.each do |json|
213
222
  next unless json['summary']
223
+
214
224
  summary_hash['example_count'] += json['summary']['example_count']
215
225
  summary_hash['failure_count'] += json['summary']['failure_count']
216
226
  summary_hash['pending_count'] += json['summary']['pending_count']
@@ -233,12 +243,13 @@ module PDK
233
243
  output = rake('spec_list_json', 'Finding unit tests.', environment)
234
244
 
235
245
  rspec_json = PDK::Util.find_first_json_in(output[:stdout])
236
- raise PDK::CLI::FatalError, 'Failed to find valid JSON in output from rspec: %{output}' % { output: output[:stdout] } unless rspec_json
246
+ raise PDK::CLI::FatalError, format('Failed to find valid JSON in output from rspec: %{output}', output: output[:stdout]) unless rspec_json
247
+
237
248
  if rspec_json['examples'].empty?
238
249
  rspec_message = rspec_json['messages'][0]
239
250
  return [] if rspec_message == 'No examples found.'
240
251
 
241
- raise PDK::CLI::FatalError, 'Unable to enumerate examples. rspec reported: %{message}' % { message: rspec_message }
252
+ raise PDK::CLI::FatalError, format('Unable to enumerate examples. rspec reported: %{message}', message: rspec_message)
242
253
  else
243
254
  examples = []
244
255
  rspec_json['examples'].each do |example|
@@ -3,9 +3,9 @@ require 'pdk'
3
3
  module PDK
4
4
  module Util
5
5
  module Bundler
6
- class BundleHelper; end
6
+ # class BundleHelper; end
7
7
 
8
- def self.ensure_bundle!(gem_overrides = nil)
8
+ def self.ensure_bundle!(gem_overrides = {})
9
9
  bundle = BundleHelper.new
10
10
 
11
11
  # This will default ensure_bundle! to re-resolving everything to latest
@@ -17,7 +17,7 @@ module PDK
17
17
  end
18
18
 
19
19
  unless bundle.gemfile?
20
- PDK.logger.debug("No Gemfile found in '%{cwd}'. Skipping bundler management." % { cwd: Dir.pwd })
20
+ PDK.logger.debug(format("No Gemfile found in '%{cwd}'. Skipping bundler management.", cwd: Dir.pwd))
21
21
  return
22
22
  end
23
23
 
@@ -44,9 +44,7 @@ module PDK
44
44
  # If there are missing dependencies after updating the lockfile, let `bundle install`
45
45
  # go out and get them. If the specified puppet gem version points to a remote location
46
46
  # or local filepath, then run bundle install as well.
47
- if !bundle.installed?(gem_overrides) || (gem_overrides[:puppet] && gem_overrides[:puppet].start_with?('file://', 'git://', 'https://'))
48
- bundle.install!(gem_overrides)
49
- end
47
+ bundle.install!(gem_overrides) if !bundle.installed?(gem_overrides) || gem_overrides[:puppet]&.start_with?('file://', 'git://', 'https://')
50
48
 
51
49
  mark_as_bundled!(bundle.gemfile, gem_overrides)
52
50
  end
@@ -81,6 +79,7 @@ module PDK
81
79
 
82
80
  def gemfile_lock
83
81
  return if gemfile.nil?
82
+
84
83
  @gemfile_lock ||= File.join(File.dirname(gemfile), 'Gemfile.lock')
85
84
  end
86
85
 
@@ -115,20 +114,16 @@ module PDK
115
114
  # Subsequent 'bundle install' will still pick up any new dependencies.
116
115
  vendored_lockfiles = [
117
116
  File.join(PDK::Util.package_cachedir, "Gemfile-#{PDK::Util::RubyVersion.active_ruby_version}.lock"),
118
- File.join(PDK::Util.package_cachedir, 'Gemfile.lock'),
117
+ File.join(PDK::Util.package_cachedir, 'Gemfile.lock')
119
118
  ]
120
119
 
121
120
  vendored_gemfile_lock = vendored_lockfiles.find do |lockfile|
122
121
  PDK::Util::Filesystem.exist?(lockfile)
123
122
  end
124
123
 
125
- unless vendored_gemfile_lock
126
- raise PDK::CLI::FatalError, 'Vendored Gemfile.lock (%{source}) not found.' % {
127
- source: vendored_gemfile_lock,
128
- }
129
- end
124
+ raise PDK::CLI::FatalError, format('Vendored Gemfile.lock (%{source}) not found.', source: vendored_gemfile_lock) unless vendored_gemfile_lock
130
125
 
131
- PDK.logger.debug('Using vendored Gemfile.lock from %{source}.' % { source: vendored_gemfile_lock })
126
+ PDK.logger.debug(format('Using vendored Gemfile.lock from %{source}.', source: vendored_gemfile_lock))
132
127
  PDK::Util::Filesystem.cp(vendored_gemfile_lock, File.join(PDK::Util.module_root, 'Gemfile.lock'))
133
128
  else
134
129
  argv = ['lock']
@@ -211,6 +206,7 @@ module PDK
211
206
 
212
207
  def binstubs!(gems)
213
208
  raise PDK::CLI::FatalError, 'Unable to install requested binstubs as the Gemfile is missing' if gemfile.nil?
209
+
214
210
  binstub_dir = File.join(File.dirname(gemfile), 'bin')
215
211
  return true if gems.all? { |gem| PDK::Util::Filesystem.file?(File.join(binstub_dir, gem)) }
216
212
 
@@ -218,7 +214,7 @@ module PDK
218
214
  result = cmd.execute!
219
215
 
220
216
  unless result[:exit_code].zero?
221
- PDK.logger.fatal("Failed to generate binstubs for '%{gems}':\n%{output}" % { gems: gems.join(' '), output: result.values_at(:stdout, :stderr).join("\n") }) unless PDK.logger.debug?
217
+ PDK.logger.fatal(format("Failed to generate binstubs for '%{gems}':\n%{output}", gems: gems.join(' '), output: result.values_at(:stdout, :stderr).join("\n"))) unless PDK.logger.debug?
222
218
  raise PDK::CLI::FatalError, 'Unable to install requested binstubs.'
223
219
  end
224
220
 
@@ -4,7 +4,7 @@ module PDK
4
4
  module Util
5
5
  module ChangelogGenerator
6
6
  # Taken from the version regex in https://forgeapi.puppet.com/schemas/module.json
7
- VERSION_REGEX = %r{^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$}
7
+ VERSION_REGEX = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$/.freeze
8
8
  GEM = 'github_changelog_generator'.freeze
9
9
 
10
10
  # Raises if the github_changelog_generator is not available
@@ -16,7 +16,7 @@ module PDK
16
16
 
17
17
  return if result[:exit_code].zero?
18
18
 
19
- raise PDK::CLI::ExitWithError, 'Unable to generate the changelog as the %{gem} gem is not included in this module\'s Gemfile' % { gem: GEM }
19
+ raise PDK::CLI::ExitWithError, format('Unable to generate the changelog as the %{gem} gem is not included in this module\'s Gemfile', gem: GEM)
20
20
  end
21
21
 
22
22
  # Runs the Changelog Generator gem (in the module's context) to automatically create a CHANGLELOG.MD file
@@ -29,11 +29,12 @@ module PDK
29
29
  changelog_command.context = :module
30
30
 
31
31
  result = changelog_command.execute!
32
- raise PDK::CLI::ExitWithError, 'Error generating changelog: %{stdout}' % { stdout: result[:stdout] } unless result[:exit_code].zero?
32
+ raise PDK::CLI::ExitWithError, format('Error generating changelog: %{stdout}', stdout: result[:stdout]) unless result[:exit_code].zero?
33
33
 
34
34
  output = changelog_content
35
35
 
36
- raise PDK::CLI::ExitWithError, 'The generated changelog contains uncategorized Pull Requests. Please label them and try again. See %{changelog_file} for more details' % { changelog_file: changelog_file } if output =~ %r{UNCATEGORIZED PRS; GO LABEL THEM} # rubocop:disable Metrics/LineLength
36
+ raise PDK::CLI::ExitWithError, format('The generated changelog contains uncategorized Pull Requests. Please label them and try again. See %{changelog_file} for more details', changelog_file: changelog_file) if output.include?('UNCATEGORIZED PRS; GO LABEL THEM') # rubocop:disable Metrics/LineLength
37
+
37
38
  output
38
39
  end
39
40
 
@@ -42,9 +43,10 @@ module PDK
42
43
  # @param current_version [String, Gem::Version] The current version of the module
43
44
  # @return [String] The new version. May be the same as the current version if there are no notable changes
44
45
  def self.compute_next_version(current_version)
45
- raise PDK::CLI::ExitWithError, 'Invalid version string %{version}' % { version: current_version } unless current_version =~ VERSION_REGEX
46
+ raise PDK::CLI::ExitWithError, format('Invalid version string %{version}', version: current_version) unless VERSION_REGEX.match?(current_version)
47
+
46
48
  version = Gem::Version.create(current_version).segments
47
- PDK.logger.info 'Determing the target version from \'%{file}\'' % { file: changelog_file }
49
+ PDK.logger.info format('Determing the target version from \'%{file}\'', file: changelog_file)
48
50
 
49
51
  # Grab all lines that start with ## between from the latest changes
50
52
  # For example given the changelog below
@@ -83,24 +85,24 @@ module PDK
83
85
  if line.start_with?('[')
84
86
  # We're leaving the latest changes so we can break
85
87
  break if in_changelog_entry
88
+
86
89
  in_changelog_entry = true
87
90
  end
88
- if in_changelog_entry && line.start_with?('##')
89
- data += line
90
- end
91
+ data += line if in_changelog_entry && line.start_with?('##')
91
92
  end
92
93
 
93
94
  # Check for meta headers in first two header line matches
94
- if data =~ %r{^### Changed}
95
+ case data
96
+ when /^### Changed/
95
97
  # Major Version bump
96
98
  version[0] += 1
97
99
  version[1] = 0
98
100
  version[2] = 0
99
- elsif data =~ %r{^### Added}
101
+ when /^### Added/
100
102
  # Minor Version bump
101
103
  version[1] += 1
102
104
  version[2] = 0
103
- elsif data =~ %r{^### Fixed}
105
+ when /^### Fixed/
104
106
  # Patch Version bump
105
107
  version[2] += 1
106
108
  end
@@ -128,6 +130,7 @@ module PDK
128
130
 
129
131
  def self.changelog_content
130
132
  return '' if changelog_file.nil?
133
+
131
134
  PDK::Util::Filesystem.read_file(changelog_file, open_args: 'rb:utf-8')
132
135
  end
133
136
  end
data/lib/pdk/util/env.rb CHANGED
@@ -21,6 +21,7 @@ module PDK
21
21
  def [](key)
22
22
  env_hash.each do |item, value|
23
23
  next unless key.casecmp(item).zero?
24
+
24
25
  return value
25
26
  end
26
27
  nil
@@ -14,19 +14,20 @@ module PDK
14
14
  # Make sure all written files have a trailing newline.
15
15
  content += "\n" unless content[-1] == "\n"
16
16
 
17
- File.open(path, 'wb') { |f| f.write(content) }
17
+ File.binwrite(path, content)
18
18
  end
19
19
  module_function :write_file
20
20
 
21
21
  def read_file(file, nil_on_error: false, open_args: 'r')
22
22
  File.read(file, open_args: Array(open_args))
23
- rescue => e
23
+ rescue StandardError => e
24
24
  raise e unless nil_on_error
25
+
25
26
  nil
26
27
  end
27
28
  module_function :read_file
28
29
 
29
- #:nocov:
30
+ # :nocov:
30
31
  # These methods just wrap core Ruby functionality and
31
32
  # can be ignored for code coverage
32
33
  def directory?(*args)
@@ -34,8 +35,8 @@ module PDK
34
35
  end
35
36
  module_function :directory?
36
37
 
37
- def mkdir_p(*args)
38
- FileUtils.mkdir_p(*args)
38
+ def mkdir_p(*args, **kwargs)
39
+ FileUtils.mkdir_p(*args, **kwargs)
39
40
  end
40
41
  module_function :mkdir_p
41
42
 
@@ -74,18 +75,18 @@ module PDK
74
75
  end
75
76
  module_function :exist?
76
77
 
77
- def rm(*args)
78
- FileUtils.rm(*args)
78
+ def rm(*args, **kwargs)
79
+ FileUtils.rm(*args, **kwargs)
79
80
  end
80
81
  module_function :rm
81
82
 
82
- def rm_f(*args)
83
- FileUtils.rm_f(*args)
83
+ def rm_f(*args, **kwargs)
84
+ FileUtils.rm_f(*args, **kwargs)
84
85
  end
85
86
  module_function :rm_f
86
87
 
87
- def rm_rf(*args)
88
- FileUtils.rm_rf(*args)
88
+ def rm_rf(*args, **kwargs)
89
+ FileUtils.rm_rf(*args, **kwargs)
89
90
  end
90
91
  module_function :rm_rf
91
92
 
@@ -95,7 +96,7 @@ module PDK
95
96
  module_function :remove_entry_secure
96
97
 
97
98
  def zero?(*args)
98
- File.zero?(*args)
99
+ File.empty?(*args)
99
100
  end
100
101
  module_function :zero?
101
102
 
@@ -109,13 +110,13 @@ module PDK
109
110
  end
110
111
  module_function :symlink?
111
112
 
112
- def cp(*args)
113
- FileUtils.cp(*args)
113
+ def cp(*args, **kwargs)
114
+ FileUtils.cp(*args, **kwargs)
114
115
  end
115
116
  module_function :cp
116
117
 
117
- def mv(*args)
118
- FileUtils.mv(*args)
118
+ def mv(*args, **kwargs)
119
+ FileUtils.mv(*args, **kwargs)
119
120
  rescue Errno::ENOENT
120
121
  # PDK-1169 - FileUtils.mv raises Errno::ENOENT when moving files inside
121
122
  # VMWare shared folders on Windows. So we need to catch this
@@ -132,7 +133,7 @@ module PDK
132
133
  end
133
134
  end
134
135
  module_function :mv
135
- #:nocov:
136
+ # :nocov:
136
137
  end
137
138
  end
138
139
  end