Permalink
Browse files

Added real command line parsing and --once command.

TConsole now runs whatever extras are passed on the command line as the
first command when loaded. If --once is passed as an argument TConsole
will exit after the passed command is run.
  • Loading branch information...
1 parent 4afe352 commit 10e34728a1fb2f9a69a29823a902bf0a97d159d0 @commondream commondream committed May 19, 2012
Showing with 109 additions and 30 deletions.
  1. +1 −1 lib/tconsole.rb
  2. +34 −3 lib/tconsole/config.rb
  3. +54 −26 lib/tconsole/console.rb
  4. +20 −0 test/unit/config_test.rb
View
@@ -6,6 +6,7 @@
require "tconsole/test_result"
require "tconsole/util"
+require "optparse"
require "readline"
require "benchmark"
require "drb/drb"
@@ -32,7 +33,6 @@ def self.run(argv)
Config.load_config(File.join(Dir.home, ".tconsole"))
Config.load_config(File.join(Dir.pwd, ".tconsole"))
config = Config.configure(argv)
- config.trace_execution = true if argv.include?("--trace")
config_errors = config.validation_errors
if config_errors.length > 0
View
@@ -31,7 +31,13 @@ class Config
# Element names we know
attr_accessor :cached_elements
- def initialize(args)
+ # First command to run when tconsole loads
+ attr_accessor :run_command
+
+ # Only runs the command passed on the command line, and then exits
+ attr_accessor :once
+
+ def initialize(argv)
self.trace_execution = false
self.test_dir = "./test"
self.include_paths = ["./test", "./lib"]
@@ -41,6 +47,9 @@ def initialize(args)
"all" => ["#{test_dir}/**/*_test.rb"]
}
+ # load any args into this config that were passed
+ load_args(argv)
+
@after_load = nil
@before_load = nil
@before_test_run = nil
@@ -49,6 +58,28 @@ def initialize(args)
@cached_elements = {}
end
+ def option_parser
+ @option_parser ||= OptionParser.new do |opts|
+ opts.on("-t", "--trace", "Enable verbose output.") do
+ self.trace_execution = true
+ end
+
+ opts.on("-o", "--once", "Run whatever command is passed and then exit.") do
+ self.once = true
+ end
+ end
+ end
+
+ # Public: Loads any passed command line arguments into the config.
+ #
+ # argv - The array of command line arguments we're loading
+ def load_args(argv)
+ args = argv.clone
+
+ option_parser.parse!(args)
+ self.run_command = args.join(" ")
+ end
+
def trace?
self.trace_execution
end
@@ -125,8 +156,8 @@ def self.clear_loaded_configs
end
# Returns an appropriate tconsole config based on the environment
- def self.configure(args = [])
- config = Config.new(args)
+ def self.configure(argv = [])
+ config = Config.new(argv)
if is_rails?
config.preload_paths = ["./config/application"]
View
@@ -42,45 +42,73 @@ def read_and_execute
print prompt
end
- while line = Readline.readline(prompt, false)
- line.strip!
- args = Shellwords.shellwords(line)
+ # Run any commands that have been passed
+ result = process_command(@config.run_command)
+ if result == :exit || @config.once
+ send_message(:stop)
+ return false
+ elsif result == :reload
+ send_message(:stop)
+ return true
+ end
- # save the line unless we're exiting or repeating the last command
- unless args[0] == "exit" || (Readline::HISTORY.length > 0 && Readline::HISTORY[Readline::HISTORY.length - 1] == line)
- Readline::HISTORY << line
- end
+ # The command entry loop
+ while command = Readline.readline(prompt, false)
+ command.strip!
+ result = process_command(command)
- if line == ""
- # do nothing
- elsif args[0] == "exit"
+ if result == :exit
send_message(:stop)
- self.pipe_server = nil
return false
- elsif args[0] == "reload"
+ elsif result == :reload
send_message(:stop)
return true
- elsif args[0] == "help"
- print_help
- elsif args[0] == "!failed"
- send_message(:run_failed)
- elsif args[0] == "!timings"
- send_message(:show_performance, args[1])
- elsif args[0] == "info"
- send_message(:run_info)
- elsif args[0] == "set"
- send_message(:set, args[1], args[2])
- elsif @config.file_sets.has_key?(args[0])
- send_message(:run_file_set, args[0])
- else
- send_message(:run_all_tests, args)
end
end
send_message(:stop)
false
end
+ # Public: Process a command however it needs to be handled.
+ #
+ # command - The command we need to parse and handle
+ def process_command(command)
+ args = Shellwords.shellwords(command)
+
+ # save the command unless we're exiting or repeating the last command
+ unless args[0] == "exit" || (Readline::HISTORY.length > 0 && Readline::HISTORY[Readline::HISTORY.length - 1] == command)
+ Readline::HISTORY << command
+ end
+
+ if command == ""
+ # do nothing
+ elsif args[0] == "exit"
+ send_message(:stop)
+ self.pipe_server = nil
+ return :exit
+ elsif args[0] == "reload"
+ send_message(:stop)
+ return :reload
+ elsif args[0] == "help"
+ print_help
+ elsif args[0] == "!failed"
+ send_message(:run_failed)
+ elsif args[0] == "!timings"
+ send_message(:show_performance, args[1])
+ elsif args[0] == "info"
+ send_message(:run_info)
+ elsif args[0] == "set"
+ send_message(:set, args[1], args[2])
+ elsif @config.file_sets.has_key?(args[0])
+ send_message(:run_file_set, args[0])
+ else
+ send_message(:run_all_tests, args)
+ end
+
+ nil
+ end
+
def send_message(message, *args)
pipe_server.write({:action => message.to_sym, :args => args})
pipe_server.read
View
@@ -34,6 +34,26 @@ class ConfigTest < MiniTest::Unit::TestCase
end
end
+ a "Config with command line arguments" do
+ it "should set up tracing correctly" do
+ @config = Config.new(Shellwords.shellwords("--trace"))
+
+ assert @config.trace_execution
+ end
+
+ it "should set up only running the passed command and exiting" do
+ @config = Config.new(Shellwords.shellwords("--once all"))
+
+ assert @config.once
+ end
+
+ it "should set all remaining unparsed text to be the first command to run" do
+ @config = Config.new(Shellwords.shellwords("--trace set fast on"))
+
+ assert_equal "set fast on", @config.run_command
+ end
+ end
+
the "Config class" do
before do
TConsole::Config.clear_loaded_configs

0 comments on commit 10e3472

Please sign in to comment.