Skip to content

Commit 9e24e01

Browse files
author
Simon Coffey
committed
Simplify test file loading
Loading files for tests currently involves a lot of manually-constructed relative requires, as well as a Dir.chdir in the tests/all_tests.rb file which adjusts where requires need to be relative from. Apart from some boilerplate, the main effect of this is that it's hard to run a single test file from the command line, as you need to edit the all_tests.rb file. This makes things a bit simpler by: * Adding the tests directory to the load path for the `rake test` task * Removing the chdir and using Pathname to manage the loading in all_tests.rb * Replacing all relative requires with direct ones. You can now run a single test file from the root directory of the project in a fairly standard way: $ ruby -Ilib:tests tests/units/test_object.rb Signed-off-by: Simon Coffey <simon.coffey@futurelearn.com>
1 parent 354412e commit 9e24e01

40 files changed

+54
-49
lines changed

Rakefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'bundler/gem_tasks'
22
require 'English'
33

4-
require "#{File.expand_path(File.dirname(__FILE__))}/lib/git/version"
4+
require 'git/version'
55

66
default_tasks = []
77

@@ -10,7 +10,9 @@ task :test do
1010
sh 'git config --global user.email "git@example.com"' if `git config user.email`.empty?
1111
sh 'git config --global user.name "GitExample"' if `git config user.name`.empty?
1212

13-
require File.dirname(__FILE__) + '/tests/all_tests.rb'
13+
$LOAD_PATH << 'tests'
14+
15+
require 'all_tests'
1416
end
1517
default_tasks << :test
1618

tests/all_tests.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
Dir.chdir(File.dirname(__FILE__)) do
2-
Dir.glob('**/test_*.rb') do |test_case|
3-
require "#{File.expand_path(File.dirname(__FILE__))}/#{test_case}"
4-
end
1+
require 'pathname'
2+
3+
Pathname.glob('tests/**/test_*.rb') do |test_case|
4+
require test_case.expand_path.relative_path_from(__dir__)
55
end
66

7-
# To run a single test:
8-
# require_relative 'units/test_lib_meets_required_version'
7+
# To run a single test, comment the above and uncomment:
8+
# require 'units/test_lib_meets_required_version'
9+
10+
# or alternatively run single tests from the command line:
11+
# $ ruby -Ilib:tests tests/units/test_lib_meets_required_version.rb

tests/units/test_archive.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

55
class TestArchive < Test::Unit::TestCase
66

tests/units/test_bare.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

55
class TestBare < Test::Unit::TestCase
66

tests/units/test_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

55
class TestBase < Test::Unit::TestCase
66

tests/units/test_branch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

55
class TestBranch < Test::Unit::TestCase
66
def setup

tests/units/test_commit_with_empty_message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env ruby
2-
require File.dirname(__FILE__) + '/../test_helper'
2+
require 'test_helper'
33

44
class TestCommitWithEmptyMessage < Test::Unit::TestCase
55
def setup

tests/units/test_commit_with_gpg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

55
class TestCommitWithGPG < Test::Unit::TestCase
66
def setup

tests/units/test_config.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#!/usr/bin/env ruby
22

3-
require_relative '../test_helper'
3+
require 'test_helper'
44

55
class TestConfig < Test::Unit::TestCase
66
def setup
77
set_file_paths
88
@git = Git.open(@wdir)
99
end
10-
10+
1111
def test_config
1212
c = @git.config
1313
assert_equal('Scott Chacon', c['user.name'])
1414
assert_equal('false', c['core.bare'])
1515
end
16-
16+
1717
def test_read_config
1818
assert_equal('Scott Chacon', @git.config('user.name'))
1919
assert_equal('false', @git.config('core.bare'))
2020
end
21-
21+
2222
def test_set_config
2323
in_temp_dir do |path|
2424
g = Git.clone(@wbare, 'bare')

tests/units/test_config_module.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

55
class TestConfigModule < Test::Unit::TestCase
66
def setup

0 commit comments

Comments
 (0)