Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-365) Inform and prompt user following new module generate #270

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for FileUtils.mv to fail without raising an exception?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I've seen

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have clarified the point of my question which was, why do we need that conditional around the log lines?

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