Skip to content

Commit

Permalink
Change to fix adding commands to namespaced application in ref to issue
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed May 22, 2018
1 parent 5312026 commit fb726cb
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/tty/commands/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def initialize(cmd_names, options)
@templater = Templater.new('add', @app_path)
end

def namespaced_path
app_name.tr('-', '/')
end

def template_context
opts = OpenStruct.new
opts[:cmd_options] = cmd_options
Expand All @@ -37,9 +41,9 @@ def template_context
opts[:cmd_desc] = cmd_desc
opts[:app_indent] = app_indent
opts[:cmd_indent] = cmd_indent
opts[:cmd_path] = "#{app_name}/commands/#{cmd_name_path}"
opts[:cmd_path] = "#{namespaced_path}/commands/#{cmd_name_path}"
opts[:subcmd_path] = subcmd_name &&
"#{app_name}/commands/#{cmd_name_path}/#{subcmd_name_path}"
"#{namespaced_path}/commands/#{cmd_name_path}/#{subcmd_name_path}"
opts[:cmd_name_constantinized] = cmd_name_constantinized
opts[:subcmd_name_constantinized] = subcmd_name && subcmd_name_constantinized
opts[:app_name_underscored] = app_name_underscored
Expand All @@ -62,10 +66,10 @@ def execute(input: $stdin, output: $stdout)
validate_cmd_name(cmd_name)

test_dir = (options["test"] == 'rspec') || ::Dir.exist?('spec') ? 'spec' : 'test'
cli_file = "lib/#{app_name}/cli.rb"
cli_file = "lib/#{namespaced_path}/cli.rb"
cli_content = ::File.read(cli_file)
cmd_file = "lib/#{app_name}/commands/#{cmd_name_path}.rb"
cmd_template_path = "lib/#{app_name}/templates/#{cmd_name_path}"
cmd_file = "lib/#{namespaced_path}/commands/#{cmd_name_path}.rb"
cmd_template_path = "lib/#{namespaced_path}/templates/#{cmd_name_path}"

cmd_integ_test_file = "#{test_dir}/integration/#{cmd_name_path}_#{test_dir}.rb"
cmd_unit_test_file = "#{test_dir}/unit/#{cmd_name_path}_#{test_dir}.rb"
Expand All @@ -87,8 +91,8 @@ def execute(input: $stdin, output: $stdout)
{after: match}.merge(file_options))
end
else
subcmd_file = "lib/#{app_name}/commands/#{cmd_name_path}/#{subcmd_name_path}.rb"
subcmd_template_path = "lib/#{app_name}/templates/#{cmd_name_path}/#{subcmd_name_path}"
subcmd_file = "lib/#{namespaced_path}/commands/#{cmd_name_path}/#{subcmd_name_path}.rb"
subcmd_template_path = "lib/#{namespaced_path}/templates/#{cmd_name_path}/#{subcmd_name_path}"
unless ::File.exists?(cmd_integ_test_file)
@templater.add_mapping(
"#{test_dir}/integration/command_#{test_dir}.rb.tt",
Expand Down
124 changes: 124 additions & 0 deletions spec/integration/add_namespaced_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
RSpec.describe "teletype add", type: :cli do
it "adds a comand to namespaced application" do
app_name = tmp_path('cli-app')
silent_run("teletype new #{app_name} --test rspec")

output = <<-OUT
create spec/integration/server_spec.rb
create spec/unit/server_spec.rb
create lib/cli/app/commands/server.rb
create lib/cli/app/templates/server/.gitkeep
inject lib/cli/app/cli.rb
OUT

within_dir(app_name) do
command = "teletype add server --no-color"

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

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

# lib/cli/app/commands/server.rb
#
expect(::File.read('lib/cli/app/commands/server.rb')).to eq <<-EOS
# frozen_string_literal: true
require_relative '../command'
module Cli
module App
module Commands
class Server < Cli::App::Command
def initialize(options)
@options = options
end
def execute(input: $stdin, output: $stdout)
# Command logic goes here ...
output.puts "OK"
end
end
end
end
end
EOS

# lib/cli/app/cli.rb
#
expect(::File.read('lib/cli/app/cli.rb')).to eq <<-EOS
# frozen_string_literal: true
require 'thor'
module Cli
module App
# Handle the application command line parsing
# and the dispatch to various command objects
#
# @api public
class CLI < Thor
# Error raised by this runner
Error = Class.new(StandardError)
desc 'version', 'cli-app version'
def version
require_relative 'version'
puts \"v\#{Cli::App::VERSION}\"
end
map %w(--version -v) => :version
desc 'server', 'Command description...'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
def server(*)
if options[:help]
invoke :help, ['server']
else
require_relative 'commands/server'
Cli::App::Commands::Server.new(options).execute
end
end
end
end
end
EOS

# test setup
#
expect(::File.read('spec/integration/server_spec.rb')).to eq <<-EOS
RSpec.describe "`cli-app server` command", type: :cli do
it "executes `cli-app help server` command successfully" do
output = `cli-app help server`
expect(output).to eq <<-OUT
Usage:
cli-app server
Options:
-h, [--help], [--no-help] # Display usage information
Command description...
OUT
end
end
EOS

expect(::File.read('spec/unit/server_spec.rb')).to eq <<-EOS
require 'cli/app/commands/server'
RSpec.describe Cli::App::Commands::Server do
it "executes `server` command successfully" do
output = StringIO.new
options = {}
command = Cli::App::Commands::Server.new(options)
command.execute(output: output)
expect(output.string).to eq("OK\\n")
end
end
EOS
end
end
end

0 comments on commit fb726cb

Please sign in to comment.