Skip to content

Commit

Permalink
Update default behavior to glob for similar files
Browse files Browse the repository at this point in the history
  • Loading branch information
ubermajestix committed Apr 27, 2013
1 parent e672345 commit c4295d9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use ruby-2.0.0-p0@tdd --create
3 changes: 3 additions & 0 deletions README.md
@@ -1,6 +1,9 @@
tdd
===

[![Gem Version](https://badge.fury.io/rb/tdd.png)](http://badge.fury.io/rb/tdd)
[![Code Climate](https://codeclimate.com/github/ubermajestix/tdd.png)](https://codeclimate.com/github/ubermajestix/tdd)

Watch files and run test/unit or rspec tests when those files change.

Is it awesome?
Expand Down
51 changes: 34 additions & 17 deletions bin/tdd
Expand Up @@ -39,30 +39,43 @@ You can pass arguments you would normally pass to `ruby -Itest` or `rspec`
$ tdd -- test/unit/some_unit_test.rb -n /some_test_name/
$ tdd -- spec/some_spec.rb:42 --fail-fast
By default, tdd will watch files in app, lib, config, test, and spec
directories, if they exist, and run your test command if any file being
watched changes.
By default, tdd will glob for files that match your test file and run your test
command if any file being watched changes.
You can specify which files to watch (note the double dashes `--`
separating the files to watch from the test file and options):
$ tdd lib/some_unit.rb config/setup.rb -- test/unit/some_unit.rb -n/some_test_name/
$ tdd test/unit/some_class_test.rb
Running: ruby -I test test/unit/some_class_test.rb
Watching:
/Users/ubermajestix/work/awesome_app/lib/some_class.rb
/Users/ubermajestix/work/awesome_app/test/unit/some_class_test.rb
##########################################
You can tell it to find a similarly named file to your test to watch
with glob mode:
You can specify additional files to watch (note the double dashes `--`
separating the files to watch from the test file and options):
$ tdd glob -- test/unit/some_unit_test.rb
$ tdd lib/other_class.rb config/setup.rb -- test/unit/some_class_test.rb -n/some_test_name/
Running: ruby -I test test/unit/some_class_test.rb -n/some_test_name/
Watching:
/Users/ubermajestix/work/awesome_app/config/setup.rb
/Users/ubermajestix/work/awesome_app/lib/some_class.rb
/Users/ubermajestix/work/awesome_app/lib/other_class.rb
/Users/ubermajestix/work/awesome_app/test/unit/some_class_test.rb
##########################################
This will look for `some_unit.rb` in your project and watch it for changes,
along with the test file.
In a Rails project you can ask tdd to watch view and controller files
related to a functional or controller test:
$ tdd controller -- test/functional/users_controller_test.rb
$ tdd controller -- spec/controllers/users_controller_spec.rb
Running: rspec spec/controllers/users_controller_spec.rb
Watching:
/Users/ubermajestix/work/awesome_rails_app/app/controllers/users_controller.rb
/Users/ubermajestix/work/awesome_rails_app/app/views/index.html.erb
/Users/ubermajestix/work/awesome_rails_app/app/views/show.html.erb
/Users/ubermajestix/work/awesome_rails_app/app/views/edit.html.erb
/Users/ubermajestix/work/awesome_rails_app/app/views/_fields.html.erb
/Users/ubermajestix/work/awesome_rails_app/spec/controllers/users_controller_spec.rb
##########################################
will watch all view files in app/views/users, the users_controller and the
test file for changes.
__

Expand Down Expand Up @@ -98,8 +111,10 @@ test file for changes.

def print_a_summary_of_watched_files
puts "Running: #{ @command }"
if @paths.length > 15
puts "Watching: #{@paths.length} files"
if @paths.length > 25
current_dir = Dir.pwd
dirs = @paths.map{|p| p.gsub(Dir.pwd + "/", '').split('/').first + "/"}.uniq
puts "Watching #{@paths.length} files in #{dirs.join(' ')}"
else
puts "Watching:"
puts @paths.join("\n")
Expand Down Expand Up @@ -141,10 +156,12 @@ test file for changes.
cmd = entry ? @command.gsub(/@/, entry) : @command
puts line
say("# starting test run #{ n } @ #{ Time.now.strftime('%H:%M:%S') } - #{ cmd }", :color => :magenta)
# TODO if blink1 installed - change its color to purple until the tests are over
puts
system(cmd)
puts
say("# finished test run #{ n } @ #{ Time.now.strftime('%H:%M:%S') } - #{ $?.exitstatus }", :color => :yellow)
# TODO if blink1 installed - change its color to yellow and fade out after 30seconds.
puts
n.succ!
end
Expand Down
22 changes: 14 additions & 8 deletions lib/tdd/command_line_parser.rb
@@ -1,7 +1,7 @@
class Tdd::CommandLineParser
def self.parse
parser = self.new
[parser.paths, parser.test_command]
[parser.paths.flatten, parser.test_command]
end

attr_accessor :paths, :test_framework
Expand All @@ -12,11 +12,12 @@ def initialize
@paths = ARGV[0 ... pos]
@test_args = ARGV[pos + 1 .. -1].join(' ')
else
@paths = %w[app lib config test spec]
@paths = []
@test_args = ARGV[0..-1].join(' ')
end
@test_file = @test_args.scan(/^.+.rb/).first
parse_glob_mode
glob_for_test_file_matches
parse_all_files_mode
parse_controller_mode
parse_test_framework
end
Expand All @@ -33,11 +34,16 @@ def exit_if_no_test_framework
exit 1
end

def parse_glob_mode
if @paths.first == "glob"
search = File.basename(@test_file).gsub(/(_spec|_test)/, '')
@paths = Dir.glob("**/#{search}")
@paths << @test_file
def glob_for_test_file_matches
return unless @test_file
search = File.basename(@test_file).gsub(/(_spec|_test)/, '')
@paths << Dir.glob("**/#{search}")
@paths << @test_file
end

def parse_all_files_mode
if @paths.first == 'all'
@paths = %w[app lib config test spec]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tdd/version.rb
@@ -1,3 +1,3 @@
module Tdd
VERSION = "1.0.2"
VERSION = "2.0.0"
end

0 comments on commit c4295d9

Please sign in to comment.