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-1495) Update pdk new test UX #749

Merged
merged 1 commit into from
Sep 16, 2019
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
2 changes: 1 addition & 1 deletion lib/pdk/cli/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ module PDK::CLI
require 'pdk/cli/new/module'
require 'pdk/cli/new/provider'
require 'pdk/cli/new/task'
require 'pdk/cli/new/test'
require 'pdk/cli/new/transport'
require 'pdk/cli/new/unit_test'
25 changes: 13 additions & 12 deletions lib/pdk/cli/new/unit_test.rb → lib/pdk/cli/new/test.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
module PDK::CLI
@new_define_cmd = @new_cmd.define_command do
name 'unit_test'
usage _('unit_test [options] <name>')
summary _('(Experimental) Create a new unit test for the object named <name>')
description _(<<-EOF
Generate a new rspec-puppet unit test for an existing class or defined type.

Please note, this is an experimental feature; the functionality and UX is
subject to change in future releases.
EOF
)
name 'test'
usage _('test [options] <name>')
summary _('Create a new test for the object named <name>')
flag :u, :unit, _('Create a new unit test.')
PDK::CLI.puppet_version_options(self)
PDK::CLI.puppet_dev_option(self)

run do |opts, args, _cmd|
PDK::CLI::Util.validate_puppet_version_opts(opts)
PDK::CLI::Util.ensure_in_module!(
message: _('Unit tests can only be created from inside a valid module directory.'),
message: _('Tests can only be created from inside a valid module directory.'),
log_level: :info,
)

Expand All @@ -28,6 +22,13 @@ module PDK::CLI
exit 1
end

unless opts[:unit]
# At a future time, we'll replace this conditional with an interactive
# question to choose the test type.
PDK.logger.info _('Test type not specified, assuming unit.')
opts[:unit] = true
end

puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
PDK::Util::PuppetVersion.fetch_puppet_dev if opts[:'puppet-dev']
PDK::Util::RubyVersion.use(puppet_env[:ruby_version])
Expand All @@ -36,7 +37,7 @@ module PDK::CLI
begin
generator, obj = PDK::Util::PuppetStrings.find_object(object_name)

PDK::CLI::Util.analytics_screen_view('new_unit_test', opts)
PDK::CLI::Util.analytics_screen_view('new_test', opts)

generator.new(module_dir, obj['name'], opts.merge(spec_only: true)).run
rescue PDK::Util::PuppetStrings::NoObjectError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper_acceptance'

describe 'pdk new unit_test', module_command: true do
describe 'pdk new test', module_command: true do
context 'in a new module' do
include_context 'in a new module', 'new_unit_test'

Expand All @@ -15,7 +15,7 @@
end

context 'when creating a test for the main class' do
describe command('pdk new unit_test new_unit_test') do
describe command('pdk new test --unit new_unit_test') do
its(:exit_status) { is_expected.to eq 0 }
its(:stderr) { is_expected.to match(%r{Creating .* from template}) }
its(:stderr) { is_expected.not_to match(%r{WARN|ERR}) }
Expand All @@ -31,7 +31,7 @@
end

context 'when creating a test for a defined type' do
describe command('pdk new unit_test def_type') do
describe command('pdk new test def_type') do
its(:exit_status) { is_expected.to eq 0 }
its(:stderr) { is_expected.to match(%r{Creating .* from template}) }
its(:stderr) { is_expected.not_to match(%r{WARN|ERR}) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
require 'spec_helper'

describe 'PDK::CLI new unit_test' do
let(:help_text) { a_string_matching(%r{^USAGE\s+pdk new unit_test}m) }
describe 'PDK::CLI new test' do
let(:help_text) { a_string_matching(%r{^USAGE\s+pdk new test}m) }

context 'when not run from inside a module' do
include_context 'run outside module'

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[new unit_test my_object]) }.to exit_nonzero
expect { PDK::CLI.run(%w[new test my_object]) }.to exit_nonzero
end

it 'does not submit the command to analytics' do
expect(analytics).not_to receive(:screen_view)

expect { PDK::CLI.run(%w[new unit_test my_object]) }.to exit_nonzero
expect { PDK::CLI.run(%w[new test my_object]) }.to exit_nonzero
end
end

Expand All @@ -28,26 +28,26 @@
end

context 'and not provided with an object name' do
it 'exits non-zero and prints the `pdk new unit_test` help' do
expect { PDK::CLI.run(%w[new unit_test]) }.to exit_nonzero.and output(help_text).to_stdout
it 'exits non-zero and prints the `pdk new test` help' do
expect { PDK::CLI.run(%w[new test --unit]) }.to exit_nonzero.and output(help_text).to_stdout
end

