diff --git a/lib/puppet/module_tool/applications/builder.rb b/lib/puppet/module_tool/applications/builder.rb index 30d40e968..6ca9cab65 100644 --- a/lib/puppet/module_tool/applications/builder.rb +++ b/lib/puppet/module_tool/applications/builder.rb @@ -1,89 +1,93 @@ require 'fileutils' require 'json' module Puppet::ModuleTool module Applications class Builder < Application def initialize(path, options = {}) @path = File.expand_path(path) @pkg_path = File.join(@path, 'pkg') super(options) end def run load_metadata! create_directory copy_contents write_json Puppet.notice "Building #{@path} for release" pack relative = Pathname.new(archive_file).relative_path_from(Pathname.new(File.expand_path(Dir.pwd))) # Return the Pathname object representing the path to the release # archive just created. This return value is used by the module_tool # face build action, and displayed to on the console using the to_s # method. # # Example return value: # # # relative end private def archive_file File.join(@pkg_path, "#{metadata.release_name}.tar.gz") end def pack FileUtils.rm archive_file rescue nil tar = Puppet::ModuleTool::Tar.instance Dir.chdir(@pkg_path) do tar.pack(metadata.release_name, archive_file) end end def create_directory FileUtils.mkdir(@pkg_path) rescue nil if File.directory?(build_path) FileUtils.rm_rf(build_path, :secure => true) end FileUtils.mkdir(build_path) end def copy_contents Dir[File.join(@path, '*')].each do |path| case File.basename(path) when *Puppet::ModuleTool::ARTIFACTS next else FileUtils.cp_r path, build_path, :preserve => true end end end def write_json metadata_path = File.join(build_path, 'metadata.json') + if metadata.to_hash.include? 'checksums' + Puppet.warning "A 'checksums' field was found in metadata.json. This field will be ignored and can safely be removed." + end + # TODO: This may necessarily change the order in which the metadata.json # file is packaged from what was written by the user. This is a # regretable, but required for now. File.open(metadata_path, 'w') do |f| f.write(metadata.to_json) end File.open(File.join(build_path, 'checksums.json'), 'w') do |f| - f.write(PSON.pretty_generate(Checksums.new(@path))) + f.write(PSON.pretty_generate(Checksums.new(build_path))) end end def build_path @build_path ||= File.join(@pkg_path, metadata.release_name) end end end end diff --git a/spec/unit/module_tool/applications/builder_spec.rb b/spec/unit/module_tool/applications/builder_spec.rb index e2b63c192..b9d7d3d79 100644 --- a/spec/unit/module_tool/applications/builder_spec.rb +++ b/spec/unit/module_tool/applications/builder_spec.rb @@ -1,66 +1,88 @@ require 'spec_helper' require 'puppet/module_tool/applications' require 'puppet_spec/modules' describe Puppet::ModuleTool::Applications::Builder do include PuppetSpec::Files let(:path) { tmpdir("working_dir") } let(:module_name) { 'mymodule-mytarball' } let(:version) { '0.0.1' } let(:release_name) { "#{module_name}-#{version}" } let(:tarball) { File.join(path, 'pkg', release_name) + ".tar.gz" } let(:builder) { Puppet::ModuleTool::Applications::Builder.new(path) } + shared_examples "a packagable module" do + def target_exists?(file) + File.exist?(File.join(path, "pkg", "#{module_name}-#{version}", file)) + end + + it "packages the module in a tarball named after the module" do + tarrer = mock('tarrer') + Puppet::ModuleTool::Tar.expects(:instance).returns(tarrer) + Dir.expects(:chdir).with(File.join(path, 'pkg')).yields + tarrer.expects(:pack).with(release_name, tarball) + + builder.run + end + end + context 'with metadata.json' do before :each do File.open(File.join(path, 'metadata.json'), 'w') do |f| f.puts({ "name" => "#{module_name}", "version" => "#{version}", "source" => "http://github.com/testing/#{module_name}", "author" => "testing", "license" => "Apache License Version 2.0", "summary" => "Puppet testing module", "description" => "This module can be used for basic testing", "project_page" => "http://github.com/testing/#{module_name}" }.to_json) end end - it "packages the module in a tarball named after the module" do - tarrer = mock('tarrer') - Puppet::ModuleTool::Tar.expects(:instance).returns(tarrer) - Dir.expects(:chdir).with(File.join(path, 'pkg')).yields - tarrer.expects(:pack).with(release_name, tarball) + it_behaves_like "a packagable module" + end - builder.run + context 'with metadata.json containing checksums' do + before :each do + File.open(File.join(path, 'metadata.json'), 'w') do |f| + f.puts({ + "name" => "#{module_name}", + "version" => "#{version}", + "source" => "http://github.com/testing/#{module_name}", + "author" => "testing", + "license" => "Apache License Version 2.0", + "summary" => "Puppet testing module", + "description" => "This module can be used for basic testing", + "project_page" => "http://github.com/testing/#{module_name}", + "checksums" => {"README.md" => "deadbeef"} + }.to_json) + end end + + it_behaves_like "a packagable module" end + context 'with Modulefile' do before :each do File.open(File.join(path, 'Modulefile'), 'w') do |f| f.write <<-MODULEFILE name '#{module_name}' version '#{version}' source 'http://github.com/testing/#{module_name}' author 'testing' license 'Apache License Version 2.0' summary 'Puppet testing module' description 'This module can be used for basic testing' project_page 'http://github.com/testing/#{module_name}' MODULEFILE end end - it "packages the module in a tarball named after the module" do - tarrer = mock('tarrer') - Puppet::ModuleTool::Tar.expects(:instance).returns(tarrer) - Dir.expects(:chdir).with(File.join(path, 'pkg')).yields - tarrer.expects(:pack).with(release_name, tarball) - - builder.run - end + it_behaves_like "a packagable module" end end