Skip to content

Commit

Permalink
Merge pull request #407 from rodjek/pdk-748
Browse files Browse the repository at this point in the history
(PDK-748) Wireframe `pdk build` CLI
  • Loading branch information
scotje authored Jan 19, 2018
2 parents 84739f5 + d0a3e1a commit 2b55f1a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def self.skip_interview_option(dsl)
end

require 'pdk/cli/bundle'
require 'pdk/cli/build'
require 'pdk/cli/convert'
require 'pdk/cli/new'
require 'pdk/cli/test'
Expand Down
43 changes: 43 additions & 0 deletions lib/pdk/cli/build.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'pdk/cli/util'

module PDK::CLI
@build_cmd = @base_cmd.define_command do
name 'build'
usage _('build [options]')
summary _('Builds a package from the module that can be published to the Puppet Forge.')

option nil, 'target-dir',
_('The target directory where you want PDK to write the package.'),
argument: :required, default: File.join(Dir.pwd, 'pkg')

be_hidden

run do |opts, _args, _cmd|
require 'pdk/module/build'

PDK::CLI::Util.ensure_in_module!(
message: _('`pdk build` can only be run from inside a valid module directory.'),
log_level: :info,
)

module_metadata = PDK::Module::Metadata.from_file('metadata.json')

# TODO: Ensure forge metadata has been set, or call out to interview
# to set it.
#
# module_metadata.interview_for_forge! unless module_metadata.forge_ready?

PDK.logger.info _('Building %{module_name} version %{module_version}') % {
module_name: module_metadata.data['name'],
module_version: module_metadata.data['version'],
}

package_path = PDK::Module::Build.invoke(opts)

PDK.logger.info _('Build of %{package_name} has completed successfully. Built package can be found here: %{package_path}') % {
package_name: 'something',
package_path: package_path,
}
end
end
end
9 changes: 9 additions & 0 deletions lib/pdk/module/build.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module PDK
module Module
class Build
def self.invoke(_options)
'pkg/dummy-path'
end
end
end
end
64 changes: 64 additions & 0 deletions spec/unit/pdk/cli/build_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

describe 'PDK::CLI build' do
let(:help_text) { a_string_matching(%r{^USAGE\s+pdk build}m) }

context 'when not run from inside a module' do
include_context 'run outside module'

it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{must be run from inside a valid module}))

expect {
PDK::CLI.run(%w[build])
}.to raise_error(SystemExit) { |error|
expect(error.status).not_to eq(0)
}
end
end

context 'when run from inside a module' do
before(:each) do
mock_metadata_obj = instance_double(PDK::Module::Metadata, data: mock_metadata)

allow(PDK::Util).to receive(:module_root).and_return('/path/to/test/module')
allow(PDK::Module::Metadata).to receive(:from_file).with('metadata.json').and_return(mock_metadata_obj)
allow(PDK::Module::Build).to receive(:invoke).with(:'target-dir' => File.join(Dir.pwd, 'pkg')).and_return(package_path)
end

after(:each) do
PDK::CLI.run(['build'] + command_opts)
end

let(:command_opts) { [] }
let(:mock_metadata) do
{
'name' => 'testuser-testmodule',
'version' => '2.3.4',
}
end
let(:package_path) { File.join(Dir.pwd, 'pkg', 'testuser-testmodule-2.3.4.tar.gz') }

it 'informs the user of the module that is being built' do
expect(logger).to receive(:info).with(a_string_matching(%r{#{mock_metadata['name']} version #{mock_metadata['version']}}i))
end

it 'informs the user of the path to the package on successful build' do
expect(logger).to receive(:info).with(a_string_matching(%r{package can be found.+#{Regexp.escape(package_path)}}i))
end

context 'and provided no flags' do
it 'invokes the builder with the default target directory' do
expect(PDK::Module::Build).to receive(:invoke).with(:'target-dir' => File.join(Dir.pwd, 'pkg'))
end
end

context 'and provided with the --target-dir option' do
let(:command_opts) { ['--target-dir', '/tmp/pdk_builds'] }

it 'invokes the builder with the specified target directory' do
expect(PDK::Module::Build).to receive(:invoke).with(:'target-dir' => '/tmp/pdk_builds')
end
end
end
end

0 comments on commit 2b55f1a

Please sign in to comment.