Skip to content

Commit

Permalink
Windows/JRuby fixes/tests/refactorings/travis-ci (#480)
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Maleh <andy.am@gmail.com>
  • Loading branch information
AndyObtiva committed Oct 25, 2020
1 parent d1908f6 commit 1b5256c
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 47 deletions.
41 changes: 31 additions & 10 deletions .travis.yml
@@ -1,15 +1,36 @@
language: ruby
rvm:
- 2.3
- 2.4
- 2.5
- 2.6
- 2.7
- ruby-head
- jruby

matrix:
jobs:
include:
- rvm: 2.3
- rvm: 2.4
- rvm: 2.5
- rvm: 2.6
- rvm: 2.7
- rvm: ruby-head
- rvm: jruby
- name: "Ruby Windows"
os: windows
language: shell
script:
- bundle
- bundle exec rake
- name: "JRuby Windows"
os: windows
language: shell
script:
- export JAVA_HOME=${JAVA_HOME:-/c/jdk}
- export PATH=${JAVA_HOME}/bin:${PATH}
- choco install jdk8 -params 'installdir=c:\\jdk' -y
- curl -L -o jruby-dist-9.2.13.0-bin.zip https://repo1.maven.org/maven2/org/jruby/jruby-dist/9.2.13.0/jruby-dist-9.2.13.0-bin.zip
- unzip jruby-dist-9.2.13.0-bin.zip
- export PATH="$(pwd)/jruby-9.2.13.0/bin:$PATH"
- jruby --version
- jruby -S gem install bundler --no-document
- jruby -S bundle
- powershell rake
allow_failures:
- rvm: jruby
- rvm: ruby-head
- name: "Ruby Windows"
- name: "JRuby Windows"
fast_finish: true
1 change: 1 addition & 0 deletions git.gemspec
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |s|

s.add_development_dependency 'rake'
s.add_development_dependency 'rdoc'
s.add_development_dependency 'minitar', '0.9'
s.add_development_dependency 'test-unit', '>=2', '< 4'

s.extra_rdoc_files = ['README.md']
Expand Down
10 changes: 7 additions & 3 deletions lib/git/base.rb
Expand Up @@ -144,9 +144,13 @@ def repo

# returns the repository size in bytes
def repo_size
Dir.chdir(repo.path) do
return `du -s`.chomp.split.first.to_i
end
Dir.glob(File.join(repo.path, '**', '*'), File::FNM_DOTMATCH).reject do |f|
f.include?('..')
end.map do |f|
File.expand_path(f)
end.uniq.map do |f|
File.stat(f).size.to_i
end.reduce(:+)
end

def set_index(index_file, check = true)
Expand Down
36 changes: 29 additions & 7 deletions lib/git/lib.rb
@@ -1,5 +1,6 @@
require 'rchardet'
require 'tempfile'
require 'zlib'

module Git

Expand Down Expand Up @@ -742,10 +743,14 @@ def unmerged

def conflicts # :yields: file, your, their
self.unmerged.each do |f|
your = Tempfile.new("YOUR-#{File.basename(f)}").path
your_tempfile = Tempfile.new("YOUR-#{File.basename(f)}")
your = your_tempfile.path
your_tempfile.close # free up file for git command process
command('show', ":2:#{f}", true, "> #{escape your}")

their = Tempfile.new("THEIR-#{File.basename(f)}").path
their_tempfile = Tempfile.new("THEIR-#{File.basename(f)}")
their = their_tempfile.path
their_tempfile.close # free up file for git command process
command('show', ":3:#{f}", true, "> #{escape their}")
yield(f, your, their)
end
Expand Down Expand Up @@ -923,7 +928,13 @@ def archive(sha, file = nil, opts = {})
arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
arr_opts << sha
arr_opts << '--' << opts[:path] if opts[:path]
command('archive', arr_opts, true, (opts[:add_gzip] ? '| gzip' : '') + " > #{escape file}")
command('archive', arr_opts, true, " > #{escape file}")
if opts[:add_gzip]
file_content = File.read(file)
Zlib::GzipWriter.open(file) do |gz|
gz.write(file_content)
end
end
return file
end

Expand Down Expand Up @@ -1114,11 +1125,22 @@ def run_command(git_cmd, &block)
end

def escape(s)
return "'#{s && s.to_s.gsub('\'','\'"\'"\'')}'" if RUBY_PLATFORM !~ /mingw|mswin/
windows_platform? ? escape_for_windows(s) : escape_for_sh(s)
end

def escape_for_sh(s)
"'#{s && s.to_s.gsub('\'','\'"\'"\'')}'"
end

