-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from rodjek/pdk-748
(PDK-748) Wireframe `pdk build` CLI
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |