abide_dev_utils 0.2.3 → 0.3.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: 5e19e043b233c6842331d4c4883a74d8557b163739e041d910b60b953acddc85
4
- data.tar.gz: cf425a2b3a0c83ecd5e41cbee5d476fc7444e8984f4f03df12f91b36ad8aeee8
3
+ metadata.gz: c4ee71223292cc98028db5407eaef69cb2a0a150de9552b4d51d3b1ee17197bf
4
+ data.tar.gz: 3f56a0ad99e2ae3ccc8478210f7bb1961c4500267bedc1d42612c1947ee7c3fb
5
5
  SHA512:
6
- metadata.gz: 52e685b3d446b7c6256fd0e45d0542dccc77490b75ccc6408e2ba66e8edbc49d76267446940b997a0e3ab6a7449e1c5a03323c98cc66dd34ca58fa87178d4602
7
- data.tar.gz: 558295f45b37110781fcd22bc4cac8a450c520f706933ddc0fe908c4f2ef863b63029603ab3e1df09142a17c6238f3f5514f20e4fefcc1ed6e2a5531abc5a479
6
+ metadata.gz: 821d519b152ffc0a246939050b2b6117b5f3986fdf094d6333cfef8617b06c1572517a93b96f76039368c860715a4ef7ef39c78c039581369c4e61b484bce0c0
7
+ data.tar.gz: 6010ad3359da0de05b352e48210f3e4ddbd95b30e942598c6e6cfa77c175a1041ab87491d2a2afa15490a12fc197c549ab5d58dbb4bfc816e569afe5ac66dd9e
data/README.md CHANGED
@@ -128,6 +128,8 @@ Install the gem:
128
128
  * `--absolute-template-dir`, `-A` - Allows you to specify an absolute path with `--template-dir`. This is useful if your template directory is not relative to your module's root directory
129
129
  * `--template-name`, `-n` - Allows you to specify a template name if it is different than the `TYPE` parameter
130
130
  * `--vars`, `-V` - Comma-separated key=value pairs to pass in to the template renderer. This allows you to pass arbitrary values that can be used in your templates.
131
+ * `--spec-template`, `-S` - Path to an ERB template to use for rspec test generation instead of the default
132
+ * `--force`, `-f` - Skips any prompts and executes the command
131
133
 
132
134
  `abide puppet new` exposes a few variables for you to use in your templates by default:
133
135
 
@@ -179,8 +181,10 @@ $ ls manifests
179
181
  init.pp
180
182
  $ abide puppet new control_class 'test_module::controls::test_new_control'
181
183
  Created file /Users/the.dude/test_module/manifests/controls/test_new_control.pp
184
+ Created file /Users/the.dude/test_module/spec/classes/controls/test_new_control_spec.rb
182
185
  $ abide puppet new util_class 'test_module::utils::test_new_util' -v 'testvar1=dude,testvar2=sweet'
183
186
  Created file /Users/the.dude/test_module/manifests/utils/test_new_util.pp
187
+ Created file /Users/the.dude/test_module/spec/classes/utils/test_new_util_spec.rb
184
188
  $ cat manifests/controls/test_new_control.pp
185
189
  # @api private