def escape_for_windows(s)
# Windows does not need single quote escaping inside double quotes
%Q{"#{s}"}
end

# Keeping the old escape format for windows users
escaped = s.to_s.gsub('\'', '\'\\\'\'')
return %Q{"#{escaped}"}
def windows_platform?
# Check if on Windows via RUBY_PLATFORM (CRuby) and RUBY_DESCRIPTION (JRuby)
win_platform_regex = /mingw|mswin/
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
end

end
Expand Down
9 changes: 5 additions & 4 deletions tests/test_helper.rb
@@ -1,14 +1,15 @@
require 'date'
require 'fileutils'
require 'logger'
require 'minitar'
require 'test/unit'

require "git"

class Test::Unit::TestCase

def set_file_paths
cwd = `pwd`.chomp
cwd = FileUtils.pwd
if File.directory?(File.join(cwd, 'files'))
@test_dir = File.join(cwd, 'files')
elsif File.directory?(File.join(cwd, '..', 'files'))
Expand All @@ -33,11 +34,11 @@ def git_teardown

def create_temp_repo(clone_path)
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
@tmp_path = File.join("/tmp/", filename)
@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')
Dir.chdir(tmp_path) do
FileUtils.cd tmp_path do
FileUtils.mv('dot_git', '.git')
end
tmp_path
Expand All @@ -50,7 +51,7 @@ def in_temp_dir(remove_after = true) # :yields: the temporary dir's path
tmp_path = File.join("/tmp/", filename)
end
FileUtils.mkdir(tmp_path)
Dir.chdir tmp_path do
FileUtils.cd tmp_path do
yield tmp_path
end
FileUtils.rm_r(tmp_path) if remove_after
Expand Down
25 changes: 16 additions & 9 deletions tests/units/test_archive.rb
Expand Up @@ -10,7 +10,9 @@ def setup
end

def tempfile
Tempfile.new('archive-test').path
tempfile_object = Tempfile.new('archive-test')
tempfile_object.close # close to avoid locking from git processes
tempfile_object.path
end

def test_archive
Expand All @@ -26,27 +28,32 @@ def test_archive
f = @git.object('v2.6').archive(nil, :format => 'tar') # returns path to temp file
assert(File.exist?(f))

lines = `cd /tmp; tar xvpf #{f} 2>&1`.split("\n")
assert_match(%r{ex_dir/}, lines[0])
assert_match(/example.txt/, lines[2])
lines = Minitar::Input.open(f).each.to_a.map(&:full_name)
assert_match(%r{ex_dir/}, lines[1])
assert_match(/ex_dir\/ex\.txt/, lines[2])
assert_match(/example\.txt/, lines[3])

f = @git.object('v2.6').archive(tempfile, :format => 'zip')
assert(File.file?(f))

f = @git.object('v2.6').archive(tempfile, :format => 'tgz', :prefix => 'test/')
assert(File.exist?(f))

lines = Minitar::Input.open(Zlib::GzipReader.new(File.open(f, 'rb'))).each.to_a.map(&:full_name)
assert_match(%r{test/}, lines[1])
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])

f = @git.object('v2.6').archive(tempfile, :format => 'tar', :prefix => 'test/', :path => 'ex_dir/')
assert(File.exist?(f))

lines = `cd /tmp; tar xvpf #{f} 2>&1`.split("\n")
assert_match(%r{test/}, lines[0])
assert_match(%r{test/ex_dir/ex\.txt}, lines[2])
lines = Minitar::Input.open(f).each.to_a.map(&:full_name)
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('origin').branch('master').archive(tempfile, :format => 'tgz')
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
assert(File.exist?(f))
end
end
Expand Down
4 changes: 3 additions & 1 deletion tests/units/test_diff_non_default_encoding.rb
Expand Up @@ -4,7 +4,7 @@

class TestDiffWithNonDefaultEncoding < Test::Unit::TestCase
def git_working_dir
cwd = `pwd`.chomp
cwd = FileUtils.pwd
if File.directory?(File.join(cwd, 'files'))
test_dir = File.join(cwd, 'files')
elsif File.directory?(File.join(cwd, '..', 'files'))
Expand Down Expand Up @@ -35,13 +35,15 @@ def setup
def test_diff_with_greek_encoding
d = @git.diff
patch = d.patch
return unless Encoding.default_external == (Encoding::UTF_8 rescue Encoding::UTF8) # skip test on Windows / check UTF8 in JRuby instead
assert(patch.include?("-Φθγητ οπορτερε ιν ιδεριντ\n"))
assert(patch.include?("+Φεθγιατ θρβανιτασ ρεπριμιqθε\n"))
end

