Skip to content
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

Speed up parallel_tests by splitting integration tests. #1156

Merged
merged 2 commits into from
Sep 27, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
194 changes: 194 additions & 0 deletions Library/Homebrew/test/helper/integration_command_test_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
require "bundler"
require "testing_env"
require "fileutils"
require "pathname"
require "formula"

class IntegrationCommandTestCase < Homebrew::TestCase
def setup
@cmd_id_index = 0 # Assign unique IDs to invocations of `cmd_output`.
(HOMEBREW_PREFIX/"bin").mkpath
FileUtils.touch HOMEBREW_PREFIX/"bin/brew"
end

def teardown
coretap = CoreTap.new
paths_to_delete = [
HOMEBREW_LINKED_KEGS,
HOMEBREW_PINNED_KEGS,
HOMEBREW_CELLAR.children,
HOMEBREW_CACHE.children,
HOMEBREW_LOCK_DIR.children,
HOMEBREW_LOGS.children,
HOMEBREW_TEMP.children,
HOMEBREW_PREFIX/"bin",
HOMEBREW_PREFIX/"share",
HOMEBREW_PREFIX/"opt",
HOMEBREW_LIBRARY/"Taps/caskroom",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-bundle",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-foo",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-services",
HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-shallow",
HOMEBREW_REPOSITORY/".git",
coretap.path/".git",
coretap.alias_dir,
coretap.formula_dir.children,
coretap.path/"formula_renames.json",
].flatten
FileUtils.rm_rf paths_to_delete
end

def needs_test_cmd_taps
return if ENV["HOMEBREW_TEST_OFFICIAL_CMD_TAPS"]
skip "HOMEBREW_TEST_OFFICIAL_CMD_TAPS is not set"
end

def needs_macos
skip "Not on MacOS" unless OS.mac?
end

def cmd_id_from_args(args)
args_pretty = args.join(" ").gsub(TEST_TMPDIR, "@TMPDIR@")
test_pretty = "#{self.class.name}\##{name}.#{@cmd_id_index += 1}"
"[#{test_pretty}] brew #{args_pretty}"
end

