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-596) Accept "forgeuser-modulename" as argument to new module #358

Merged
merged 2 commits into from
Nov 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
10 changes: 8 additions & 2 deletions lib/pdk/cli/module/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ module PDK::CLI
answer = redirect.run

if answer
opts[:module_name] = module_name
opts[:target_dir] = module_name
module_name_parts = module_name.split('-', 2)
if module_name_parts.size > 1
opts[:username] = module_name_parts[0]
opts[:module_name] = module_name_parts[1]
else
opts[:module_name] = module_name
end
opts[:target_dir] = opts[:module_name]

PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
PDK::Generate::Module.invoke(opts)
Expand Down
10 changes: 8 additions & 2 deletions lib/pdk/cli/new/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ module PDK::CLI
target_dir = args[1]

unless module_name.nil? || module_name.empty?
opts[:module_name] = module_name
opts[:target_dir] = target_dir.nil? ? module_name : target_dir
module_name_parts = module_name.split('-', 2)
if module_name_parts.size > 1
opts[:username] = module_name_parts[0]
opts[:module_name] = module_name_parts[1]
else
opts[:module_name] = module_name
end
opts[:target_dir] = target_dir.nil? ? opts[:module_name] : target_dir
end

PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
Expand Down
13 changes: 7 additions & 6 deletions lib/pdk/generate/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def self.invoke(opts = {})

def self.username_from_login
login = Etc.getlogin || ''
login_clean = login.gsub(%r{[^0-9a-z]}i, '')
login_clean = login.downcase.gsub(%r{[^0-9a-z]}i, '')
login_clean = 'username' if login_clean.empty?

if login_clean != login
Expand All @@ -123,7 +123,7 @@ def self.username_from_login
end

def self.prepare_metadata(opts = {})
username = PDK.answers['forge-username'] || username_from_login
opts[:username] = (opts[:username] || PDK.answers['forge-username'] || username_from_login).downcase

defaults = {
'version' => '0.1.0',
Expand All @@ -132,7 +132,7 @@ def self.prepare_metadata(opts = {})
{ 'name' => 'puppet', 'version_requirement' => '>= 4.7.0 < 6.0.0' },
],
}
defaults['name'] = "#{username}-#{opts[:module_name]}" unless opts[:module_name].nil?
defaults['name'] = "#{opts[:username]}-#{opts[:module_name]}" unless opts[:module_name].nil?
defaults['author'] = PDK.answers['author'] unless PDK.answers['author'].nil?
defaults['license'] = PDK.answers['license'] unless PDK.answers['license'].nil?
defaults['license'] = opts[:license] if opts.key? :license
Expand Down Expand Up @@ -297,6 +297,7 @@ def self.module_interview(metadata, opts = {})
interview = PDK::CLI::Util::Interview.new(prompt)

questions.reject! { |q| q[:name] == 'module_name' } if opts.key?(:module_name)
questions.reject! { |q| q[:name] == 'forge_username' } if opts.key?(:username)
questions.reject! { |q| q[:name] == 'license' } if opts.key?(:license)

interview.add_questions(questions)
Expand All @@ -316,10 +317,10 @@ def self.module_interview(metadata, opts = {})
exit 0
end

forge_username = answers['forge_username']
opts[:username] ||= answers['forge_username']
opts[:module_name] ||= answers['module_name']

answers['name'] = "#{answers['forge_username']}-" + (opts[:module_name])
answers['name'] = "#{opts[:username]}-" + (opts[:module_name])
answers['license'] = opts[:license] if opts.key?(:license)
answers['operatingsystem_support'].flatten!

Expand Down Expand Up @@ -347,7 +348,7 @@ def self.module_interview(metadata, opts = {})
end

PDK.answers.update!(
'forge-username' => forge_username,
'forge-username' => opts[:username],
'author' => answers['author'],
'license' => answers['license'],
)
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/pdk/cli/module/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
expect(PDK::Generate::Module).to receive(:invoke)
PDK::CLI.run(['module', 'generate', module_name])
end

context 'when passed a module name with a forge user' do
let(:module_name) { 'user-test123' }

it 'validates and parses the module name' do
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(module_name: 'test123', username: 'user'))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['module', 'generate', module_name])
end
end
end

# context 'with a no at the prompt' do
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/pdk/cli/new/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@
end
end
end

context 'when passed a module name with a forge user' do
let(:module_name) { 'user-test123' }

it 'validates and parses the module name' do
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(module_name: 'test123', username: 'user'))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['new', 'module', module_name])
end
end
end
11 changes: 10 additions & 1 deletion spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@
end

context 'when the login is entirely alphanumeric' do
let(:login) { 'testUser123' }
let(:login) { 'testuser123' }

it 'returns the unaltered login' do
is_expected.to eq(login)
Expand All @@ -750,6 +750,15 @@
end
end

context 'when the login contains some upper case characters' do
let(:login) { 'Administrator' }

it 'warns the user and returns the login with the characters downcased' do
expect(logger).to receive(:warn).with(a_string_matching(%r{not a valid forge username}i))
is_expected.to eq('administrator')
end
end

context 'when the login contains only non-alphanumeric characters' do
let(:login) { 'Αρίσταρχος ό Σάμιος' }

Expand Down