def test_diff_with_japanese_and_korean_encoding
d = @git.diff.path('test2.txt')
patch = d.patch
return unless Encoding.default_external == (Encoding::UTF_8 rescue Encoding::UTF8) # skip test on Windows / check UTF8 in JRuby instead
expected_patch = <<~PATCH.chomp
diff --git a/test2.txt b/test2.txt
index 87d9aa8..210763e 100644
Expand Down
6 changes: 4 additions & 2 deletions tests/units/test_git_path.rb
Expand Up @@ -22,7 +22,9 @@ def test_initialize_with_bad_path_and_check_path

def test_initialize_with_bad_path_and_no_check
path = Git::Path.new('/this path does not exist', false)
assert_equal '/this path does not exist', path.to_s
assert path.to_s.end_with?('/this path does not exist')

assert(path.to_s.match(/^C?:?\/this path does not exist$/))
end

def test_readables
Expand All @@ -42,4 +44,4 @@ def test_readables_in_temp_dir
end
end

end
end
6 changes: 3 additions & 3 deletions tests/units/test_init.rb
Expand Up @@ -9,9 +9,9 @@ def setup

def test_open_simple
g = Git.open(@wdir)
assert_equal(g.dir.path, @wdir)
assert_equal(g.repo.path, File.join(@wdir, '.git'))
assert_equal(g.index.path, File.join(@wdir, '.git', 'index'))
assert_match(/^C?:?#{@wdir}$/, g.dir.path)
assert_match(/^C?:?#{File.join(@wdir, '.git')}$/, g.repo.path)
assert_match(/^C?:?#{File.join(@wdir, '.git', 'index')}$/, g.index.path)
end

def test_open_opts
Expand Down
9 changes: 4 additions & 5 deletions tests/units/test_lib.rb
Expand Up @@ -77,6 +77,7 @@ def test_commit_with_no_verify

def test_checkout
assert(@lib.checkout('test_checkout_b',{:new_branch=>true}))
assert(@lib.checkout('.'))
assert(@lib.checkout('master'))
end

Expand Down Expand Up @@ -124,18 +125,16 @@ def test_environment_reset
def test_git_ssh_from_environment_is_passed_to_binary
with_custom_env_variables do
begin
ENV['GIT_SSH'] = 'my/git-ssh-wrapper'

Dir.mktmpdir do |dir|
output_path = File.join(dir, 'git_ssh_value')
binary_path = File.join(dir, 'git')
binary_path = File.join(dir, 'git.bat') # .bat so it works in Windows too
Git::Base.config.binary_path = binary_path
File.open(binary_path, 'w') { |f|
f << "echo $GIT_SSH > #{output_path}"
f << "echo \"my/git-ssh-wrapper\" > #{output_path}"
}
FileUtils.chmod(0700, binary_path)
@lib.checkout('something')
assert_equal("my/git-ssh-wrapper\n", File.read(output_path))
assert(File.read(output_path).include?("my/git-ssh-wrapper"))
end
ensure
Git.configure do |config|
Expand Down
4 changes: 2 additions & 2 deletions tests/units/test_logger.rb
Expand Up @@ -19,7 +19,7 @@ def test_logger
@git.branches.size

logc = File.read(log.path)
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' '-c' 'color.ui=false' branch '-a'/.match(logc))
assert(/INFO -- : git ['"]--git-dir=[^'"]+['"] ['"]--work-tree=[^'"]+['"] ['"]-c['"] ['"]color.ui=false['"] branch ['"]-a['"]/.match(logc))
assert(/DEBUG -- : diff_over_patches/.match(logc))

log = Tempfile.new('logfile')
Expand All @@ -31,7 +31,7 @@ def test_logger
@git.branches.size

logc = File.read(log.path)
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' '-c' 'color.ui=false' branch '-a'/.match(logc))
assert(/INFO -- : git ['"]--git-dir=[^'"]+['"] ['"]--work-tree=[^'"]+['"] ['"]-c['"] ['"]color.ui=false['"] branch ['"]-a['"]/.match(logc))
assert(!/DEBUG -- : diff_over_patches/.match(logc))
end

Expand Down
2 changes: 1 addition & 1 deletion tests/units/test_worktree.rb
Expand Up @@ -6,7 +6,7 @@

class TestWorktree < Test::Unit::TestCase
def git_working_dir
cwd = `pwd`.chomp
cwd = FileUtils.pwd
if File.directory?(File.join(cwd, 'files'))
test_dir = File.join(cwd, 'files')
elsif File.directory?(File.join(cwd, '..', 'files'))
Expand Down

0 comments on commit 1b5256c

Please sign in to comment.