Class: Retrospec

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-retrospec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, default_template_dir = ENV['RETROSPEC_TEMPLATES_PATH']) ⇒ Retrospec

Returns a new instance of Retrospec.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet-retrospec.rb', line 17

def initialize(path=nil, default_template_dir=ENV['RETROSPEC_TEMPLATES_PATH'])
  # user supplied a template path or user wants to use local templates
  if not default_template_dir.nil? or ENV['RETROSPEC_ENABLE_LOCAL_TEMPLATES'] =~ /true/i
    default_template_dir = Helpers.setup_user_template_dir(default_template_dir)
  end
  @default_path = path
  @default_modules = ['stdlib']
  @template_dir = default_template_dir
  module_name
  modules_included
end

Instance Attribute Details

#classes_and_defines(file) ⇒ Object (readonly)

Returns the value of attribute classes_and_defines.



8
9
10
# File 'lib/puppet-retrospec.rb', line 8

def classes_and_defines
  @classes_and_defines
end

#default_modulesObject

Returns the value of attribute default_modules.



13
14
15
# File 'lib/puppet-retrospec.rb', line 13

def default_modules
  @default_modules
end

#default_pathObject

Returns the value of attribute default_path.



11
12
13
# File 'lib/puppet-retrospec.rb', line 11

def default_path
  @default_path
end

#included_declarations(file) ⇒ Object (readonly)

finds all the included resources so we can test and depend on in the fixtures file



126
127
128
# File 'lib/puppet-retrospec.rb', line 126

def included_declarations
  @included_declarations
end

#manifest_filesObject

Returns the value of attribute manifest_files.



12
13
14
# File 'lib/puppet-retrospec.rb', line 12

def manifest_files
  @manifest_files
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



9
10
11
# File 'lib/puppet-retrospec.rb', line 9

def module_name
  @module_name
end

#modules_includedObject (readonly)

Returns the value of attribute modules_included.



10
11
12
# File 'lib/puppet-retrospec.rb', line 10

def modules_included
  @modules_included
end

#template_dirObject

if user doesn’t supply template directory we assume we should use the templates in this gem



56
57
58
# File 'lib/puppet-retrospec.rb', line 56

def template_dir
  @template_dir
end

Class Method Details

.run(path = nil, template_dir = ) ⇒ Object

pass in either the path to the module directory or the path to a specific manifest defaults to all manifests in the current directory if ENV = ‘true’ the use the default user templates path if ENV is set then we will override the default user template path with the path provided we will use the necessary templates from that directory instead of the default gem path



46
47
48
49
# File 'lib/puppet-retrospec.rb', line 46

def self.run(path=nil, template_dir=ENV['RETROSPEC_TEMPLATES_PATH'])
  spec = Retrospec.new(path, template_dir)
  spec.create_files
end

Instance Method Details

#create_filesObject



29
30
31
32
33
34
35
36
37
# File 'lib/puppet-retrospec.rb', line 29

def create_files
  safe_create_spec_helper
  safe_create_fixtures_file
  safe_create_gemfile
  manifest_files.each do |file|
    safe_create_resource_spec_files(file)
  end
  safe_make_shared_context
end

#manifest_dirObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/puppet-retrospec.rb', line 217

def manifest_dir
  # look and compare, then get basename, loop till found
  if @manifest_dir.nil?
    file = manifest_files.first
    file.split(File::SEPARATOR).each do |part|
      filename = File.basename(file)
      if filename != 'manifests'
        file = File.dirname(file)
      else
        @manifest_dir = file
      end
    end
  end
  @manifest_dir
end

#module_dirObject

calculates where the spec directory is by going one directory back from manifests directory



213
214
215
# File 'lib/puppet-retrospec.rb', line 213

def module_dir
  @spec_dir ||= File.join(File.dirname(manifest_dir))
end

#referenced_modulesObject



64
65
66
# File 'lib/puppet-retrospec.rb', line 64

def referenced_modules
  []
end

#safe_create_fixtures_file(template = 'fixtures_file.erb') ⇒ Object



189
190
191
# File 'lib/puppet-retrospec.rb', line 189

def safe_create_fixtures_file(template='fixtures_file.erb')
  safe_create_template_file('.fixtures.yml', template)
end

#safe_create_gemfile(template = 'gemfile.erb') ⇒ Object



51
52
53
# File 'lib/puppet-retrospec.rb', line 51

def safe_create_gemfile(template='gemfile.erb')
  safe_create_template_file('Gemfile', template)
end

#safe_create_resource_spec_files(manifest_file, template = 'resource_spec_file.erb') ⇒ Object

Gets all the classes and define types from all the files in the manifests directory Creates an associated spec file for each type and even creates the subfolders for nested classes one::two::three



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/puppet-retrospec.rb', line 158

def safe_create_resource_spec_files(manifest_file,template='resource_spec_file.erb')
  classes_dir = 'spec/classes'
  defines_dir = 'spec/defines'
  classes_and_defines(manifest_file).each do |value|
    types = value[:types]
    types.each do |type|
      # run template
      tokens = type[:name].split('::')
      if type[:type_name] == 'class'
        type_dir_name = classes_dir
      else
        type_dir_name = defines_dir
      end
      file_name = tokens.pop  # the last item should be the filename
      # if there are only two tokens ie. tomcat::params we dont need to create a subdirectory
      if tokens.count > 1
        # this is a deep level resource ie. tomcat::config::server::connector
        # however we don't need the tomcat directory so we can just remove it
        # this should leave us with config/server/connector_spec.rb
        tokens.delete_at(0)
        # so lets make a directory structure out of it
        dir_name = File.join(tokens)  # config/server
        dir_name = File.join(type_dir_name,dir_name) # spec/classes/tomcat/config/server
        safe_create_template_file(File.join(dir_name,"#{file_name}_spec.rb"), template)
      else
        safe_create_template_file(File.join(type_dir_name,"#{file_name}_spec.rb"), template)
      end
    end
  end
end

#safe_create_spec_helper(template = 'spec_helper_file.erb') ⇒ Object



193
194
195
# File 'lib/puppet-retrospec.rb', line 193

def safe_create_spec_helper(template='spec_helper_file.erb')
  safe_create_template_file('spec/spec_helper.rb', template)
end

#safe_create_template_file(path, template) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/puppet-retrospec.rb', line 197

def safe_create_template_file(path, template)
  # check to ensure parent directory exists
  file_dir_path = File.expand_path(File.join(module_dir,File.dirname(path)))
  if ! File.exists?(file_dir_path)
    Helpers.safe_mkdir(file_dir_path)
  end
  template_path = File.join(template_dir, template)
  File.open(template_path) do |file|
    renderer = ERB.new(file.read, 0, '-')
    content = renderer.result binding
    Helpers.safe_create_file(File.expand_path(File.join(module_dir,path)), content)
  end

end

#safe_make_shared_context(template = 'shared_context.erb') ⇒ Object



152
153
154
# File 'lib/puppet-retrospec.rb', line 152

def safe_make_shared_context(template='shared_context.erb')
  safe_create_template_file('spec/shared_contexts.rb', template)
end