Skip to content

Commit 22ba027

Browse files
author
Simon Coffey
committed
Simplify test running and fixture repo cloning
This commit aims to make working with the test suite easier in two ways: 1. make individual tests easier to run 2. clarify use of fixture repo clones To do this it takes the following steps: * Eliminate complexity due to regular changes of working directory * Make individual test files runnable from the command line using the new bin/test executable * Extract constants to refer to fixture locations * Reduce unnecessary cloning of certain fixture repos * Begin using helper methods to clarify which tests are using which fixture repo Signed-off-by: Simon Coffey <simon.coffey@futurelearn.com>
1 parent 08d04ef commit 22ba027

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+698
-837
lines changed

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ In order to ensure high quality, all pull requests must meet these requirements:
8181
* The entire test suite must pass when `bundle exec rake default` is run from the
8282
project's local working copy.
8383

84+
While working on specific features you can run individual test files or
85+
a group of tests using `bin/test`:
86+
87+
# run a single file:
88+
$ bin/test tests/units/test_object.rb
89+
90+
# run multiple files:
91+
$ bin/test tests/units/test_object.rb tests/units/test_archive.rb
92+
93+
# run all unit tests:
94+
$ bin/test
95+
8496
### Continuous integration
8597
* All tests must pass in the project's [GitHub Continuous Integration build](https://github.com/ruby-git/ruby-git/actions?query=workflow%3ACI)
8698
before the pull request will be merged.

Rakefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
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

88
desc 'Run Unit Tests'
99
task :test do
10-
sh 'git config --global user.email "git@example.com"' if `git config user.email`.empty?
11-
sh 'git config --global user.name "GitExample"' if `git config user.name`.empty?
10+
sh 'ruby bin/test'
1211

13-
require File.dirname(__FILE__) + '/tests/all_tests.rb'
12+
# You can run individual test files (or multiple files) from the command
13+
# line with:
14+
#
15+
# $ bin/test tests/units/test_archive.rb
16+
#
17+
# $ bin/test tests/units/test_archive.rb tests/units/test_object.rb
1418
end
1519
default_tasks << :test
1620

bin/test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'bundler/setup'
5+
6+
`git config --global user.email "git@example.com"` if `git config user.email`.empty?
7+
`git config --global user.name "GitExample"` if `git config user.name`.empty?
8+
9+
project_root = File.expand_path(File.join(__dir__, '..'))
10+
11+
$LOAD_PATH.unshift(File.join(project_root, 'tests'))
12+
13+
if ARGV.empty?
14+
paths = Dir.glob(File.join(project_root, 'tests/**/test_*.rb'))
15+
else
16+
paths = ARGV.map { |p| File.join(project_root, p) }
17+
end
18+
19+
paths.each { |p| require p }

tests/all_tests.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/test_helper.rb

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,50 @@
88

99
class Test::Unit::TestCase
1010

11-
def set_file_paths
12-
cwd = FileUtils.pwd
13-
if File.directory?(File.join(cwd, 'files'))
14-
@test_dir = File.join(cwd, 'files')
15-
elsif File.directory?(File.join(cwd, '..', 'files'))
16-
@test_dir = File.join(cwd, '..', 'files')
17-
elsif File.directory?(File.join(cwd, 'tests', 'files'))
18-
@test_dir = File.join(cwd, 'tests', 'files')
19-
end
11+
TEST_ROOT = File.expand_path(__dir__)
12+
TEST_FIXTURES = File.join(TEST_ROOT, 'files')
2013

21-
@wdir_dot = File.expand_path(File.join(@test_dir, 'working'))
22-
@wbare = File.expand_path(File.join(@test_dir, 'working.git'))
23-
@index = File.expand_path(File.join(@test_dir, 'index'))
14+
BARE_REPO_PATH = File.join(TEST_FIXTURES, 'working.git')
2415

25-
@wdir = create_temp_repo(@wdir_dot)
16+
def clone_working_repo
17+
@wdir = create_temp_repo('working')
2618
end
2719

