Skip to content
Snippets Groups Projects
Commit cd677cf3 authored by Markus Reiter's avatar Markus Reiter
Browse files

Speed up `parallel_tests` by splitting integration tests.

parent 7ccc554f
No related branches found
No related tags found
No related merge requests found
Showing
with 417 additions and 899 deletions
require "bundler"
require "testing_env"
require "fileutils"
require "pathname"
require "formula"
class IntegrationCommandTests < 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
This diff is collapsed.
require "integration_cmds_tests"
class IntegrationCommandTestAnalytics < IntegrationCommandTests
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
require "integration_cmds_tests"
class IntegrationCommandTestBottle < IntegrationCommandTests
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
require "integration_cmds_tests"
class IntegrationCommandTestBundle < IntegrationCommandTests
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
require "integration_cmds_tests"
class IntegrationCommandTestCache < IntegrationCommandTests
def test_cache
assert_equal HOMEBREW_CACHE.to_s,
cmd("--cache")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCacheFormula < IntegrationCommandTests
def test_cache_formula
assert_match %r{#{HOMEBREW_CACHE}/testball-},
cmd("--cache", testball)
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCask < IntegrationCommandTests
def test_cask
needs_test_cmd_taps
needs_macos
setup_remote_tap("caskroom/cask")
cmd("cask", "list")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCat < IntegrationCommandTests
def test_cat
formula_file = setup_test_formula "testball"
assert_equal formula_file.read.chomp, cmd("cat", "testball")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCellar < IntegrationCommandTests
def test_cellar
assert_equal HOMEBREW_CELLAR.to_s,
cmd("--cellar")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCellarFormula < IntegrationCommandTests
def test_cellar_formula
assert_match "#{HOMEBREW_CELLAR}/testball",
cmd("--cellar", testball)
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCleanup < IntegrationCommandTests
def test_cleanup
(HOMEBREW_CACHE/"test").write "test"
assert_match "#{HOMEBREW_CACHE}/test", cmd("cleanup", "--prune=all")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCommand < IntegrationCommandTests
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
require "integration_cmds_tests"
class IntegrationCommandTestCommands < IntegrationCommandTests
def test_commands
assert_match "Built-in commands",
cmd("commands")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestConfig < IntegrationCommandTests
def test_config
assert_match "HOMEBREW_VERSION: #{HOMEBREW_VERSION}",
cmd("config")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCreate < IntegrationCommandTests
def test_create
url = "file://#{File.expand_path("..", __FILE__)}/tarballs/testball-0.1.tbz"
cmd("create", url, "HOMEBREW_EDITOR" => "/bin/cat")
formula_file = CoreTap.new.formula_dir/"testball.rb"
assert formula_file.exist?, "The formula source should have been created"
assert_match %(sha256 "#{TESTBALL_SHA256}"), formula_file.read
end
end
require "integration_cmds_tests"
class IntegrationCommandTestCustomCommand < IntegrationCommandTests
def test_custom_command
mktmpdir do |path|
cmd = "int-test-#{rand}"
file = "#{path}/brew-#{cmd}"
File.open(file, "w") { |f| f.write "#!/bin/sh\necho 'I am #{cmd}'\n" }
FileUtils.chmod 0777, file
assert_match "I am #{cmd}",
cmd(cmd, "PATH" => "#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}")
end
end
end
require "integration_cmds_tests"
class IntegrationCommandTestDeps < IntegrationCommandTests
def test_deps
setup_test_formula "foo"
setup_test_formula "bar"
setup_test_formula "baz", <<-EOS.undent
url "https://example.com/baz-1.0"
depends_on "bar"
EOS
assert_equal "", cmd("deps", "foo")
assert_equal "foo", cmd("deps", "bar")
assert_equal "bar\nfoo", cmd("deps", "baz")
end
end
require "integration_cmds_tests"
class IntegrationCommandTestDesc < IntegrationCommandTests
def test_desc
setup_test_formula "testball"
assert_equal "testball: Some test", cmd("desc", "testball")
assert_match "Pick one, and only one", cmd_fail("desc", "--search", "--name")
assert_match "You must provide a search term", cmd_fail("desc", "--search")
desc_cache = HOMEBREW_CACHE/"desc_cache.json"
refute_predicate desc_cache, :exist?, "Cached file should not exist"
cmd("desc", "--description", "testball")
assert_predicate desc_cache, :exist?, "Cached file should not exist"
end
end
require "integration_cmds_tests"
class IntegrationCommandTestDoctor < IntegrationCommandTests
def test_doctor
assert_match "This is an integration test",
cmd_fail("doctor", "check_integration_test")
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment