puppet-function-updater 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c8e70b6514414dcd252d6337de80765df70e417173250932fc332b6a2eaac90
4
- data.tar.gz: e116616d37d9376ff3f4df574df00f75ba3a98ae16b02d1bfd83b0e1bea0a629
3
+ metadata.gz: a5913312054d42bd2b3bfdcb744600991f0b23aa09a4763153f48f0cddb2f355
4
+ data.tar.gz: 8734defb6ff4b41c8e99dcd2baad098aa58a3534d2ed4bb61ba244535d405a63
5
5
  SHA512:
6
- metadata.gz: aa9bf40727c562d18890b1a722d93276f46f544ba22ff995aaabb7184cb9fde47cb55f175365681f512145658642be906f373bad0d855401709c3cac5bbe857b
7
- data.tar.gz: 0f77d9209e589364b682906af5f68fa87fb48dba1cc733b968da855aa8756657305b9c4998ff4324ed861c796c7a3a37df16f21c780bb650e36c4c25996a8369
6
+ metadata.gz: a4ddd547257eca3091c81ca70f85a8ce81d371f2f409f335079cb26fc3fe263b2698cf0fe1c71d2a688662f031127b1212a27d169e9d1c6fe9054d9a3b02d83c
7
+ data.tar.gz: 3b987e1bce14d1b7d4f74c459911975cadd43816b38da43e6ce70d64da5f976f98acb580f91e8276ebcb549f29c81ca807631618ce6f8558322c5f067dbaf764
@@ -1,3 +1,8 @@
1
+ v0.0.4
2
+ * Generate a simple spec test for the new function
3
+ * Properly report the `require` warning as a warning
4
+ * Write a little more docs
5
+
1
6
  v0.0.3
2
7
  * Cleans up by deleting the legacy function files if requested with `--clean`
3
8
 
data/README.md CHANGED
@@ -17,8 +17,52 @@ $ gem install puppet-function-updater
17
17
 
18
18
  ## Usage
19
19
 
