tconsole 0.0.3.pre → 0.0.4.pre

Sign up to get free protection for your applications and to get access to all the features.
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "bundle/nerdcommenter"]
2
+ path = bundle/nerdcommenter
3
+ url = git://github.com/scrooloose/nerdcommenter.git
data/lib/tconsole.rb CHANGED
@@ -25,7 +25,7 @@ module TConsole
25
25
  while running
26
26
  # ignore ctrl-c during load, since things can get kind of messy if we don't
27
27
 
28
- pid = fork do
28
+ fork do
29
29
  begin
30
30
  server = Server.new
31
31
 
@@ -63,7 +63,7 @@ module TConsole
63
63
  running = command_loop(server) if running
64
64
 
65
65
  server.stop
66
- Process.wait2(pid)
66
+ Process.waitall
67
67
  end
68
68
 
69
69
  puts
@@ -73,24 +73,31 @@ module TConsole
73
73
 
74
74
  def self.command_loop(server)
75
75
  while line = Readline.readline("tconsole> ", true)
76
+ line.strip!
77
+ args = line.split(/\s/)
78
+
76
79
  if line == ""
77
80
  # do nothing
78
- elsif line == "exit"
81
+ elsif args[0] == "exit"
79
82
  return false
80
- elsif line == "reload"
83
+ elsif args[0] == "reload"
81
84
  return true
82
- elsif line == "help"
85
+ elsif args[0] == "help"
83
86
  help
84
- elsif line == "units"
85
- server.run_tests(["test/unit/**/*_test.rb"])
86
- elsif line == "functionals"
87
- server.run_tests(["test/functional/**/*_test.rb"])
88
- elsif line == "integration"
89
- server.run_tests(["test/integration/**/*_test.rb"])
90
- elsif line == "all"
91
- server.run_tests(["test/unit/**/*_test.rb", "test/functional/**/*_test.rb", "test/integration/**/*_test.rb"])
87
+ elsif args[0] == "units"
88
+ server.run_tests(["test/unit/**/*_test.rb"], args[1])
89
+ elsif args[0] == "functionals"
90
+ server.run_tests(["test/functional/**/*_test.rb"], args[1])
91
+ elsif args[0] == "integration"
92
+ server.run_tests(["test/integration/**/*_test.rb"], args[1])
93
+ elsif args[0] == "recent"
94
+ server.run_recent(args[1])
95
+ elsif args[0] == "uncommitted"
96
+ server.run_uncommitted(args[1])
97
+ elsif args[0] == "all"
98
+ server.run_tests(["test/unit/**/*_test.rb", "test/functional/**/*_test.rb", "test/integration/**/*_test.rb"], args[1])
92
99
  else
93
- server.run_tests([line])
100
+ server.run_tests([args[0]], args[1])
94
101
  end
95
102
  end
96
103
 
@@ -102,13 +109,23 @@ module TConsole
102
109
  puts
103
110
  puts "Available commands:"
104
111
  puts
105
- puts "all # Run all test types (units, functionals, integration)"
106
- puts "units # Run unit tests"
107
- puts "functionals # Run functional tests"
108
- puts "integration # Run integration tests"
109
- puts "[filename] # Run the tests contained in the given file"
110
- puts "reload # Reload your Rails environment"
111
- puts "exit # Exit the console"
112
+ puts "all [test_pattern] # Run all test types (units, functionals, integration)"
113
+ puts "units [test_pattern] # Run unit tests"
114
+ puts "functionals [test_pattern] # Run functional tests"
115
+ puts "integration [test_pattern] # Run integration tests"
116
+ puts "recent [test_pattern] # Run tests for recently changed files"
117
+ puts "uncommitted [test_pattern] # Run tests for uncommitted changes"
118
+ puts "[filename] [test_pattern] # Run the tests contained in the given file"
119
+ puts "reload # Reload your Rails environment"
120
+ puts "exit # Exit the console"
121
+ puts
122
+ puts
123
+ puts "Working with test patterns:"
124
+ puts
125
+ puts "All of the test execution commands include an optional test_pattern argument. A"
126
+ puts "test pattern can be given to filter the executed tests to only those tests whose"
127
+ puts "name matches the pattern given. This is especially useful when rerunning a failing"
128
+ puts "test."
112
129
  puts
113
130
  end
114
131
  end
@@ -0,0 +1,29 @@
1
+ module TConsole
2
+ class MiniTestHandler
3
+ def self.run(name_pattern)
4
+ args = []
5
+ unless name_pattern.nil?
6
+ args = ["--name", name_pattern]
7
+ end
8
+
9
+ MiniTest::Unit.runner.run(args)
10
+
11
+ patch_minitest
12
+ end
13
+
14
+ # We're basically breaking MiniTest autorun here, since we want to manually run our
15
+ # tests and Rails relies on autorun
16
+ #
17
+ # A big reason for the need for this is that we're trying to work in the Rake environment
18
+ # rather than rebuilding all of the code in Rake just to get test prep happening
19
+ # correctly.
20
+ def self.patch_minitest
21
+ MiniTest::Unit.class_eval do
22
+ alias_method :old_run, :run
23
+ def run(args = [])
24
+ # do nothing
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -30,11 +30,11 @@ module TConsole
30
30
  return true