def cmd_output(*args)
# 1.8-compatible way of writing def cmd_output(*args, **env)
env = args.last.is_a?(Hash) ? args.pop : {}
cmd_args = %W[
-W0
-I#{HOMEBREW_LIBRARY_PATH}/test/lib
-rconfig
]
if ENV["HOMEBREW_TESTS_COVERAGE"]
# This is needed only because we currently use a patched version of
# simplecov, and gems installed through git are not available without
# requiring bundler/setup first. See also the comment in test/Gemfile.
# Remove this line when we'll switch back to a stable simplecov release.
cmd_args << "-rbundler/setup"
cmd_args << "-rsimplecov"
end
cmd_args << "-rintegration_mocks"
cmd_args << (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path.to_s
cmd_args += args
Bundler.with_original_env do
ENV["HOMEBREW_BREW_FILE"] = HOMEBREW_PREFIX/"bin/brew"
ENV["HOMEBREW_INTEGRATION_TEST"] = cmd_id_from_args(args)
ENV["HOMEBREW_TEST_TMPDIR"] = TEST_TMPDIR
env.each_pair { |k, v| ENV[k] = v }

read, write = IO.pipe
begin
pid = fork do
read.close
$stdout.reopen(write)
$stderr.reopen(write)
write.close
exec RUBY_PATH, *cmd_args
end
write.close
read.read.chomp
ensure
Process.wait(pid)
read.close
end
end
end

def cmd(*args)
output = cmd_output(*args)
status = $?.exitstatus
puts "\n#{output}" if status.nonzero?
assert_equal 0, status
output
end

def cmd_fail(*args)
output = cmd_output(*args)
status = $?.exitstatus
$stderr.puts "\n#{output}" if status.zero?
refute_equal 0, status
output
end

def setup_test_formula(name, content = nil)
formula_path = CoreTap.new.formula_dir/"#{name}.rb"

case name
when /^testball/
content = <<-EOS.undent
desc "Some test"
homepage "https://example.com/#{name}"
url "file://#{File.expand_path("../..", __FILE__)}/tarballs/testball-0.1.tbz"
sha256 "#{TESTBALL_SHA256}"

option "with-foo", "Build with foo"
#{content}

def install
(prefix/"foo"/"test").write("test") if build.with? "foo"
prefix.install Dir["*"]
(buildpath/"test.c").write \
"#include <stdio.h>\\nint main(){return printf(\\"test\\");}"
bin.mkpath
system ENV.cc, "test.c", "-o", bin/"test"
end

# something here
EOS
when "foo"
content = <<-EOS.undent
url "https://example.com/#{name}-1.0"
EOS
when "bar"
content = <<-EOS.undent
url "https://example.com/#{name}-1.0"
depends_on "foo"
EOS
end

formula_path.write <<-EOS.undent
class #{Formulary.class_s(name)} < Formula
#{content}
end
EOS

formula_path
end

def setup_remote_tap(name)
tap = Tap.fetch name
tap.install(full_clone: false, quiet: true) unless tap.installed?
tap
end

def install_and_rename_coretap_formula(old_name, new_name)
core_tap = CoreTap.new
core_tap.path.cd do
shutup do
system "git", "init"
system "git", "add", "--all"
system "git", "commit", "-m",
"#{old_name.capitalize} has not yet been renamed"
end
end

cmd("install", old_name)
(core_tap.path/"Formula/#{old_name}.rb").unlink
formula_renames = core_tap.path/"formula_renames.json"
formula_renames.write Utils::JSON.dump(old_name => new_name)

core_tap.path.cd do
shutup do
system "git", "add", "--all"
system "git", "commit", "-m",
"#{old_name.capitalize} has been renamed to #{new_name.capitalize}"
end
end
end

def testball
"#{File.expand_path("../..", __FILE__)}/testball.rb"
end
end
28 changes: 28 additions & 0 deletions Library/Homebrew/test/test_ENV.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
require "testing_env"
require "extend/ENV"
require "helper/integration_command_test_case"

class IntegrationCommandTestEnv < IntegrationCommandTestCase
def test_env
assert_match(/CMAKE_PREFIX_PATH="#{Regexp.escape(HOMEBREW_PREFIX)}[:"]/,
cmd("--env"))
end

def test_env_fish
assert_match(/set [-]gx CMAKE_PREFIX_PATH "#{Regexp.quote(HOMEBREW_PREFIX.to_s)}"/,
cmd("--env", "--shell=fish"))
end

def test_env_csh
assert_match(/setenv CMAKE_PREFIX_PATH #{Regexp.quote(HOMEBREW_PREFIX.to_s)};/,
cmd("--env", "--shell=tcsh"))
end

def test_env_bash
assert_match(/export CMAKE_PREFIX_PATH="#{Regexp.quote(HOMEBREW_PREFIX.to_s)}"/,
cmd("--env", "--shell=bash"))
end

def test_env_plain
assert_match(/CMAKE_PREFIX_PATH: #{Regexp.quote(HOMEBREW_PREFIX)}/,
cmd("--env", "--plain"))
end
end

module SharedEnvTests
def setup
Expand Down
26 changes: 26 additions & 0 deletions Library/Homebrew/test/test_analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestAnalytics < IntegrationCommandTestCase
def test_analytics
HOMEBREW_REPOSITORY.cd do
shutup do
system "git", "init"
end
end

assert_match "Analytics is disabled (by HOMEBREW_NO_ANALYTICS)",
cmd("analytics", "HOMEBREW_NO_ANALYTICS" => "1")

cmd("analytics", "off")
assert_match "Analytics is disabled",
cmd("analytics", "HOMEBREW_NO_ANALYTICS" => nil)

cmd("analytics", "on")
assert_match "Analytics is enabled", cmd("analytics",
"HOMEBREW_NO_ANALYTICS" => nil)

