Class: CookbookDevelopment::TestTasks

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/cookbook/development/rake/test_tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ TestTasks

Returns a new instance of TestTasks.

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cookbook/development/rake/test_tasks.rb', line 15

def initialize
  @project_dir   = Dir.pwd
  @chef_dir      = File.join(project_dir, 'test', '.chef')
  @knife_cfg     = File.join(chef_dir, 'knife.rb')
  @vendor_dir    = File.join(project_dir, 'vendor')
  @cookbooks_dir = File.join(vendor_dir, 'cookbooks')
  @berks_file    = File.join(project_dir, 'Berksfile')
  @metadata_file = File.join(project_dir, 'metadata.rb')

  yield(self) if block_given?
  define
end

Instance Attribute Details

#berks_fileObject (readonly)

Returns the value of attribute berks_file.



13
14
15
# File 'lib/cookbook/development/rake/test_tasks.rb', line 13

def berks_file
  @berks_file
end

#chef_dirObject (readonly)

Returns the value of attribute chef_dir.



9
10
11
# File 'lib/cookbook/development/rake/test_tasks.rb', line 9

def chef_dir
  @chef_dir
end

#cookbooks_dirObject (readonly)

Returns the value of attribute cookbooks_dir.



12
13
14
# File 'lib/cookbook/development/rake/test_tasks.rb', line 12

def cookbooks_dir
  @cookbooks_dir
end

#knife_cfgObject (readonly)

Returns the value of attribute knife_cfg.



10
11
12
# File 'lib/cookbook/development/rake/test_tasks.rb', line 10

def knife_cfg
  @knife_cfg
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



8
9
10
# File 'lib/cookbook/development/rake/test_tasks.rb', line 8

def project_dir
  @project_dir
end

#vendor_dirObject (readonly)

Returns the value of attribute vendor_dir.



11
12
13
# File 'lib/cookbook/development/rake/test_tasks.rb', line 11

def vendor_dir
  @vendor_dir
end

Instance Method Details

#defineObject



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
56
57
58
59
60
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
89
90
91
92
93
94
95
96
97
# File 'lib/cookbook/development/rake/test_tasks.rb', line 28

def define

  kitchen_config = Kitchen::Config.new
  Kitchen.logger = Kitchen.default_file_logger

  namespace "kitchen" do
    kitchen_config.instances.each do |instance|
      desc "Run #{instance.name} test instance"
      task instance.name do
        instance.test(:passing)
      end
    end

    desc "Run all test instances"
    task "all" => kitchen_config.instances.map { |i| i.name }
  end

  desc 'Runs knife cookbook test'
  task :knife_test => [knife_cfg, :berks_install] do |task|
    cookbook_name = File.basename(project_dir)
    IO.readlines(@metadata_file).each do |line|
      cookbook_name = line.split[1] if line.split[0] == 'name'
    end  
    Dir.chdir(File.join(project_dir, '..')) do
      sh "bundle exec knife cookbook test #{cookbook_name} --config #{knife_cfg}"
    end
  end

  desc 'Runs Foodcritic linting'
  FoodCritic::Rake::LintTask.new do |task|
    task.options = {:search_gems => true, :fail_tags => ['any'], :tags => ['~FC003', '~FC015'], :exclude_paths => ['vendor/cookbooks/**/*']}
  end

  desc 'Runs unit tests'
  RSpec::Core::RakeTask.new(:unit) do |task|
    task.pattern = FileList[File.join(project_dir, 'test', 'unit', '**/*_spec.rb')]
  end
  task :unit => :berks_install

  desc 'Runs integration tests'
  task :integration do
    Rake::Task['kitchen:all'].invoke
  end

  desc 'Run all tests and linting'
  task :test do
    Rake::Task['knife_test'].invoke
    Rake::Task['foodcritic'].invoke
    Rake::Task['unit'].invoke
    Rake::Task['integration'].invoke
  end

  task :berks_install do |task|
    FileUtils.rm_rf vendor_dir
    Berkshelf::Berksfile.from_file(berks_file).install(:path => cookbooks_dir)
  end

  directory chef_dir

  file knife_cfg => chef_dir do |task|
    File.open(task.name, 'w+') do |file|
      file.write <<-EOF.gsub(/^\s+/, "")
      cache_type 'BasicFile'
      cache_options(:path => "\#{ENV['HOME']}/.chef/checksums")
      cookbook_path '#{cookbooks_dir}'
      EOF
    end
  end

end