Skip to content

Commit

Permalink
Add minitest integration and unit tests generation for subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed May 22, 2018
1 parent 36b57ce commit af7431d
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/tty/templates/add/spec/unit/sub_command_spec.rb.tt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require '<%= subcmd_path %>'

RSpec.describe <%= object = cmd_object_parts.join('::') %> do
it "executes `<%= subcmd_name_underscored %>` command successfully" do
it "executes `<%= "#{cmd_name_underscored} #{subcmd_name_underscored}" %>` command successfully" do
output = StringIO.new
<%- cmd_options.each do |option| -%>
<%= option %> = <%= option == 'options' ? {} : 'nil' %>
Expand Down
22 changes: 18 additions & 4 deletions lib/tty/templates/add/test/integration/command_test.rb.tt
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
require 'test_helper'
require '<%= cmd_path %>'

class <%= cmd_object_parts.join('::') %>Test < Minitest::Test
def test_executes_<%= cmd_name_underscored %>_command_successfully
output = `<%= app_name_underscored %> <%= cmd_name_underscored %>`
assert_equal nil, output
class <%= (cmd_object_parts - [subcmd_name_constantinized]).join('::') %>Test < Minitest::Test
def test_executes_<%= "#{app_name_underscored}_help_#{cmd_name_underscored}" %>_command_successfully
output = `<%= app_name_underscored %> help <%= cmd_name_underscored %>`
expected_output = <<-OUT
<%- if subcmd_name_underscored.to_s.empty? -%>
Usage:
<%= app_name_underscored + ' ' + cmd_name_underscored + cmd_desc_args %>

Options:
-h, [--help], [--no-help] # Display usage information

<%= cmd_desc %>
<%- else -%>
Commands:
<%- end -%>
OUT

assert_equal expected_output, output
end
end
19 changes: 19 additions & 0 deletions lib/tty/templates/add/test/integration/sub_command_test.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'
require '<%= subcmd_path %>'

class <%= cmd_object_parts.join('::') %>Test < Minitest::Test
def test_executes_<%= "#{app_name_underscored}_#{cmd_name_underscored}_help_#{subcmd_name_underscored}" %>_command_successfully
output = `<%= "#{app_name_underscored} #{cmd_name_underscored} help #{subcmd_name_underscored}" %>`
expect_output = <<-OUT
Usage:
<%= app_name_underscored + ' ' + subcmd_name_underscored + cmd_desc_args %>

Options:
-h, [--help], [--no-help] # Display usage information

<%= cmd_desc %>
OUT

assert_equal expected_output, output
end
end
16 changes: 16 additions & 0 deletions lib/tty/templates/add/test/unit/sub_command_test.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'
require '<%= subcmd_path %>'

class <%= object = cmd_object_parts.join('::') %>Test < Minitest::Test
def test_executes_<%= "#{cmd_name_underscored}_#{subcmd_name_underscored}" %>_command_successfully
output = StringIO.new
<%- cmd_options.each do |option| -%>
<%= option %> = <%= option == 'options' ? {} : 'nil' %>
<%- end -%>
command = <%= object %>.new(<%= cmd_options.join(', ') %>)

command.execute(output: output)

assert_equal "OK\n", output.string
end
end
16 changes: 13 additions & 3 deletions spec/integration/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,19 @@ def server(*)
require 'newcli/commands/server'
class Newcli::Commands::ServerTest < Minitest::Test
def test_executes_server_command_successfully
output = `newcli server`
assert_equal nil, output
def test_executes_newcli_help_server_command_successfully
output = `newcli help server`
expected_output = <<-OUT
Usage:
newcli server
Options:
-h, [--help], [--no-help] # Display usage information
Command description...
OUT
assert_equal expected_output, output
end
end
EOS
Expand Down
93 changes: 91 additions & 2 deletions spec/integration/add_subcommand_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def execute(input: $stdin, output: $stdout)
end
EOS

# test setup
# spec/integration/config_spec.rb
#
expect(::File.read('spec/integration/config_spec.rb')).to eq <<-EOS
RSpec.describe "`newcli config` command", type: :cli do
Expand All @@ -115,6 +115,8 @@ def execute(input: $stdin, output: $stdout)
end
EOS

# spec/integration/config/set_spec.rb
#
expect(::File.read('spec/integration/config/set_spec.rb')).to eq <<-EOS
RSpec.describe "`newcli config set` command", type: :cli do
it "executes `newcli config help set` command successfully" do
Expand All @@ -132,11 +134,13 @@ def execute(input: $stdin, output: $stdout)
end
EOS

# spec/unit/config/set_spec.rb
#
expect(::File.read('spec/unit/config/set_spec.rb')).to eq <<-EOS
require 'newcli/commands/config/set'
RSpec.describe Newcli::Commands::Config::Set do
it "executes `set` command successfully" do
it "executes `config set` command successfully" do
output = StringIO.new
options = {}
command = Newcli::Commands::Config::Set.new(options)
Expand All @@ -163,4 +167,89 @@ def execute(input: $stdin, output: $stdout)
expect(status.exitstatus).to eq(0)
end
end

it "adds a new subcommand with minitest" do
app_name = tmp_path('newcli')
silent_run("teletype new #{app_name} --test minitest")

output = <<-OUT
create test/integration/config_test.rb
create test/integration/config/set_test.rb
create test/unit/config/set_test.rb
create lib/newcli/commands/config.rb
create lib/newcli/commands/config/set.rb
create lib/newcli/templates/config/set/.gitkeep
inject lib/newcli/cli.rb
inject lib/newcli/commands/config.rb
OUT

within_dir(app_name) do
command_set = "teletype add config set --no-color"

out, err, status = Open3.capture3(command_set)

expect(err).to eq('')
expect(out).to eq(output)
expect(status.exitstatus).to eq(0)

# test setup
#
expect(::File.read('test/integration/config_test.rb')).to eq <<-EOS
require 'test_helper'
require 'newcli/commands/config'
class Newcli::Commands::ConfigTest < Minitest::Test
def test_executes_newcli_help_config_command_successfully
output = `newcli help config`
expected_output = <<-OUT
Commands:
OUT
assert_equal expected_output, output
end
end
EOS

expect(::File.read('test/integration/config/set_test.rb')).to eq <<-EOS
require 'test_helper'
require 'newcli/commands/config/set'
class Newcli::Commands::Config::SetTest < Minitest::Test
def test_executes_newcli_config_help_set_command_successfully
output = `newcli config help set`
expect_output = <<-OUT
Usage:
newcli set
Options:
-h, [--help], [--no-help] # Display usage information
Command description...
OUT
assert_equal expected_output, output
end
end
EOS

# test/unit/config/set_test.rb
#
expect(::File.read('test/unit/config/set_test.rb')).to eq <<-EOS
require 'test_helper'
require 'newcli/commands/config/set'
class Newcli::Commands::Config::SetTest < Minitest::Test
def test_executes_config_set_command_successfully
output = StringIO.new
options = {}
command = Newcli::Commands::Config::Set.new(options)
command.execute(output: output)
assert_equal "OK\\n", output.string
end
end
EOS
end
end
end

0 comments on commit af7431d

Please sign in to comment.