Module: PDQTest::Skeleton

Defined in:
lib/pdqtest/skeleton.rb

Constant Summary collapse

FIXTURES =
'.fixtures.yml'
BACKUP_EXT =
'.pdqtest_old'
SPEC_DIR =
'spec'
ACCEPTANCE_DIR =
File.join(SPEC_DIR, 'acceptance')
CLASSES_DIR =
File.join(SPEC_DIR, 'classes')
SKELETON_DIR =
File.join('res', 'skeleton')
EXAMPLES_DIR =
'examples'
GEMFILE =
'Gemfile'
GEMFILE_LINE =
"gem 'pdqtest', '#{PDQTest::VERSION}'"

Class Method Summary collapse

Class Method Details

.initObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pdqtest/skeleton.rb', line 72

def self.init

  # move .fixtures.yml out of the way
  if File.exists?(FIXTURES)
    FileUtils.mv(FIXTURES, FIXTURES + BACKUP_EXT)
  end

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


  # skeleton files if required
  install_skeleton('Rakefile', 'Rakefile')
  install_skeleton(File.join('spec', 'spec_helper.rb'), 'spec_helper.rb')
  install_skeleton(File.join('spec', 'acceptance', 'init.bats'), 'init.bats', false)
  install_skeleton(File.join('spec', 'acceptance', 'init__before.bats'), 'init__before.bats', false)
  install_skeleton(File.join('spec', 'acceptance', 'init__setup.sh'), 'init__setup.sh', false)

  install_example()
  install_gemfile()

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

.install_exampleObject



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

def self.install_example
  example_file = File.join(EXAMPLES_DIR, 'init.pp')
  if ! File.exists?(example_file)
    init_pp = "      \#{PDQTest::Puppet::MAGIC_MARKER}\n      include \#{PDQTest::Puppet.module_name}\n    END\n    File.write(example_file, init_pp)\n  end\nend\n"

.install_gemfileObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pdqtest/skeleton.rb', line 55

def self.install_gemfile
  insert_gem = false
  if File.exists?(GEMFILE)
    if ! File.readlines(GEMFILE).grep(/pdqtest/).any?
      insert_gem = true
    end
  else
    install_skeleton(GEMFILE, GEMFILE)
    insert_gem = true
  end
  if insert_gem
    open(GEMFILE, 'a') { |f|
      f.puts GEMFILE_LINE
    }
  end
end

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



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

def self.install_skeleton(target_file, skeleton, replace=true)
  skeleton_file = resource_path(File.join('skeleton', skeleton))
  if File.exists?(target_file) and replace and should_replace_file(target_file, skeleton_file)
    # move existing file out of the way
    FileUtils.mv(target_file, target_file + BACKUP_EXT)
    install = true
  else
    install = true
  end
  if install
    FileUtils.cp(skeleton_file, target_file)
  end
end

.resource_path(resource) ⇒ Object



26
27
28
# File 'lib/pdqtest/skeleton.rb', line 26

def self.resource_path(resource)
  File.join(File.dirname(File.expand_path(__FILE__)), "../../res/#{resource}")
end

.should_replace_file(target, skeleton) ⇒ Object



19
20
21
22
23
24
# File 'lib/pdqtest/skeleton.rb', line 19

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

  target_hash != skeleton_hash
end