it 'does not submit the command to analytics' do
expect(analytics).not_to receive(:screen_view)

expect { PDK::CLI.run(%w[new unit_test]) }.to exit_nonzero.and output(help_text).to_stdout
expect { PDK::CLI.run(%w[new test --unit]) }.to exit_nonzero.and output(help_text).to_stdout
end
end

context 'and provided an empty string as the object name' do
it 'exits non-zero and prints the `pdk new unit_test` help' do
expect { PDK::CLI.run(['new', 'unit_test', '']) }.to exit_nonzero.and output(help_text).to_stdout
it 'exits non-zero and prints the `pdk new test` help' do
expect { PDK::CLI.run(['new', 'test', '--unit', '']) }.to exit_nonzero.and output(help_text).to_stdout
end

it 'does not submit the command to analytics' do
expect(analytics).not_to receive(:screen_view)

expect { PDK::CLI.run(['new', 'unit_test', '']) }.to exit_nonzero.and output(help_text).to_stdout
expect { PDK::CLI.run(['new', 'test', '--unit', '']) }.to exit_nonzero.and output(help_text).to_stdout
end
end

Expand All @@ -59,13 +59,13 @@
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{unable to find anything called "test-class"}i))

expect { PDK::CLI.run(%w[new unit_test test-class]) }.to exit_nonzero
expect { PDK::CLI.run(%w[new test --unit test-class]) }.to exit_nonzero
end

it 'does not submit the command to analytics' do
expect(analytics).not_to receive(:screen_view)

expect { PDK::CLI.run(%w[new unit_test test-class]) }.to exit_nonzero
expect { PDK::CLI.run(%w[new test --unit test-class]) }.to exit_nonzero
end
end

Expand All @@ -76,23 +76,48 @@
allow(PDK::Util::PuppetStrings).to receive(:find_object).with('test_class').and_return([PDK::Generate::PuppetClass, { 'name' => 'my_module::test_class' }])
end

after(:each) do
PDK::CLI.run(%w[new unit_test test_class])
context 'and the test type is specified with --unit' do
after(:each) do
PDK::CLI.run(%w[new test --unit test_class])
end

it 'generates a unit test for the class' do
expect(PDK::Generate::PuppetClass).to receive(:new).with(anything, 'my_module::test_class', include(spec_only: true)).and_return(generator)
expect(generator).to receive(:run)
end

it 'submits the command to analytics' do
allow(PDK::Generate::PuppetClass).to receive(:new).and_return(generator)

expect(analytics).to receive(:screen_view).with(
'new_test',
cli_options: 'unit=true',
output_format: 'default',
ruby_version: RUBY_VERSION,
)
end
end

it 'generates a unit test for the class' do
expect(PDK::Generate::PuppetClass).to receive(:new).with(anything, 'my_module::test_class', include(spec_only: true)).and_return(generator)
expect(generator).to receive(:run)
end

it 'submits the command to analytics' do
allow(PDK::Generate::PuppetClass).to receive(:new).and_return(generator)

expect(analytics).to receive(:screen_view).with(
'new_unit_test',
output_format: 'default',
ruby_version: RUBY_VERSION,
)
context 'and the test type is not specified' do
after(:each) do
PDK::CLI.run(%w[new test test_class])
end

it 'generates a unit test for the class' do
expect(PDK::Generate::PuppetClass).to receive(:new).with(anything, 'my_module::test_class', include(spec_only: true)).and_return(generator)
expect(generator).to receive(:run)
end

it 'submits the command to analytics' do
allow(PDK::Generate::PuppetClass).to receive(:new).and_return(generator)

expect(analytics).to receive(:screen_view).with(
'new_test',
cli_options: 'unit=true',
output_format: 'default',
ruby_version: RUBY_VERSION,
)
end
end
end

Expand All @@ -108,13 +133,13 @@
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{pdk does not support generating unit tests for "unsupported_thing"}i))

expect { PDK::CLI.run(%w[new unit_test test_thing]) }.to exit_nonzero
expect { PDK::CLI.run(%w[new test --unit test_thing]) }.to exit_nonzero
end

it 'does not submit the command to analytics' do
expect(analytics).not_to receive(:screen_view)

expect { PDK::CLI.run(%w[new unit_test test_thing]) }.to exit_nonzero
expect { PDK::CLI.run(%w[new test --unit test_thing]) }.to exit_nonzero
end
end
end
Expand Down