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

(SDK-317) Ensure parent of 'pdk new module' is writable before generation #175

Merged
merged 1 commit into from
Jul 25, 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
17 changes: 16 additions & 1 deletion lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def self.invoke(opts = {})
raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % { dir: target_dir }
end

parent_dir = File.dirname(target_dir)
unless File.writable?(parent_dir)
raise PDK::CLI::FatalError, _("You do not have permission to write to '%{parent_dir}'") % {
parent_dir: parent_dir,
}
end

metadata = prepare_metadata(opts)

temp_target_dir = PDK::Util.make_tmpdir_name('pdk-module-target')
Expand All @@ -50,7 +57,15 @@ def self.invoke(opts = {})

PDK.answers.update!('template-url' => template_url)

FileUtils.mv(temp_target_dir, target_dir)
begin
FileUtils.mv(temp_target_dir, target_dir)
rescue Errno::EACCES => e
raise PDK::CLI::FatalError, _("Failed to move '%{source}' to '%{target}': %{message}") % {
source: temp_target_dir,
target: target_dir,
message: e.message,
}
end
end

def self.username_from_login
Expand Down
24 changes: 24 additions & 0 deletions spec/generators/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,25 @@
include_context 'mock metadata.json'

let(:temp_target_dir) { '/path/to/temp/dir' }
let(:target_parent_writeable) { true }

before(:each) do
allow(File).to receive(:exist?).with(target_dir).and_return(false)
allow(PDK::Util).to receive(:make_tmpdir_name).with(anything).and_return(temp_target_dir)
allow(FileUtils).to receive(:mv).with(temp_target_dir, target_dir)
allow(PDK::Util::Version).to receive(:version_string).and_return('0.0.0')
allow(described_class).to receive(:prepare_module_directory).with(temp_target_dir)
allow(File).to receive(:writable?).with(File.dirname(target_dir)).and_return(target_parent_writeable)
end

context 'when the parent directory of the target is not writable' do
let(:target_parent_writeable) { false }

it 'raises a FatalError' do
expect {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you also need to expect that it doesn't start the interview here first?

Copy link
Contributor

Choose a reason for hiding this comment

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

The ways I tried doing this as a unit test were all pretty clunky a likely to just silently continue passing if the underlying code was refactored. I'm inclined to just merge as-is.

described_class.invoke(invoke_opts)
}.to raise_error(PDK::CLI::FatalError, %r{you do not have permission to write to}i)
end
end

it 'generates the new module in a temporary directory' do
Expand Down Expand Up @@ -128,6 +140,18 @@
described_class.invoke(invoke_opts)
end

context 'when the move to the target directory fails due to invalid permissions' do
before(:each) do
allow(FileUtils).to receive(:mv).with(temp_target_dir, target_dir).and_raise(Errno::EACCES, 'permission denied')
end

it 'raises a FatalError' do
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
it 'uses that template to generate the module' do
expect(PDK::Module::TemplateDir).to receive(:new).with('cli-template').and_yield(test_template_dir)
Expand Down