20
- Run the command `puppet_function_updater` in the root of a Puppet module, then
21
- inspect all the generated functions for suitability.
20
+ Run the command puppet_function_updater in the root of a Puppet module, then
21
+ inspect all the generated functions for suitability when it’s done. If you pass
22
+ the --clean argument it will **delete the legacy function file from disk** after
23
+ validating that the new function works.
24
+
25
+ You might see some warnings. Many can be ignored. For example, the following
26
+ warning just means that the function attempted to require a library file outside
27
+ of the the function block. You might evaluate whether you could remove the call
28
+ or move it inside the function declaration so it's lazily loaded.
29
+
30
+ ```
31
+ INFO: Creating lib/puppet/functions/stdlib/deep_merge.rb
32
+ WARN: The function attempted to load libraries outside the function block.
33
+ WARN: cannot load such file -- puppet/parser/functions (ignored)
34
+ ```
35
+
36
+ On the other hand, errors like below indicate that the function code doesn't parse
37
+ properly after porting. It's generally an issue with the original source code.
38
+
39
+ ```
40
+ INFO: Creating lib/puppet/functions/stdlib/validate_x509_rsa_key_pair.rb
41
+ ERROR: Oh crap; the generated function isn't valid Ruby code!
42
+ ERROR: <compiled>:47: dynamic constant assignment
43
+ NUM_ARGS = 2 unless defined? NUM_ARGS
44
+ ^
45
+ ```
46
+
47
+ ### After porting
48
+
49
+ Two files will be generated, the function file and the spec test for that function.
50
+
51
+ * `lib/puppet/functions/<namespace>/<function>.rb`
52
+ * `spec/functions/<namespace>_<function>_spec.rb`
53
+
54
+ After porting, you should inspect the generated files. At a minimum, you'll want
55
+ to clean up the documentation comments, but I'd also suggest reading a bit on the
56
+ new function API and writing better dispatches to provide proper type checking
57
+ and reduce the amount of manual argument validation your function must do.
58
+
59
+ The test simply validates that the function compiles and defines a function
60
+ properly, so you'll also want to write more test cases. If your legacy function
61
+ has unit tests, you might consider porting them to the new function, following
62
+ the examples provided as comments.
63
+
64
+ See a [tutorial](https://binford2k.com/2019/11/27/automagic-function-port/) on its usage on my blog.
65
+
22
66
 
23
67
  ### Example:
24
68
 
@@ -42,7 +86,7 @@ INFO: Creating lib/puppet/functions/stdlib/concat.rb
42
86
  INFO: Creating lib/puppet/functions/stdlib/convert_base.rb
43
87
  INFO: Creating lib/puppet/functions/stdlib/count.rb
44
88
  INFO: Creating lib/puppet/functions/stdlib/deep_merge.rb
45
- ERROR: The function attempted to load libraries outside the function block.
89
+ WARN: The function attempted to load libraries outside the function block.
46
90
  WARN: cannot load such file -- puppet/parser/functions (ignored)
47
91
  INFO: Creating lib/puppet/functions/stdlib/defined_with_params.rb
48
92
  INFO: Creating lib/puppet/functions/stdlib/delete.rb
@@ -60,10 +104,10 @@ INFO: Creating lib/puppet/functions/stdlib/downcase.rb
60
104
  INFO: Creating lib/puppet/functions/stdlib/empty.rb
61
105
  INFO: Creating lib/puppet/functions/stdlib/enclose_ipv6.rb
62
106
  INFO: Creating lib/puppet/functions/stdlib/ensure_packages.rb
63
- ERROR: The function attempted to load libraries outside the function block.
107
+ WARN: The function attempted to load libraries outside the function block.
64
108
  WARN: cannot load such file -- puppet/parser/functions (ignored)
65
109
  INFO: Creating lib/puppet/functions/stdlib/ensure_resource.rb
66
- ERROR: The function attempted to load libraries outside the function block.
110
+ WARN: The function attempted to load libraries outside the function block.
67
111
  WARN: cannot load such file -- puppet/parser/functions (ignored)
68
112
  INFO: Creating lib/puppet/functions/stdlib/ensure_resources.rb
69
113
  INFO: Creating lib/puppet/functions/stdlib/flatten.rb
@@ -72,7 +116,7 @@ INFO: Creating lib/puppet/functions/stdlib/fqdn_rand_string.rb
72
116
  INFO: Creating lib/puppet/functions/stdlib/fqdn_rotate.rb
73
117
  INFO: Creating lib/puppet/functions/stdlib/fqdn_uuid.rb
74
118
  INFO: Creating lib/puppet/functions/stdlib/get_module_path.rb
75
- ERROR: The function attempted to load libraries outside the function block.
119
+ WARN: The function attempted to load libraries outside the function block.
76
120
  WARN: cannot load such file -- puppet/parser/functions (ignored)
77
121
  INFO: Creating lib/puppet/functions/stdlib/getparam.rb
78
122
  INFO: Creating lib/puppet/functions/stdlib/getvar.rb
@@ -153,7 +197,7 @@ INFO: Creating lib/puppet/functions/stdlib/validate_absolute_path.rb
153
197
  INFO: Creating lib/puppet/functions/stdlib/validate_array.rb
154
198
  INFO: Creating lib/puppet/functions/stdlib/validate_augeas.rb
155
199
  INFO: Creating lib/puppet/functions/stdlib/validate_bool.rb
156
- ERROR: The function attempted to load libraries outside the function block.
200
+ WARN: The function attempted to load libraries outside the function block.
157
201
  WARN: cannot load such file -- puppet/util/execution (ignored)
158
202
  INFO: Creating lib/puppet/functions/stdlib/validate_cmd.rb
159
203
  INFO: Creating lib/puppet/functions/stdlib/validate_domain_name.rb
@@ -30,7 +30,7 @@ mean to port all the legacy functions.
30
30
  loglevel = Logger::DEBUG
31
31
  end
32
32
 
33
- opts.on('-c', '--clean', 'Show debugging messages') do
33
+ opts.on('-c', '--clean', 'Delete legacy function files from disk') do
34
34
  options[:clean] = true
35
35
  end
36
36
 
@@ -4,8 +4,11 @@ require 'fileutils'
4
4
  class Pfu::Generator
5
5
  def self.write(opts)
6
6
  newpath = "lib/puppet/functions/#{opts[:namespace]}/#{opts[:name]}.rb"
7
- if File.exist?(newpath)
8
- $logger.error "Cowardly refusing to overwrite #{newpath}"
7
+ specpath = "spec/functions/#{opts[:namespace]}_#{opts[:name]}_spec.rb"
8
+
9
+ [newpath, specpath].each do |path|
10
+ next unless File.exist?(path)
11
+ $logger.error "Cowardly refusing to overwrite #{path}"
9
12
  return
10
13
  end
11
14
 
@@ -19,6 +22,9 @@ class Pfu::Generator
19
22
  template = File.join(File.dirname(__FILE__), '..', '..', 'templates', 'function.erb')
20
23
  contents = ERB.new(File.read(template), nil, '-').result(binding)
21
24
 
25
+ template = File.join(File.dirname(__FILE__), '..', '..', 'templates', 'function_spec.erb')
26
+ specfile = ERB.new(File.read(template), nil, '-').result(binding)
27
+
22
28
  $logger.info "Creating #{newpath}"
23
29
  $logger.debug "Function contents:\n#{contents}"
24
30
 
@@ -26,8 +32,13 @@ class Pfu::Generator
26
32
  # syntax check the code before writing it
27
33
  # TODO: validate that it actually creates a puppet function
28
34
  RubyVM::InstructionSequence.compile(contents)
35
+
29
36
  FileUtils.mkdir_p("lib/puppet/functions/#{opts[:namespace]}")
30
37
  File.write(newpath, contents)
38
+
39
+ FileUtils.mkdir_p("spec/functions")
40
+ File.write(specpath, specfile)
41
+
31
42
  return true
32
43
  rescue Exception => e
33
44
  $logger.error "Oh crap; the generated function isn't valid Ruby code!"
@@ -17,7 +17,7 @@ module Kernel
17
17
  def require(*a, &b)
18
18
  original_require(*a, &b)
19
19
  rescue LoadError => e
20
- $logger.error "The function attempted to load libraries outside the function block."
20
+ $logger.warn "The function attempted to load libraries outside the function block."
21
21
  $logger.warn "#{e.message} (ignored)"
22
22
  end
23
23
  end
@@ -1,3 +1,3 @@
1
1
  class Pfu
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe '<%= opts[:fullname] %>' do
4
+ # without knowing details about the implementation, this is the only test
5
+ # case that we can autogenerate. You should add more examples below!
6
+ it { is_expected.not_to eq(nil) }
7
+
8
+ #################################
9
+ # Below are some example test cases. You may uncomment and modify them to match
10
+ # your needs. Notice that they all expect the base error class of `StandardError`.
11
+ # This is because the autogenerated function uses an untyped array for parameters
12
+ # and relies on your implementation to do the validation. As you convert your
13
+ # function to proper dispatches and typed signatures, you should change the
14
+ # expected error of the argument validation examples to `ArgumentError`.
15
+ #
16
+ # Other error types you might encounter include
17
+ #
18
+ # * StandardError
19
+ # * ArgumentError
20
+ # * Puppet::ParseError
21
+ #
22
+ # Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
23
+ #
24
+ # it 'raises an error if called with no argument' do
25
+ # is_expected.to run.with_params.and_raise_error(StandardError)
26
+ # end
27
+ #
28
+ # it 'raises an error if there is more than 1 arguments' do
29
+ # is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
30
+ # end
31
+ #
32
+ # it 'raises an error if argument is not the proper type' do
33
+ # is_expected.to run.with_params('foo').and_raise_error(StandardError)
34
+ # end
35
+ #
36
+ # it 'returns the proper output' do
37
+ # is_expected.to run.with_params(123).and_return('the expected output')
38
+ # end
39
+ #################################
40
+
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-function-updater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Ford
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2019-11-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Run this command with a space separated list of either function file paths, or
@@ -33,6 +33,7 @@ files:
33
33
  - lib/puppet/functions/testing/module.rb
34
34
  - lib/puppet/functions/testing/noopts.rb
35
35
  - templates/function.erb
36
+ - templates/function_spec.erb
36
37
  homepage:
37
38
  licenses:
38
39
  - Apache-2.0