Module: PDQTest::Skeleton

Defined in:
lib/pdqtest/skeleton.rb

Constant Summary collapse

FIXTURES =
'.fixtures.yml'
SPEC_DIR =
'spec'
ACCEPTANCE_DIR =
File.join(SPEC_DIR, 'acceptance')
CLASSES_DIR =
File.join(SPEC_DIR, 'classes')
SKELETON_DIR =
'skeleton'
EXAMPLES_DIR =
'examples'
GEMFILE =
'Gemfile'
HIERA_DIR =
File.join(SPEC_DIR, 'fixtures', 'hieradata')
HIERA_YAML =
'hiera.yaml'
HIERA_TEST =
'test.yaml'

Class Method Summary collapse

Class Method Details

.generate_acceptance(example = nil) ⇒ Object

Scan the examples directory and create a set of acceptance tests. If a specific file is given as ‘example` then only the listed example will be processed (and it will be created if missing). If files already exist, they will not be touched



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pdqtest/skeleton.rb', line 109

def self.generate_acceptance(example=nil)
  examples = []
  if example
    # specific file only
    examples << example
  else
    # Each .pp file in /examples (don't worry about magic markers yet, user
    # will be told on execution if no testscases are present as a reminder to
    # add it
    examples += Dir["examples/*.pp"]
  end

  examples.each { |e|
    install_acceptance(e)
  }
end

.initObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdqtest/skeleton.rb', line 61

def self.init

  # move .fixtures.yml out of the way
  if File.exists?(FIXTURES)
    File.delete(FIXTURES)
  end

  # make directory structure for testcases
  FileUtils.mkdir_p(ACCEPTANCE_DIR)
  FileUtils.mkdir_p(CLASSES_DIR)
  FileUtils.mkdir_p(EXAMPLES_DIR)
  FileUtils.mkdir_p(HIERA_DIR)


  # skeleton files if required
  install_skeleton('Rakefile', 'Rakefile')
  install_skeleton(File.join('spec', 'spec_helper.rb'), 'spec_helper.rb')
  install_skeleton('.gitignore', 'dot_gitignore')
  install_skeleton('.rspec', 'dot_rspec')
  install_skeleton(File.join(SPEC_DIR, 'fixtures', HIERA_YAML), HIERA_YAML)
  install_skeleton(File.join(HIERA_DIR, HIERA_TEST), HIERA_TEST)

  install_acceptance()
  install_gemfile()
  install_integrations()

  # Make sure there is a Gemfile and we are in it
end

.install_acceptance(example_file = "init.pp") ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/pdqtest/skeleton.rb', line 96

def self.install_acceptance(example_file ="init.pp")
  example_name = File.basename(example_file).gsub(/\.pp$/, '')
  install_template("#{EXAMPLES_DIR}/#{File.basename(example_file)}",'examples_init.pp.erb', {})

  install_skeleton(File.join('spec', 'acceptance', "#{example_name}.bats"), 'init.bats', false)
  install_skeleton(File.join('spec', 'acceptance', "#{example_name}__before.bats"), 'init__before.bats', false)
  install_skeleton(File.join('spec', 'acceptance', "#{example_name}__setup.sh"), 'init__setup.sh', false)
end

.install_gemfileObject



54
55
56
57
58
59
# File 'lib/pdqtest/skeleton.rb', line 54

def self.install_gemfile
  install_skeleton(GEMFILE, GEMFILE)

  # upgrade the gemfile to *this* version of pdqtest + puppet-strings
  Upgrade.upgrade()
end

.install_integrationsObject



90
91
92
93
94
# File 'lib/pdqtest/skeleton.rb', line 90

def self.install_integrations()
  install_skeleton('Makefile', 'Makefile')
  install_skeleton('bitbucket-pipelines.yml', 'bitbucket-pipelines.yml')
  install_skeleton('.travis.yml', 'dot_travis.yml')
end

.install_skeleton(target_file, skeleton, replace = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdqtest/skeleton.rb', line 29

def self.install_skeleton(target_file, skeleton, replace=true)
  skeleton_file = Util::resource_path(File.join(SKELETON_DIR, skeleton))
  install = false
  if File.exists?(target_file)
    if replace and should_replace_file(target_file, skeleton_file)
      install = true
    end
  else
    install = true
  end
  if install
    FileUtils.cp(skeleton_file, target_file)
  end
end

.install_template(target, template_file, vars) ⇒ Object

vars is a hash of variables that can be accessed in template



45
46
47
48
49
50
51
52
# File 'lib/pdqtest/skeleton.rb', line 45

def self.install_template(target, template_file, vars)
  example_file = File.join(EXAMPLES_DIR, template_file)
  if ! File.exists?(target)
    template = File.read(Util::resource_path(File.join('templates', template_file)))
    content  = ERB.new(template, nil, '-').result(binding)
    File.write(target, content)
  end
end

.should_replace_file(target, skeleton) ⇒ Object



22
23
24
25
26
27
# File 'lib/pdqtest/skeleton.rb', line 22

def self.should_replace_file(target, skeleton)
  target_hash   = Digest::SHA256.file target
  skeleton_hash = Digest::SHA256.file skeleton

  target_hash != skeleton_hash
end