Class: ChefSpec::Policyfile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/chefspec/policyfile.rb

Instance Method Summary collapse

Constructor Details

#initializePolicyfile

Returns a new instance of Policyfile.



18
19
20
# File 'lib/chefspec/policyfile.rb', line 18

def initialize
  @tmpdir = Dir.mktmpdir
end

Instance Method Details

#setup!Object

Setup and install the necessary dependencies in the temporary directory



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chefspec/policyfile.rb', line 25

def setup!
  policyfile_path = RSpec.configuration.policyfile_path
  if policyfile_path.nil?
    policyfile_path = File.join(Dir.pwd, "Policyfile.rb")
  end

  Chef::WorkstationConfigLoader.new(nil).load

  installer = ChefCLI::PolicyfileServices::Install.new(
    policyfile: policyfile_path,
    ui: ChefCLI::UI.null,
    config: Chef::Config
  )

  installer.run

  exporter = ChefCLI::PolicyfileServices::ExportRepo.new(
    policyfile: policyfile_path,
    export_dir: @tmpdir
  )

  FileUtils.rm_rf(@tmpdir)
  exporter.run

  ::RSpec.configure do |config|
    config.cookbook_path = [
      File.join(@tmpdir, "cookbooks"),
      File.join(@tmpdir, "cookbook_artifacts"),
    ]
  end
end

#teardown!Object

Remove the temporary directory



60
61
62
# File 'lib/chefspec/policyfile.rb', line 60

def teardown!
  FileUtils.rm_rf(@tmpdir) if File.exist?(@tmpdir)
end