assert_match "Invalid usage", cmd_fail("analytics", "on", "off")
assert_match "Invalid usage", cmd_fail("analytics", "testball")
cmd("analytics", "regenerate-uuid")
end
end
21 changes: 21 additions & 0 deletions Library/Homebrew/test/test_bottle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestBottle < IntegrationCommandTestCase
def test_bottle
cmd("install", "--build-bottle", testball)
assert_match "Formula not from core or any taps",
cmd_fail("bottle", "--no-rebuild", testball)

setup_test_formula "testball"

# `brew bottle` should not fail with dead symlink
# https://github.com/Homebrew/legacy-homebrew/issues/49007
(HOMEBREW_CELLAR/"testball/0.1").cd do
FileUtils.ln_s "not-exist", "symlink"
end
assert_match(/testball-0\.1.*\.bottle\.tar\.gz/,
cmd_output("bottle", "--no-rebuild", "testball"))
ensure
FileUtils.rm_f Dir["testball-0.1*.bottle.tar.gz"]
end
end
22 changes: 22 additions & 0 deletions Library/Homebrew/test/test_bundle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestBundle < IntegrationCommandTestCase
def test_bundle
needs_test_cmd_taps
setup_remote_tap("homebrew/bundle")
HOMEBREW_REPOSITORY.cd do
shutup do
system "git", "init"
system "git", "commit", "--allow-empty", "-m", "This is a test commit"
end
end

mktmpdir do |path|
FileUtils.touch "#{path}/Brewfile"
Dir.chdir path do
assert_equal "The Brewfile's dependencies are satisfied.",
cmd("bundle", "check")
end
end
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCache < IntegrationCommandTestCase
def test_cache
assert_equal HOMEBREW_CACHE.to_s,
cmd("--cache")
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_cache_formula.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCacheFormula < IntegrationCommandTestCase
def test_cache_formula
assert_match %r{#{HOMEBREW_CACHE}/testball-},
cmd("--cache", testball)
end
end
10 changes: 10 additions & 0 deletions Library/Homebrew/test/test_cask.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCask < IntegrationCommandTestCase
def test_cask
needs_test_cmd_taps
needs_macos
setup_remote_tap("caskroom/cask")
cmd("cask", "list")
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCat < IntegrationCommandTestCase
def test_cat
formula_file = setup_test_formula "testball"
assert_equal formula_file.read.chomp, cmd("cat", "testball")
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_cellar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCellar < IntegrationCommandTestCase
def test_cellar
assert_equal HOMEBREW_CELLAR.to_s,
cmd("--cellar")
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_cellar_formula.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCellarFormula < IntegrationCommandTestCase
def test_cellar_formula
assert_match "#{HOMEBREW_CELLAR}/testball",
cmd("--cellar", testball)
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
require "cleanup"
require "fileutils"
require "pathname"
require "helper/integration_command_test_case"

class IntegrationCommandTestCleanup < IntegrationCommandTestCase
def test_cleanup
(HOMEBREW_CACHE/"test").write "test"
assert_match "#{HOMEBREW_CACHE}/test", cmd("cleanup", "--prune=all")
end
end

class CleanupTests < Homebrew::TestCase
def setup
Expand Down
11 changes: 11 additions & 0 deletions Library/Homebrew/test/test_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "helper/integration_command_test_case"

class IntegrationCommandTestCommand < IntegrationCommandTestCase
def test_command
assert_equal "#{HOMEBREW_LIBRARY_PATH}/cmd/info.rb",
cmd("command", "info")

assert_match "Unknown command",
cmd_fail("command", "I-don't-exist")
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/test_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
require "cmd/command"
require "cmd/commands"
require "fileutils"
require "helper/integration_command_test_case"

class IntegrationCommandTestCommands < IntegrationCommandTestCase
def test_commands
assert_match "Built-in commands",
cmd("commands")
end
end

class CommandsTests < Homebrew::TestCase
def setup
Expand Down