186
190
  class test_module::controls::test_new_control (
@@ -204,6 +208,9 @@ class test_module::utils::test_new_util (
204
208
 
205
209
  ```
206
210
 
211
+ **NOTE**: You can use two special prefixes on your template files to denote where the rspec test should be generated for that object.
212
+ If the prefix `c-` is used, the test will be generated in the `spec/classes` directory. If the prefix `d-` is used, the test will be generated in the `spec/defines` directory. For example, to create a template for a defined type, name the template something like this: `d-my_defined_type.pp.erb`.
213
+
207
214
  ### XCCDF Command Reference
208
215
 
209
216
  #### to_hiera
@@ -87,6 +87,16 @@ module Abide
87
87
  '--vars [VARNAME=VALUE]',
88
88
  'Allows you to specify comma-separated variable names and values that will be converted into a hash that is available for you to use in your templates'
89
89
  ) { |v| @data[:vars] = v }
90
+ options.on(
91
+ '-S [PATH]',
92
+ '--spec-template [PATH]',
93
+ 'Path to an ERB template to use for rspec test generation instead of the default'
94
+ )
95
+ options.on(
96
+ '-f',
97
+ '--force',
98
+ 'Skips any prompts and executes the command'
99
+ ) { |_| @data[:force] = true }
90
100
  end
91
101
 
92
102
  def execute(type, name)
@@ -97,8 +107,7 @@ module Abide
97
107
  opts: @data,
98
108
  vars: @data.fetch(:vars, '').split(',').map { |i| i.split('=') }.to_h # makes the str a hash
99
109
  )
100
- result = builder.build
101
- Abide::CLI::OUTPUT.simple(result)
110
+ builder.build
102
111
  end
103
112
  end
104
113
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'erb'
4
4
  require 'pathname'
5
+ require 'abide_dev_utils/output'
5
6
  require 'abide_dev_utils/prompt'
6
7
  require 'abide_dev_utils/errors/ppt'
7
8
 
@@ -9,6 +10,10 @@ module AbideDevUtils
9
10
  module Ppt
10
11
  class NewObjectBuilder
11
12
  DEFAULT_EXT = '.pp'
13
+ VALID_EXT = /(\.pp|\.rb)\.erb$/.freeze
14
+ TMPL_PATTERN = /^[a-zA-Z][^\s]*\.erb$/.freeze
15
+ OBJ_PREFIX = /^(c-|d-)/.freeze
16
+ PREFIX_TEST_PATH = { 'c-' => 'classes', 'd-' => 'defines' }.freeze
12
17
 
13
18
  def initialize(obj_type, obj_name, opts: {}, vars: {})
14
19
  @obj_type = obj_type
@@ -19,23 +24,14 @@ module AbideDevUtils
19
24
  validate_class_vars
20
25
  end
21
26
 
22
- attr_reader :obj_type, :obj_name, :tmpl_name, :root_dir, :tmpl_dir, :tmpl_path, :type_path_map, :obj_path, :vars
23
-
24
- def render
25
- ERB.new(File.read(@tmpl_path.to_s), 0, '<>-').result(binding)
26
- end
27
+ attr_reader :obj_type, :obj_name, :root_dir, :tmpl_dir, :obj_path, :vars
27
28
 
28
29
  def build
29
- continue = File.exist?(obj_path) ? AbideDevUtils::Prompt.yes_no('File exists, would you like to overwrite?') : true
30
- return "Not overwriting file #{obj_path}" unless continue
31
-
32
- dir, = Pathname.new(obj_path).split
33
- Pathname.new(dir).mkpath unless Dir.exist?(dir)
34
- content = render
35
- File.open(obj_path, 'w') { |f| f.write(render) } unless content.empty?
36
- raise AbideDevUtils::Errors::Ppt::FailedToCreateFileError, obj_path unless File.file?(obj_path)
37
-
38
- "Created file #{obj_path}"
30
+ force = @opts.fetch(:force, false)
31
+ obj_cont = force ? true : continue?(obj_path)
32
+ spec_cont = force ? true : continue?(@tmpl_data[:spec_path])
33
+ write_file(obj_path, @tmpl_data[:path]) if obj_cont
34
+ write_file(@tmpl_data[:spec_path], @spec_tmpl) if spec_cont
39
35
  end
40
36
 
41
37
  # If a method gets called on the Hiera object which is not defined,
@@ -59,42 +55,99 @@ module AbideDevUtils
59
55
 
60
56
  private
61
57
 
58
+ def continue?(path)
59
+ continue = if File.exist?(path)
60
+ AbideDevUtils::Prompt.yes_no('File exists, would you like to overwrite?')
61
+ else
62
+ true
63
+ end
64
+ AbideDevUtils::Output.simple("Not overwriting file #{path}") unless continue
65
+
66
+ continue
67
+ end
68
+
69
+ def write_file(path, tmpl_path)
70
+ dir, = Pathname.new(path).split
71
+ Pathname.new(dir).mkpath unless Dir.exist?(dir)
72
+ content = render(tmpl_path)
73
+ File.open(path, 'w') { |f| f.write(content) } unless content.empty?
74
+ raise AbideDevUtils::Errors::Ppt::FailedToCreateFileError, path unless File.file?(path)
75
+
76
+ AbideDevUtils::Output.simple("Created file #{path}")
77
+ end
78
+
79
+ def build_obj; end
80
+
62
81
  def class_vars
63
- @tmpl_name = @opts.fetch(:tmpl_name, "#{@obj_type}.erb")
64
82
  @root_dir = Pathname.new(@opts.fetch(:root_dir, Dir.pwd))
65
83
  @tmpl_dir = if @opts.fetch(:absolute_template_dir, false)
66
84
  @opts.fetch(:tmpl_dir)
67
85
  else
68
- "#{@opts.fetch(:root_dir, Dir.pwd)}/#{@opts.fetch(:tmpl_dir, 'object_templates')}"
86
+ "#{@root_dir}/#{@opts.fetch(:tmpl_dir, 'object_templates')}"
69
87
  end
70
- @tmpl_path = Pathname.new("#{@tmpl_dir}/#{@opts.fetch(:tmpl_name, "#{@obj_type}.erb")}")
71
- @type_path_map = @opts.fetch(:type_path_map, {})
88
+ @tmpl_data = template_data(@opts.fetch(:tmpl_name, @obj_type))
72
89
  @obj_path = new_obj_path
90
+ @spec_tmpl = @opts.fetch(:spec_template, File.expand_path(File.join(__dir__, '../resources/generic_spec.erb')))
73
91
  end
74
92
 
75
93
  def validate_class_vars
76
94
  raise AbideDevUtils::Errors::PathNotDirectoryError, @root_dir unless Dir.exist? @root_dir
77
95
  raise AbideDevUtils::Errors::PathNotDirectoryError, @tmpl_dir unless Dir.exist? @tmpl_dir
78
- raise AbideDevUtils::Errors::Ppt::TemplateNotFoundError, @tmpl_path.to_s unless @tmpl_path.file?
79
96
  end
80
97
 
81
98
  def basename(obj_name)
82
99
  obj_name.split('::')[-1]
83
100
  end
84
101
 
85
- def new_obj_path
86
- if obj_type == 'class'
87
- obj_path_from_name
88
- else
89
- custom_obj_path
102
+ def prefix
103
+ pfx = basename.match(OBJ_PREFIX)
104
+ return pfx[1] unless pfx.empty?
105
+ end
106
+
107
+ def templates
108
+ return [] if Dir.entries(tmpl_dir).empty?
109
+
110
+ file_names = Dir.entries(tmpl_dir).select { |f| f.match?(TMPL_PATTERN) }
111
+ file_names.map { |i| File.join(tmpl_dir, i) }
112
+ end
113
+
114
+ def template_data(query)
115
+ raise AbideDevUtils::Errors::Ppt::TemplateNotFoundError, @tmpl_dir if Dir.entries(@tmpl_dir).empty?
116
+
117
+ data = {}
118
+ pattern = /#{Regexp.quote(query)}/
119
+ templates.each do |i|
120
+ pn = Pathname.new(i)
121
+ next unless pn.basename.to_s.match?(pattern)
122
+
123
+ data[:path] = pn.to_s
124
+ data[:fname] = pn.basename.to_s
90
125
  end
126
+ raise AbideDevUtils::Errors::Ppt::TemplateNotFoundError, @tmpl_dir unless data.key?(:fname)
127
+
128
+ data[:ext] = data[:fname].match?(VALID_EXT) ? data[:fname].match(VALID_EXT)[1] : '.pp'
129
+ data[:pfx] = data[:fname].match?(OBJ_PREFIX) ? data[:fname].match(OBJ_PREFIX)[1] : 'c-'
130
+ data[:spec_base] = PREFIX_TEST_PATH[data[:pfx]]
131
+ data[:obj_name] = normalize_obj_name(data.dup)
132
+ data[:spec_name] = "#{data[:obj_name].slice(/([^\s]+)(?:#{Regexp.quote(data[:ext])})/, 1)}_spec.rb"
133
+ data[:spec_path] = spec_path(data[:spec_base], data[:spec_name])
134
+ data
135
+ end
136
+
137
+ def normalize_obj_name(data)
138
+ new_name = data[:fname].slice(/^(?:#{Regexp.quote(data[:pfx])})?(?<name>[^\s.]+)(?:#{Regexp.quote(data[:ext])})?\.erb$/, 'name')
139
+ "#{new_name}#{data[:ext]}"
140
+ end
141
+
142
+ def render(path)
143
+ ERB.new(File.read(path), 0, '<>-').result(binding)
91
144
  end
92
145
 
93
146
  def namespace_format(name)
94
147
  name.split(':').reject(&:empty?).join('::')
95
148
  end
96
149
 
97
- def obj_path_from_name
150
+ def new_obj_path
98
151
  parts = @obj_name.split('::')[1..-2]
99
152
  parts.insert(0, 'manifests')
100
153
  parts.insert(-1, "#{basename(@obj_name)}#{DEFAULT_EXT}")
@@ -102,27 +155,13 @@ module AbideDevUtils
102
155
  path.to_s
103
156
  end
104
157
 
105
- def custom_obj_path
106
- map_val = type_path_map.fetch(@obj_type.to_sym, nil)
107
- return obj_path_from_name if map_val.nil?
108
-
109
- if map_val.respond_to?(:key?)
110
- custom_obj_path_from_hash(map_val, @obj_name)
111
- else
112
- abs_path = Pathname.new(map_val).absolute? ? map_val : "#{Dir.pwd}/#{map_val}"
113
- "#{abs_path}/#{basename(@obj_name)}#{DEFAULT_EXT}"
114
- end
115
- end
116
-
117
- def custom_obj_path_from_hash(map_val, obj_name)
118
- raise AbideDevUtils::Errors::Ppt::CustomObjPathKeyError, map_val unless map_val.key?(:path)
119
-
120
- abs_path = Pathname.new(map_val[:path]).absolute? ? map_val[:path] : "#{Dir.pwd}/#{map_val[:path]}"
121
- if map_val.key?(:extension)
122
- "#{abs_path}/#{basename(obj_name)}#{map_val[:extension]}"
123
- else
124
- "#{abs_path}/#{basename(obj_name)}#{DEFAULT_EXT}"
125
- end
158
+ def spec_path(base_dir, spec_name)
159
+ parts = @obj_name.split('::')[1..-2]
160
+ parts.insert(0, 'spec')
161
+ parts.insert(1, base_dir)
162
+ parts.insert(-1, spec_name)
163
+ path = @root_dir + Pathname.new(parts.join('/'))
164
+ path.to_s
126
165
  end
127
166
  end
128
167
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe '<%= @obj_name %>' do
6
+ on_supported_os.each do |os, os_facts|
7
+ context "on #{os}" do
8
+ let(:facts) { os_facts }
9
+
10
+ it { is_expected.to compile }
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AbideDevUtils
4
- VERSION = "0.2.3"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -114,7 +114,7 @@ module AbideDevUtils
114
114
 
115
115
  def normalize_str(str)
116
116
  nstr = str.downcase
117
- nstr.gsub!(/[^a-z]$/, '')
117
+ nstr.gsub!(/[^a-z0-9]$/, '')
118
118
  nstr.gsub!(/^[^a-z]/, '')
119
119
  nstr.gsub!(/^(l1_|l2_|ng_)/, '')
120
120
  nstr.delete!('(/|\\)')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abide_dev_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Heston Snodgrass
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-29 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -278,6 +278,7 @@ files:
278
278
  - lib/abide_dev_utils/ppt/coverage.rb
279
279
  - lib/abide_dev_utils/ppt/new_obj.rb
280
280
  - lib/abide_dev_utils/prompt.rb
281
+ - lib/abide_dev_utils/resources/generic_spec.erb
281
282
  - lib/abide_dev_utils/utils/general.rb
282
283
  - lib/abide_dev_utils/validate.rb
283
284
  - lib/abide_dev_utils/version.rb