Skip to content

Simplify test execution #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ In order to ensure high quality, all pull requests must meet these requirements:
* The entire test suite must pass when `bundle exec rake default` is run from the
project's local working copy.

While working on specific features you can run individual test files or
a group of tests using `bin/test`:

# run a single file:
$ bin/test tests/units/test_object.rb

# run multiple files:
$ bin/test tests/units/test_object.rb tests/units/test_archive.rb

# run all unit tests:
$ bin/test

### Continuous integration
* All tests must pass in the project's [GitHub Continuous Integration build](https://github.com/ruby-git/ruby-git/actions?query=workflow%3ACI)
before the pull request will be merged.
Expand Down
12 changes: 8 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
require 'bundler/gem_tasks'
require 'English'

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

default_tasks = []

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

require File.dirname(__FILE__) + '/tests/all_tests.rb'
# You can run individual test files (or multiple files) from the command
# line with:
#
# $ bin/test tests/units/test_archive.rb
#
# $ bin/test tests/units/test_archive.rb tests/units/test_object.rb
end
default_tasks << :test

Expand Down
19 changes: 19 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'

`git config --global user.email "git@example.com"` if `git config user.email`.empty?
`git config --global user.name "GitExample"` if `git config user.name`.empty?

project_root = File.expand_path(File.join(__dir__, '..'))

$LOAD_PATH.unshift(File.join(project_root, 'tests'))

if ARGV.empty?
paths = Dir.glob(File.join(project_root, 'tests/**/test_*.rb'))
else
paths = ARGV.map { |p| File.join(project_root, p) }
end

paths.each { |p| require p }
8 changes: 0 additions & 8 deletions tests/all_tests.rb

This file was deleted.

43 changes: 26 additions & 17 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,50 @@

class Test::Unit::TestCase

def set_file_paths
cwd = FileUtils.pwd
if File.directory?(File.join(cwd, 'files'))
@test_dir = File.join(cwd, 'files')
elsif File.directory?(File.join(cwd, '..', 'files'))
@test_dir = File.join(cwd, '..', 'files')
elsif File.directory?(File.join(cwd, 'tests', 'files'))
@test_dir = File.join(cwd, 'tests', 'files')
end
TEST_ROOT = File.expand_path(__dir__)
TEST_FIXTURES = File.join(TEST_ROOT, 'files')

@wdir_dot = File.expand_path(File.join(@test_dir, 'working'))
@wbare = File.expand_path(File.join(@test_dir, 'working.git'))
@index = File.expand_path(File.join(@test_dir, 'index'))
BARE_REPO_PATH = File.join(TEST_FIXTURES, 'working.git')

@wdir = create_temp_repo(@wdir_dot)
def clone_working_repo
@wdir = create_temp_repo('working')
end

teardown
def git_teardown
FileUtils.rm_r(@tmp_path) if instance_variable_defined?(:@tmp_path)
end

def create_temp_repo(clone_path)
def in_bare_repo_clone
in_temp_dir do |path|
git = Git.clone(BARE_REPO_PATH, 'bare')
Dir.chdir('bare') do
yield git
end
end
end

def in_temp_repo(clone_name)
clone_path = create_temp_repo(clone_name)
Dir.chdir(clone_path) do
yield
end
end

def create_temp_repo(clone_name)
clone_path = File.join(TEST_FIXTURES, clone_name)
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
@tmp_path = File.expand_path(File.join("/tmp/", filename))
FileUtils.mkdir_p(@tmp_path)
FileUtils.cp_r(clone_path, @tmp_path)
tmp_path = File.join(@tmp_path, 'working')
tmp_path = File.join(@tmp_path, File.basename(clone_path))
FileUtils.cd tmp_path do
FileUtils.mv('dot_git', '.git')
end
tmp_path
end

def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
def in_temp_dir # :yields: the temporary dir's path
tmp_path = nil
while tmp_path.nil? || File.directory?(tmp_path)
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
Expand All @@ -52,7 +61,7 @@ def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
FileUtils.cd tmp_path do
yield tmp_path
end
FileUtils.rm_r(tmp_path) if remove_after
FileUtils.rm_r(tmp_path)
end

def create_file(path, content)
Expand Down
13 changes: 4 additions & 9 deletions tests/units/test_archive.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'

class TestArchive < Test::Unit::TestCase

def setup
set_file_paths
clone_working_repo
@git = Git.open(@wdir)
@tempfiles = []
end
Expand Down Expand Up @@ -56,13 +56,8 @@ def test_archive
assert_match(%r{test/}, lines[1])
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])

in_temp_dir do
c = Git.clone(@wbare, 'new')
c.chdir do
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
assert(File.exist?(f))
end
end
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
assert(File.exist?(f))
end

end
5 changes: 2 additions & 3 deletions tests/units/test_bare.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'

class TestBare < Test::Unit::TestCase

def setup
set_file_paths
@git = Git.bare(@wbare)
@git = Git.bare(BARE_REPO_PATH)
end