2820
teardown
2921
def git_teardown
3022
FileUtils.rm_r(@tmp_path) if instance_variable_defined?(:@tmp_path)
3123
end
3224

33-
def create_temp_repo(clone_path)
25+
def in_bare_repo_clone
26+
in_temp_dir do |path|
27+
git = Git.clone(BARE_REPO_PATH, 'bare')
28+
Dir.chdir('bare') do
29+
yield git
30+
end
31+
end
32+
end
33+
34+
def in_temp_repo(clone_name)
35+
clone_path = create_temp_repo(clone_name)
36+
Dir.chdir(clone_path) do
37+
yield
38+
end
39+
end
40+
41+
def create_temp_repo(clone_name)
42+
clone_path = File.join(TEST_FIXTURES, clone_name)
3443
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
3544
@tmp_path = File.expand_path(File.join("/tmp/", filename))
3645
FileUtils.mkdir_p(@tmp_path)
3746
FileUtils.cp_r(clone_path, @tmp_path)
38-
tmp_path = File.join(@tmp_path, 'working')
47+
tmp_path = File.join(@tmp_path, File.basename(clone_path))
3948
FileUtils.cd tmp_path do
4049
FileUtils.mv('dot_git', '.git')
4150
end
4251
tmp_path
4352
end
4453

45-
def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
54+
def in_temp_dir # :yields: the temporary dir's path
4655
tmp_path = nil
4756
while tmp_path.nil? || File.directory?(tmp_path)
4857
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
@@ -52,7 +61,7 @@ def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
5261
FileUtils.cd tmp_path do
5362
yield tmp_path
5463
end
55-
FileUtils.rm_r(tmp_path) if remove_after
64+
FileUtils.rm_r(tmp_path)
5665
end
5766

5867
def create_file(path, content)

tests/units/test_archive.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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

77
def setup
8-
set_file_paths
8+
clone_working_repo
99
@git = Git.open(@wdir)
1010
@tempfiles = []
1111
end
@@ -56,13 +56,8 @@ def test_archive
5656
assert_match(%r{test/}, lines[1])
5757
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])
5858

59-
in_temp_dir do
60-
c = Git.clone(@wbare, 'new')
61-
c.chdir do
62-
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
63-
assert(File.exist?(f))
64-
end
65-
end
59+
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
60+
assert(File.exist?(f))
6661
end
6762

6863
end

tests/units/test_bare.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
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

77
def setup
8-
set_file_paths
9-
@git = Git.bare(@wbare)
8+
@git = Git.bare(BARE_REPO_PATH)
109
end
1110

1211
def test_commit

tests/units/test_base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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

77
def setup
8-
set_file_paths
8+
clone_working_repo
99
end
1010

1111
def test_add

tests/units/test_branch.rb

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
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
7-
set_file_paths
7+
clone_working_repo
88
@git = Git.open(@wdir)
9-
9+
1010
@commit = @git.object('1cc8667014381')
1111
@tree = @git.object('1cc8667014381^{tree}')
1212
@blob = @git.object('v2.5:example.txt')
13-
13+
1414
@branches = @git.branches
1515
end
16-
16+
1717
def test_branches_all
1818
assert(@git.branches[:master].is_a?(Git::Branch))
1919
assert(@git.branches.size > 5)
2020
end
21-
21+
2222
def test_branches_local
2323
bs = @git.branches.local
2424
assert(bs.size > 4)
@@ -28,22 +28,22 @@ def test_branches_remote
2828
bs = @git.branches.remote
2929
assert_equal(1, bs.size)
3030
end
31-
31+
3232
def test_branches_single
3333
branch = @git.branches[:test_object]
3434
assert_equal('test_object', branch.name)
3535