31
31
  end
32
32
 
33
- def run_tests(globs)
33
+ def run_tests(globs, name_pattern, message = "Running tests...")
34
34
  time = Benchmark.realtime do
35
- pid = fork do
35
+ fork do
36
36
 
37
- puts "Running tests..."
37
+ puts message
38
38
  puts
39
39
 
40
40
  paths = []
@@ -49,14 +49,82 @@ module TConsole
49
49
  if defined? ActiveRecord
50
50
  ActiveRecord::Base.connection.reconnect!
51
51
  end
52
+
53
+ if defined?(MiniTest)
54
+ require File.join(File.dirname(__FILE__), "minitest_handler")
55
+
56
+ MiniTestHandler.run(name_pattern)
57
+ elsif defined?(Test::Unit)
58
+ puts "Sorry, but tconsole doesn't support Test::Unit yet"
59
+ return
60
+ elsif defined?(RSpec)
61
+ puts "Sorry, but tconsole doesn't support RSpec yet"
62
+ return
63
+ end
52
64
  end
53
65
 
54
- Process.wait2(pid)
66
+ Process.waitall
55
67
  end
56
68
 
57
69
  puts
58
70
  puts "Test time (including load): #{time}s"
59
71
  puts
60
72
  end
73
+
74
+ # This code is from the rails test:recents command
75
+ def run_recent(test_pattern)
76
+ touched_since = Time.now - 600 # 10 minutes ago
77
+ files = recent_files(touched_since, "app/models/**/*.rb", "test/unit")
78
+ files.concat(recent_files(touched_since, "app/controllers/**/*.rb", "test/functional"))
79
+
80
+ message = "Running #{files.length} #{files.length == 1 ? "test file" : "test files"} based on changed files..."
81
+ run_tests(files, test_pattern, message)
82
+ end
83
+
84
+ def recent_files(touched_since, source_pattern, test_path)
85
+ Dir.glob(source_pattern).map do |path|
86
+ if File.mtime(path) > touched_since
87
+ tests = []
88
+ source_dir = File.dirname(path).split("/")
89
+ source_file = File.basename(path, '.rb')
90
+
91
+ # Support subdirs in app/models and app/controllers
92
+ modified_test_path = source_dir.length > 2 ? "#{test_path}/" << source_dir[1..source_dir.length].join('/') : test_path
93
+
94
+ # For modified files in app/ run the tests for it. ex. /test/functional/account_controller.rb
95
+ test = "#{modified_test_path}/#{source_file}_test.rb"
96
+ tests.push test if File.exist?(test)
97
+
98
+ # For modified files in app, run tests in subdirs too. ex. /test/functional/account/*_test.rb
99
+ test = "#{modified_test_path}/#{File.basename(path, '.rb').sub("_controller","")}"
100
+ File.glob("#{test}/*_test.rb").each { |f| tests.push f } if File.exist?(test)
101
+
102
+ return tests
103
+
104
+ end
105
+ end.flatten.compact
106
+ end
107
+
108
+ # Based on the code from rake test:uncommitted in Rails
109
+ def run_uncommitted(test_pattern)
110
+ if File.directory?(".svn")
111
+ changed_since_checkin = silence_stderr { `svn status` }.split.map { |path| path.chomp[7 .. -1] }
112
+ elsif File.directory?(".git")
113
+ changed_since_checkin = silence_stderr { `git ls-files --modified --others` }.split.map { |path| path.chomp }
114
+ else
115
+ puts "Not a Subversion or Git checkout."
116
+ return
117
+ end
118
+
119
+ models = changed_since_checkin.select { |path| path =~ /app[\\\/]models[\\\/].*\.rb$/ }
120
+ controllers = changed_since_checkin.select { |path| path =~ /app[\\\/]controllers[\\\/].*\.rb$/ }
121
+
122
+ unit_tests = models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" }
123
+ functional_tests = controllers.map { |controller| "test/functional/#{File.basename(controller, '.rb')}_test.rb" }
124
+ files = (unit_tests + functional_tests).uniq.select { |file| File.exist?(file) }
125
+
126
+ message = "Running #{files.length} #{files.length == 1 ? "test file" : "test files"} based on uncommitted changes..."
127
+ run_tests(files, test_pattern, message)
128
+ end
61
129
  end
62
130
  end
@@ -1,3 +1,3 @@
1
1
  module TConsole
2
- VERSION = "0.0.3.pre"
2
+ VERSION = "0.0.4.pre"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tconsole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.pre
4
+ version: 0.0.4.pre
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000 Z
12
+ date: 2011-12-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: tconsole gives you a helpful console for running Rails tests
15
15
  email:
@@ -20,11 +20,13 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - .gitignore
23
+ - .gitmodules
23
24
  - Gemfile
24
25
  - README.md
25
26
  - Rakefile
26
27
  - bin/tconsole
27
28
  - lib/tconsole.rb
29
+ - lib/tconsole/minitest_handler.rb
28
30
  - lib/tconsole/server.rb
29
31
  - lib/tconsole/version.rb
30
32
  - tconsole.gemspec