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

tests/units/test_describe.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 TestDescribe < Test::Unit::TestCase
66

tests/units/test_diff.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 TestDiff < Test::Unit::TestCase
66
def setup

tests/units/test_diff_non_default_encoding.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 TestDiffWithNonDefaultEncoding < Test::Unit::TestCase
66
def git_working_dir

tests/units/test_diff_with_escaped_path.rb

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

4-
require File.dirname(__FILE__) + '/../test_helper'
4+
require 'test_helper'
55

66
# Test diff when the file path has to be quoted according to core.quotePath
77
# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath

tests/units/test_each_conflict.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 TestEachConflict < Test::Unit::TestCase
66

tests/units/test_escaped_path.rb

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

4-
require "#{File.dirname(__FILE__)}/../test_helper"
4+
require 'test_helper'
55

66
# Test diff when the file path has escapes according to core.quotePath
77
# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath

tests/units/test_git_clone.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'test/unit'
4-
require_relative '../test_helper'
4+
require 'test_helper'
55

66
# Tests for Git.clone
77
class TestGitClone < Test::Unit::TestCase

tests/units/test_git_dir.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 TestGitDir < Test::Unit::TestCase
66
def test_index_calculated_from_git_dir

tests/units/test_git_path.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 TestGitPath < Test::Unit::TestCase
66

tests/units/test_index_ops.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 TestIndexOps < Test::Unit::TestCase
66

tests/units/test_init.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
require 'stringio'
55
require 'logger'
66

tests/units/test_lib.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
require "fileutils"
55

66
# tests all the low level git communication

tests/units/test_lib_meets_required_version.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 TestLibMeetsRequiredVersion < Test::Unit::TestCase
66
def test_with_supported_command_version

tests/units/test_log.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
require 'logger'
3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

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

tests/units/test_logger.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
require 'logger'
3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require 'test_helper'
44

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

tests/units/test_ls_files_with_escaped_path.rb

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

4-
require File.dirname(__FILE__) + '/../test_helper'
4+
require 'test_helper'
55

66
# Test diff when the file path has to be quoted according to core.quotePath
77
# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath

tests/units/test_merge.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 TestMerge < Test::Unit::TestCase
66
def setup

tests/units/test_merge_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 TestMergeBase < Test::Unit::TestCase
66
def setup

tests/units/test_object.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 TestObject < Test::Unit::TestCase
66
def setup

tests/units/test_remotes.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_relative '../test_helper'
3+
require 'test_helper'
44

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

tests/units/test_repack.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 TestRepack < Test::Unit::TestCase
66
def setup

tests/units/test_show.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 TestShow < Test::Unit::TestCase
66
def test_do_not_chomp_contents

tests/units/test_signed_commits.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
require "fileutils"
55

66
class TestSignedCommits < Test::Unit::TestCase

tests/units/test_stashes.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 TestStashes < Test::Unit::TestCase
66
def setup

tests/units/test_status.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
#!/usr/bin/env ruby
33

4-
require File.dirname(__FILE__) + '/../test_helper'
4+
require 'test_helper'
55

66
class TestStatus < Test::Unit::TestCase
77

tests/units/test_tags.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 TestTags < Test::Unit::TestCase
66
def setup

tests/units/test_thread_safety.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 TestThreadSafety < Test::Unit::TestCase
66
def setup

tests/units/test_tree_ops.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 TestTreeOps < Test::Unit::TestCase
66

tests/units/test_windows_cmd_escaping.rb

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

4-
require File.dirname(__FILE__) + '/../test_helper'
4+
require 'test_helper'
55

66
# Test diff when the file path has to be quoted according to core.quotePath
77
# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath

tests/units/test_worktree.rb

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

66
SAMPLE_LAST_COMMIT = '5e53019b3238362144c2766f02a2c00d91fcc023'
77

0 commit comments

Comments
 (0)