3636
%w{working/master remotes/working/master}.each do |branch_name|
3737
branch = @git.branches[branch_name]
38-
38+
3939
assert_equal('master', branch.name)
4040
assert_equal('remotes/working/master', branch.full)
4141
assert_equal('working', branch.remote.name)
4242
assert_equal('+refs/heads/*:refs/remotes/working/*', branch.remote.fetch_opts)
4343
assert_equal('../working.git', branch.remote.url)
4444
end
4545
end
46-
46+
4747
def test_true_branch_contains?
4848
assert(@git.branch('git_grep').contains?('master'))
4949
end
@@ -55,52 +55,47 @@ def test_false_branch_contains?
5555
def test_branch_commit
5656
assert_equal(270, @git.branches[:test_branches].gcommit.size)
5757
end
58-
58+
5959
def test_branch_create_and_switch
60-
in_temp_dir do |path|
61-
g = Git.clone(@wbare, 'branch_test')
62-
Dir.chdir('branch_test') do
63-
assert(!g.branch('new_branch').current)
64-
g.branch('other_branch').create
65-
assert(!g.branch('other_branch').current)
66-
g.branch('new_branch').checkout
67-
assert(g.branch('new_branch').current)
68-
69-
assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
70-
71-
new_file('test-file1', 'blahblahblah1')
72-
new_file('test-file2', 'blahblahblah2')
73-
new_file('.test-dot-file1', 'blahblahblahdot1')
74-
assert(g.status.untracked.assoc('test-file1'))
75-
assert(g.status.untracked.assoc('.test-dot-file1'))
76-
77-
g.add(['test-file1', 'test-file2'])
78-
assert(!g.status.untracked.assoc('test-file1'))
79-
80-
g.reset
81-
assert(g.status.untracked.assoc('test-file1'))
82-
assert(!g.status.added.assoc('test-file1'))
83-
84-
assert_raise Git::GitExecuteError do
85-
g.branch('new_branch').delete
86-
end
87-
assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
88-
89-
g.branch('master').checkout
90-
g.branch('new_branch').delete
91-
assert_equal(0, g.branches.select { |b| b.name == 'new_branch' }.size)
92-
93-
g.checkout('other_branch')
94-
assert(g.branch('other_branch').current)
95-
96-
g.checkout('master')
97-
assert(!g.branch('other_branch').current)
98-
99-
g.checkout(g.branch('other_branch'))
100-
assert(g.branch('other_branch').current)
101-
60+
in_bare_repo_clone do |git|
61+
assert(!git.branch('new_branch').current)
62+
git.branch('other_branch').create
63+
assert(!git.branch('other_branch').current)
64+
git.branch('new_branch').checkout
65+
assert(git.branch('new_branch').current)
66+
67+
assert_equal(1, git.branches.select { |b| b.name == 'new_branch' }.size)
68+
69+
new_file('test-file1', 'blahblahblah1')
70+
new_file('test-file2', 'blahblahblah2')
71+
new_file('.test-dot-file1', 'blahblahblahdot1')
72+
assert(git.status.untracked.assoc('test-file1'))
73+
assert(git.status.untracked.assoc('.test-dot-file1'))
74+
75+
git.add(['test-file1', 'test-file2'])
76+
assert(!git.status.untracked.assoc('test-file1'))
77+
78+
git.reset
79+
assert(git.status.untracked.assoc('test-file1'))
80+
assert(!git.status.added.assoc('test-file1'))
81+
82+
assert_raise Git::GitExecuteError do
83+
git.branch('new_branch').delete
10284
end
85+
assert_equal(1, git.branches.select { |b| b.name == 'new_branch' }.size)
86+
87+
git.branch('master').checkout
88+
git.branch('new_branch').delete
89+
assert_equal(0, git.branches.select { |b| b.name == 'new_branch' }.size)
90+
91+
git.checkout('other_branch')
92+
assert(git.branch('other_branch').current)
93+
94+
git.checkout('master')
95+
assert(!git.branch('other_branch').current)
96+
97+
git.checkout(@git.branch('other_branch'))
98+
assert(git.branch('other_branch').current)
10399
end
104100
end
105-
106101
end

tests/units/test_commit_with_empty_message.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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
6-
set_file_paths
6+
clone_working_repo
77
end
88

99
def test_without_allow_empty_message_option

tests/units/test_commit_with_gpg.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
7-
set_file_paths
7+
clone_working_repo
88
end
99

1010
def test_with_configured_gpg_keyid

0 commit comments

Comments
 (0)