Skip to content

JRuby on Windows #480

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
Oct 25, 2020
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
Windows/JRuby fixes/tests/refactorings/travis-ci
Signed-off-by: Andy Maleh <andy.am@gmail.com>
  • Loading branch information
AndyObtiva committed Oct 3, 2020
commit f01715cd73cbedad6478f5dac19f7a11cf32b215
41 changes: 31 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

Copy link
Member

@jcouball jcouball Aug 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about shortening this to the more intention revealing:

def escape(s)
  return escape_for_windows(s) if windows_platform?

  escape_for_sh(s) # not sure what the best name for this method is: are we escaping for sh?  linux? linux_shell?
end

Then move the existing code to fill in the new methods referenced above. Then add a test (or more) for each method.

Copy link
Contributor Author

@AndyObtiva AndyObtiva Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I agree that the suggested refactorings into intention revealing methods make the code a lot more readable/understandable.

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is quite right. What do you think about this:

skip 'Encoding tests do not work in Windows' if windows_platform?

On other platforms, these tests should work not matter what the external encoding is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention... I have not ever seen rescue used in this way. Can you point me to something that explains how you are using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About your first question, if you can figure out a better way to skip tests on Windows, then power to you! After all, they do not work in their current form inside the Windows Command Prompt or Git Bash and I did not feel it was important enough for me to invest more time into it (especially given that there was zero coverage before to begin with, so any coverage now is better than nothing)

About your second question, I learned this from a senior Ruby developer called Chris Sepic more than a decade ago while working at this Chicago suburb company, Leapfrog Online.

Basically, in rare cases where you do not need to do anything with the error exception object, instead of doing this:

begin 
  try_something
rescue => e
  do_nothing_with_e
end

You can drop the error exception object:

begin 
  try_something
rescue
  do_something
end

And then finally, you can fold it into one line:

try_something rescue do_something

I don't advise using this in the majority of cases. I only used it because this was a test (not production code) and brevity helped keep things on one line.

Specifically, there is this odd quirk about JRuby where it calls the encoding constant UTF8 instead of UTF_8, so instead of me writing something very long and elaborate to check it such as:

if Encoding.constants.include?(:UTF_8)
  # do something with Encoding::UTF_8
elsif Encoding.constants.include?(:UTF8)
  # do something with Encoding::UTF8
end

I just shortened it knowing that if you call `Encoding::UTF_8` in JRuby, it bombs, and then the `rescue` keyword rescues it unto the next value to compare to, which is `Encoding::UTF8`

Again, this is not a recommended technique in general. I just did it because it was low-risk code in a test (spec).

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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