Skip to content

Commit

Permalink
Merge pull request #270 from bmjen/pdk-365
Browse files Browse the repository at this point in the history
(PDK-365) Inform and prompt user following new module generate
  • Loading branch information
bmjen authored Aug 28, 2017
2 parents bafb86f + 4d9e546 commit 35914f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def self.invoke(opts = {})
PDK.answers.update!('template-url' => template_url)

begin
FileUtils.mv(temp_target_dir, target_dir)
if FileUtils.mv(temp_target_dir, target_dir)
PDK.logger.info(_('Module \'%{name}\' generated at path \'%{path}\'.') % { name: opts[:name], path: target_dir })
PDK.logger.info(_('In your new module directory, add classes with the \'pdk new class\' command.'))
end
rescue Errno::EACCES => e
raise PDK::CLI::FatalError, _("Failed to move '%{source}' to '%{target}': %{message}") % {
source: temp_target_dir,
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
end

it 'raises a FatalError' do
expect(logger).not_to receive(:info).with(a_string_matching(%r{generated at path}i))
expect(logger).not_to receive(:info).with(a_string_matching(%r{In your new module directory, add classes with the 'pdk new class' command}i))

expect {
described_class.invoke(target_dir: target_dir)
}.to raise_error(PDK::CLI::FatalError, %r{destination directory '.+' already exists}i)
Expand Down Expand Up @@ -92,6 +95,9 @@
let(:target_parent_writeable) { false }

it 'raises a FatalError' do
expect(logger).not_to receive(:info).with(a_string_matching(%r{generated at path}i))
expect(logger).not_to receive(:info).with(a_string_matching(%r{In your new module directory, add classes with the 'pdk new class' command}i))

expect {
described_class.invoke(invoke_opts)
}.to raise_error(PDK::CLI::FatalError, %r{you do not have permission to write to}i)
Expand Down Expand Up @@ -147,15 +153,25 @@
end

it 'raises a FatalError' do
expect(logger).not_to receive(:info).with(a_string_matching(%r{generated at path}i))
expect(logger).not_to receive(:info).with(a_string_matching(%r{In your new module directory, add classes with the 'pdk new class' command}i))

expect {
described_class.invoke(invoke_opts)
}.to raise_error(PDK::CLI::FatalError, %r{failed to move .+: permission denied}i)
end
end

context 'when a template-url is supplied on the command line' do
before(:each) do
allow(FileUtils).to receive(:mv).with(temp_target_dir, target_dir).and_return(0)
end

it 'uses that template to generate the module' do
expect(PDK::Module::TemplateDir).to receive(:new).with('cli-template', anything).and_yield(test_template_dir)
expect(logger).to receive(:info).with(a_string_matching(%r{generated at path}i))
expect(logger).to receive(:info).with(a_string_matching(%r{In your new module directory, add classes with the 'pdk new class' command}i))

described_class.invoke(invoke_opts.merge(:'template-url' => 'cli-template'))
end

Expand All @@ -173,10 +189,16 @@
end

context 'when a template-url is not supplied on the command line' do
before(:each) do
allow(FileUtils).to receive(:mv).with(temp_target_dir, target_dir).and_return(0)
end

context 'and a template-url answer exists' do
it 'uses the template-url from the answer file to generate the module' do
PDK.answers.update!('template-url' => 'answer-template')
expect(PDK::Module::TemplateDir).to receive(:new).with('answer-template', anything).and_yield(test_template_dir)
expect(logger).to receive(:info).with(a_string_matching(%r{generated at path}i))
expect(logger).to receive(:info).with(a_string_matching(%r{In your new module directory, add classes with the 'pdk new class' command}i))

described_class.invoke(invoke_opts)
end
Expand Down

0 comments on commit 35914f5

Please sign in to comment.