Sha256: 12924ad0cb8bd58c37b8a051fada0c864f44a52ae0bc5f752ea17095800b0311

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

#!/usr/bin/env ruby

require 'erb'
require 'fileutils'
require 'ostruct'

module ChefSpec
  class Bootstrap
    def generate(cookbooks_dir, spec_dir, template_file)
      if not Dir.exist?(cookbooks_dir)
        abort "Unable to locate your cookbooks directory (#{cookbooks_dir})"
      end

      if not template_file
        template_file = root.join('templates', 'default.erb')
      end

      if not File.exist?(template_file)
        abort "Unable to locate template file (#{template_file})"
      end

      erb = ERB.new(File.read(template_file))

      Dir.glob("#{cookbooks_dir}/*/recipes/*").each do |path|
        path, recipe_file = File.split(path)
        recipe = recipe_file.split('.')[0]
        cookbook = path.split(File::SEPARATOR)[1]

        filename = "#{spec_dir}/#{cookbook}/#{recipe}_spec.rb"

        if not File.exist?(filename)
          FileUtils.mkdir_p "#{spec_dir}/#{cookbook}"

          puts "Creating spec file: #{filename}"

          File.open(filename, "w") do |spec_file|
            spec_file.write(erb.result(binding))
          end
        end
      end
    end

    def root
      @root ||= Pathname.new(File.expand_path('../../', __FILE__))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chefspec-bootstrap-0.0.1 lib/chefspec-bootstrap.rb