def test_commit
Expand Down
4 changes: 2 additions & 2 deletions tests/units/test_base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'

class TestBase < Test::Unit::TestCase

def setup
set_file_paths
clone_working_repo
end

def test_add
Expand Down
101 changes: 48 additions & 53 deletions tests/units/test_branch.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'

class TestBranch < Test::Unit::TestCase
def setup
set_file_paths
clone_working_repo
@git = Git.open(@wdir)

@commit = @git.object('1cc8667014381')
@tree = @git.object('1cc8667014381^{tree}')
@blob = @git.object('v2.5:example.txt')

@branches = @git.branches
end

def test_branches_all
assert(@git.branches[:master].is_a?(Git::Branch))
assert(@git.branches.size > 5)
end

def test_branches_local
bs = @git.branches.local
assert(bs.size > 4)
Expand All @@ -28,22 +28,22 @@ def test_branches_remote
bs = @git.branches.remote
assert_equal(1, bs.size)
end

def test_branches_single
branch = @git.branches[:test_object]
assert_equal('test_object', branch.name)

%w{working/master remotes/working/master}.each do |branch_name|
branch = @git.branches[branch_name]

assert_equal('master', branch.name)
assert_equal('remotes/working/master', branch.full)
assert_equal('working', branch.remote.name)
assert_equal('+refs/heads/*:refs/remotes/working/*', branch.remote.fetch_opts)
assert_equal('../working.git', branch.remote.url)
end
end

def test_true_branch_contains?
assert(@git.branch('git_grep').contains?('master'))
end
Expand All @@ -55,52 +55,47 @@ def test_false_branch_contains?
def test_branch_commit
assert_equal(270, @git.branches[:test_branches].gcommit.size)
end

def test_branch_create_and_switch
in_temp_dir do |path|
g = Git.clone(@wbare, 'branch_test')
Dir.chdir('branch_test') do
assert(!g.branch('new_branch').current)
g.branch('other_branch').create
assert(!g.branch('other_branch').current)
g.branch('new_branch').checkout
assert(g.branch('new_branch').current)

assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)

new_file('test-file1', 'blahblahblah1')
new_file('test-file2', 'blahblahblah2')
new_file('.test-dot-file1', 'blahblahblahdot1')
assert(g.status.untracked.assoc('test-file1'))
assert(g.status.untracked.assoc('.test-dot-file1'))

g.add(['test-file1', 'test-file2'])
assert(!g.status.untracked.assoc('test-file1'))

g.reset
assert(g.status.untracked.assoc('test-file1'))
assert(!g.status.added.assoc('test-file1'))

assert_raise Git::GitExecuteError do
g.branch('new_branch').delete
end
assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)

g.branch('master').checkout
g.branch('new_branch').delete
assert_equal(0, g.branches.select { |b| b.name == 'new_branch' }.size)

g.checkout('other_branch')
assert(g.branch('other_branch').current)

g.checkout('master')
assert(!g.branch('other_branch').current)

g.checkout(g.branch('other_branch'))
assert(g.branch('other_branch').current)

in_bare_repo_clone do |git|
assert(!git.branch('new_branch').current)
git.branch('other_branch').create
assert(!git.branch('other_branch').current)
git.branch('new_branch').checkout
assert(git.branch('new_branch').current)

assert_equal(1, git.branches.select { |b| b.name == 'new_branch' }.size)

new_file('test-file1', 'blahblahblah1')
new_file('test-file2', 'blahblahblah2')
new_file('.test-dot-file1', 'blahblahblahdot1')
assert(git.status.untracked.assoc('test-file1'))
assert(git.status.untracked.assoc('.test-dot-file1'))

git.add(['test-file1', 'test-file2'])
assert(!git.status.untracked.assoc('test-file1'))

git.reset
assert(git.status.untracked.assoc('test-file1'))
assert(!git.status.added.assoc('test-file1'))

assert_raise Git::GitExecuteError do
git.branch('new_branch').delete
end
assert_equal(1, git.branches.select { |b| b.name == 'new_branch' }.size)

git.branch('master').checkout
git.branch('new_branch').delete
assert_equal(0, git.branches.select { |b| b.name == 'new_branch' }.size)

git.checkout('other_branch')
assert(git.branch('other_branch').current)

git.checkout('master')
assert(!git.branch('other_branch').current)

git.checkout(@git.branch('other_branch'))
assert(git.branch('other_branch').current)
end
end

end
4 changes: 2 additions & 2 deletions tests/units/test_commit_with_empty_message.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'

class TestCommitWithEmptyMessage < Test::Unit::TestCase
def setup
set_file_paths
clone_working_repo
end

def test_without_allow_empty_message_option
Expand Down
4 changes: 2 additions & 2 deletions tests/units/test_commit_with_gpg.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'

class TestCommitWithGPG < Test::Unit::TestCase
def setup
set_file_paths
clone_working_repo
end

def test_with_configured_gpg_keyid
Expand Down
Loading