Skip to content

Commit

Permalink
Merge pull request #335 from rodjek/pdk-621
Browse files Browse the repository at this point in the history
(PDK-621) Implement a skeleton `pdk convert` command
  • Loading branch information
Helen authored Nov 8, 2017
2 parents 34d3e63 + 8e1e901 commit acc5897
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 8 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/convert'
require 'pdk/cli/new'
require 'pdk/cli/test'
require 'pdk/cli/validate'
Expand Down
30 changes: 30 additions & 0 deletions lib/pdk/cli/convert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'pdk/cli/util'

module PDK::CLI
@convert_cmd = @base_cmd.define_command do
name 'convert'
usage _('convert [options]')
summary _('Convert an existing module to be compatible with the PDK.')
be_hidden

PDK::CLI.template_url_option(self)
flag nil, :noop, _('Do not convert the module, just output what would be done.')
flag nil, :force, _('Convert the module automatically, with no prompts.')

run do |opts, _args, _cmd|
require 'pdk/module/convert'
PDK::CLI::Util.ensure_in_module!

if opts[:noop] && opts[:force]
raise PDK::CLI::ExitWithError, _('You can not specify --noop and --force when converting a module')
end

unless opts[:noop] || opts[:force]
PDK.logger.info _('This is a potentially destructive action. Please ensure that you have committed it to a version control system or have a backup before continuing.')
exit 0 unless PDK::CLI::Util.prompt_for_yes(_('Do you want to continue converting this module?'))
end

PDK::Module::Convert.invoke(opts)
end
end
end
17 changes: 17 additions & 0 deletions lib/pdk/cli/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def spinner_opts_for_platform
{}
end
module_function :spinner_opts_for_platform

def prompt_for_yes(question_text, opts = {})
prompt = opts[:prompt] || TTY::Prompt.new(help_color: :cyan)
validator = proc { |value| [true, false].include?(value) || value =~ %r{\A(?:yes|y|no|n)\Z}i }
response = nil

begin
response = prompt.yes?(question_text) do |q|
q.validate(validator, _('Answer "Y" to continue or "n" to cancel.'))
end
rescue TTY::Prompt::Reader::InputInterrupt
PDK.logger.info opts[:cancel_message] if opts[:cancel_message]
end

response
end
module_function :prompt_for_yes
end
end
end
14 changes: 6 additions & 8 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'pdk/module/metadata'
require 'pdk/module/templatedir'
require 'pdk/cli/exec'
require 'pdk/cli/util'
require 'pdk/cli/util/interview'
require 'pdk/cli/util/option_validator'
require 'pdk/util'
Expand Down Expand Up @@ -332,14 +333,11 @@ def self.module_interview(metadata, opts = {})
puts '-' * 40
puts

begin
continue = prompt.yes?(_('About to generate this module; continue?')) do |q|
q.validate(proc { |value| [true, false].include?(value) || value =~ %r{\A(?:yes|y|no|n)\Z}i }, _('Answer "Y" to continue or "n" to cancel.'))
end
rescue TTY::Prompt::Reader::InputInterrupt
PDK.logger.info _('Interview cancelled; not generating the module.')
exit 0
end
continue = PDK::CLI::Util.prompt_for_yes(
_('About to generate this module, continue?'),
prompt: prompt,
cancel_message: _('Interview cancelled; not generating the module.'),
)

unless continue
PDK.logger.info _('Module not generated.')
Expand Down
9 changes: 9 additions & 0 deletions lib/pdk/module/convert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module PDK
module Module
class Convert
def self.invoke(_options)
true
end
end
end
end
117 changes: 117 additions & 0 deletions spec/unit/pdk/cli/convert_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require 'spec_helper'

describe 'PDK::CLI convert' do
let(:help_text) { a_string_matching(%r{^USAGE\s+pdk convert}m) }
let(:backup_warning) { a_string_matching(%r{backup before continuing}i) }

context 'when not run from inside a module' do
before(:each) do
allow(PDK::Util).to receive(:module_root).and_return(nil)
end

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[convert])
}.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
allow(PDK::Util).to receive(:module_root).and_return('/path/to/test/module')
end

context 'and provided no flags' do
before(:each) do
allow(logger).to receive(:info).with(backup_warning)
allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(a_string_matching(%r{do you want to continue}i)).and_return(true)
end

it 'asks the user if they want to continue' do
expect(logger).to receive(:info).with(backup_warning)
expect(PDK::CLI::Util).to receive(:prompt_for_yes).with(a_string_matching(%r{do you want to continue}i)).and_return(true)
allow(PDK::Module::Convert).to receive(:invoke).with(any_args).and_return(0)

PDK::CLI.run(%w[convert])
end

it 'exits cleanly if the user chooses not to continue' do
allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(a_string_matching(%r{do you want to continue}i)).and_return(false)
expect(PDK::Module::Convert).not_to receive(:invoke)

expect {
PDK::CLI.run(['convert'])
}.to raise_error(SystemExit) { |error|
expect(error.status).to eq(0)
}
end

it 'invokes the converter with the default template if the user chooses to continue' do
expect(PDK::Module::Convert).to receive(:invoke).with(:'template-url' => PDK::Generate::Module.default_template_url)

PDK::CLI.run(['convert'])
end
end

context 'and the --template-url option has been passed' do
before(:each) do
allow(logger).to receive(:info).with(backup_warning)
allow(PDK::CLI::Util).to receive(:prompt_for_yes).with(a_string_matching(%r{do you want to continue}i)).and_return(true)
end

it 'invokes the converter with the user supplied template' do
expect(PDK::Module::Convert).to receive(:invoke).with(:'template-url' => 'https://my/template')

PDK::CLI.run(['convert', '--template-url', 'https://my/template'])
end
end

context 'and the --noop flag has been passed' do
it 'does not prompt the user before invoking the converter' do
expect(logger).not_to receive(:info).with(backup_warning)
expect(PDK::CLI::Util).not_to receive(:prompt_for_yes)
allow(PDK::Module::Convert).to receive(:invoke).with(any_args)

PDK::CLI.run(['convert', '--noop'])
end

it 'passes the noop option through to the converter' do
expect(PDK::Module::Convert).to receive(:invoke).with(:noop => true, :'template-url' => anything)

PDK::CLI.run(['convert', '--noop'])
end
end

context 'and the --force flag has been passed' do
it 'does not prompt the user before invoking the converter' do
expect(logger).not_to receive(:info).with(backup_warning)
expect(PDK::CLI::Util).not_to receive(:prompt_for_yes)
allow(PDK::Module::Convert).to receive(:invoke).with(any_args)

PDK::CLI.run(['convert', '--force'])
end

it 'passes the force option through to the converter' do
expect(PDK::Module::Convert).to receive(:invoke).with(:force => true, :'template-url' => anything)

PDK::CLI.run(['convert', '--force'])
end
end

context 'and the --force and --noop flags have been passed' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{can not specify --noop and --force}i))

expect {
PDK::CLI.run(['convert', '--noop', '--force'])
}.to raise_error(SystemExit) { |error|
expect(error.status).not_to eq(0)
}
end
end
end
end

0 comments on commit acc5897

Please sign in to comment.