From 8d219a9bd7b484b301185319c940975e1d788b3e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 18 Sep 2023 02:11:48 +0900 Subject: [PATCH] Refactor test_sync_default_gems.rb --- tool/test/test_sync_default_gems.rb | 85 +++++++++++++++-------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/tool/test/test_sync_default_gems.rb b/tool/test/test_sync_default_gems.rb index a73fc65d6e..2d46d5b6bf 100755 --- a/tool/test/test_sync_default_gems.rb +++ b/tool/test/test_sync_default_gems.rb @@ -1,6 +1,7 @@ #!/usr/bin/ruby require 'test/unit' require 'stringio' +require 'tmpdir' require_relative '../sync_default_gems' module Test_SyncDefaultGems @@ -83,31 +84,31 @@ module Test_SyncDefaultGems @git_config = %W"HOME GIT_CONFIG_GLOBAL".each_with_object({}) {|k, c| c[k] = ENV[k]} ENV["HOME"] = @testdir ENV["GIT_CONFIG_GLOBAL"] = @testdir + "/gitconfig" - system(*%W"git config --global user.email test@ruby-lang.org") - system(*%W"git config --global user.name", "Ruby") - system(*%W"git config --global init.defaultBranch default") + git(*%W"config --global user.email test@ruby-lang.org") + git(*%W"config --global user.name", "Ruby") + git(*%W"config --global init.defaultBranch default") @target = "sync-test" SyncDefaultGems::REPOSITORIES[@target] = ["ruby/#{@target}", "default"] @sha = {} @origdir = Dir.pwd Dir.chdir(@testdir) ["src", @target].each do |dir| - system(*%W"git init -q #{dir}", exception: true) + git(*%W"init -q #{dir}") if dir == "src" Dir.mkdir("#{dir}/lib") File.write("#{dir}/lib/fine.rb", "return\n") Dir.mkdir("#{dir}/test") File.write("#{dir}/test/test_fine.rb", "return\n") - system(*%W"git add lib/fine.rb test/test_fine.rb", exception: true, chdir: dir) - system(*%W"git commit -q -m", "Looks fine", exception: true, chdir: dir) + git(*%W"add lib/fine.rb test/test_fine.rb", chdir: dir) + git(*%W"commit -q -m", "Looks fine", chdir: dir) end Dir.mkdir("#{dir}/tool") File.write("#{dir}/tool/ok", "#!/bin/sh\n""echo ok\n") - system(*%W"git add tool/ok", exception: true, chdir: dir) - system(*%W"git commit -q -m", "Add tool #{dir}", exception: true, chdir: dir) - @sha[dir] = IO.popen(%W[git log --format=%H -1], chdir: dir, &:read).chomp + git(*%W"add tool/ok", chdir: dir) + git(*%W"commit -q -m", "Add tool #{dir}", chdir: dir) + @sha[dir] = top_commit(dir) end - system(*%W"git remote add #{@target} ../#{@target}", exception: true, chdir: "src") + git(*%W"remote add #{@target} ../#{@target}", chdir: "src") end def teardown @@ -121,6 +122,7 @@ module Test_SyncDefaultGems end def capture_process_output_to(outputs) + return yield unless outputs&.empty? == false IO.pipe do |r, w| orig = outputs.map {|out| out.dup} outputs.each {|out| out.reopen(w)} @@ -145,42 +147,49 @@ module Test_SyncDefaultGems return out, err end - def test_skip_tool - system(*%W"git rm -q tool/ok", exception: true, chdir: @target) - system(*%W"git commit -q -m", "Remove tool", exception: true, chdir: @target) + def git(*commands, **opts) + system("git", *commands, exception: true, **opts) + end + + def top_commit(dir, format: "%H") + IO.popen(%W[git log --format=#{format} -1], chdir: dir, &:read)&.chomp + end + + def assert_sync(commits = true, success: true) + result = nil out = capture_process_output_to([STDOUT, STDERR]) do Dir.chdir("src") do - SyncDefaultGems.sync_default_gems_with_commits(@target, true) + result = SyncDefaultGems.sync_default_gems_with_commits(@target, commits) end end - assert_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out) + assert_equal(success, result, out) + out + end + + def test_skip_tool + git(*%W"rm -q tool/ok", chdir: @target) + git(*%W"commit -q -m", "Remove tool", chdir: @target) + out = assert_sync() + assert_equal(@sha["src"], top_commit("src"), out) end def test_skip_test_fixtures Dir.mkdir("#@target/test") Dir.mkdir("#@target/test/fixtures") File.write("#@target/test/fixtures/fixme.rb", "") - system(*%W"git add test/fixtures/fixme.rb", exception: true, chdir: @target) - system(*%W"git commit -q -m", "Add fitures", exception: true, chdir: @target) - out = capture_process_output_to([STDOUT, STDERR]) do - Dir.chdir("src") do - SyncDefaultGems.sync_default_gems_with_commits(@target, ["#{@sha[@target]}..#{@target}/default"]) - end - end - assert_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out) + git(*%W"add test/fixtures/fixme.rb", chdir: @target) + git(*%W"commit -q -m", "Add fixtures", chdir: @target) + out = assert_sync(["#{@sha[@target]}..#{@target}/default"]) + assert_equal(@sha["src"], top_commit("src"), out) end def test_skip_toplevel Dir.mkdir("#@target/docs") File.write("#@target/docs/NEWS.md", "= NEWS!!!\n") - system(*%W"git add --", "docs/NEWS.md", exception: true, chdir: @target) - system(*%W"git commit -q -m", "It's a news", exception: true, chdir: @target) - out = capture_process_output_to([STDOUT, STDERR]) do - Dir.chdir("src") do - SyncDefaultGems.sync_default_gems_with_commits(@target, true) - end - end - assert_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out) + git(*%W"add --", "docs/NEWS.md", chdir: @target) + File.write("#@target/docs/hello.md", "Hello\n") + out = assert_sync() + assert_equal(@sha["src"], top_commit("src"), out) end def test_adding_toplevel @@ -188,16 +197,12 @@ module Test_SyncDefaultGems File.write("#@target/docs/NEWS.md", "= New library\n") Dir.mkdir("#@target/lib") File.write("#@target/lib/news.rb", "return\n") - system(*%W"git add --", "docs/NEWS.md", "lib/news.rb", exception: true, chdir: @target) - system(*%W"git commit -q -m", "New lib", exception: true, chdir: @target) - out = capture_process_output_to([STDOUT, STDERR]) do - Dir.chdir("src") do - SyncDefaultGems.sync_default_gems_with_commits(@target, true) - end - end - assert_not_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out) + git(*%W"add --", "docs/NEWS.md", "lib/news.rb", chdir: @target) + git(*%W"commit -q -m", "New lib", chdir: @target) + out = assert_sync() + assert_not_equal(@sha["src"], top_commit("src"), out) assert_equal "return\n", File.read("src/lib/news.rb") - assert_include IO.popen(%W[git log -1 --oneline], chdir: "src", &:read), "[ruby/#{@target}] New lib" + assert_include top_commit("src", format: "oneline"), "[ruby/#{@target}] New lib" assert_not_operator File, :exist?, "src/docs" end end