From fee7cde7669bb81140270d32e27e64794e4550da Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 9 Sep 2023 14:21:18 -0400 Subject: [PATCH] test: backfill coverage for the `#activate` method --- test/test_activate.rb | 127 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 test/test_activate.rb diff --git a/test/test_activate.rb b/test/test_activate.rb new file mode 100644 index 0000000..4be659a --- /dev/null +++ b/test/test_activate.rb @@ -0,0 +1,127 @@ +require File.expand_path('../helper', __FILE__) + +class TestActivate < TestCase + attr_reader :recipe + + def setup + super + + @save_env = %w[PATH CPATH LIBRARY_PATH].inject({}) do |env, var| + env.update(var => ENV[var]) + end + + FileUtils.rm_rf(["tmp", "ports"]) # remove any previous test files + + @recipe = MiniPortile.new("foo", "1.0.0").tap do |recipe| + recipe.logger = StringIO.new + end + end + + def teardown + FileUtils.rm_rf(["tmp", "ports"]) # remove any previous test files + + @save_env.each do |var, val| + ENV[var] = val + end + + super + end + + def test_PATH_env_var_when_bin_does_not_exist + refute(Dir.exist?(bin_path)) + refute(path_elements('PATH').include?(bin_path)) + + recipe.activate + + refute(path_elements('PATH').include?(bin_path)) + end + + def test_PATH_env_var_when_bin_exists + FileUtils.mkdir_p(bin_path) + refute(path_elements('PATH').include?(bin_path)) + + recipe.activate + + assert(path_elements('PATH').include?(bin_path)) + end + + def test_CPATH_env_var_when_include_does_not_exist + refute(Dir.exist?(include_path)) + refute(path_elements('CPATH').include?(include_path)) + + recipe.activate + + refute(path_elements('CPATH').include?(include_path)) + end + + def test_CPATH_env_var_when_include_exists + FileUtils.mkdir_p(include_path) + refute(path_elements('CPATH').include?(include_path)) + + recipe.activate + + assert(path_elements('CPATH').include?(include_path)) + end + + def test_LIBRARY_PATH_env_var_when_lib_does_not_exist + refute(Dir.exist?(lib_path)) + refute(path_elements('LIBRARY_PATH').include?(lib_path)) + + recipe.activate + + refute(path_elements('LIBRARY_PATH').include?(lib_path)) + end + + def test_LIBRARY_PATH_env_var_when_lib_exists + FileUtils.mkdir_p(lib_path) + refute(path_elements('LIBRARY_PATH').include?(lib_path)) + + recipe.activate + + assert(path_elements('LIBRARY_PATH').include?(lib_path)) + end + + def test_LDFLAGS_env_var_when_not_cross_compiling + FileUtils.mkdir_p(lib_path) + assert_equal(recipe.host, recipe.original_host) # assert on setup) + + refute(flag_elements('LDFLAGS').include?("-L#{lib_path}")) + + recipe.activate + + refute(flag_elements('LDFLAGS').include?("-L#{lib_path}")) + end + + def test_LDFLAGS_env_var_when_cross_compiling + recipe.host = recipe.original_host + "-x" # make them not-equal + FileUtils.mkdir_p(lib_path) + + refute(flag_elements('LDFLAGS').include?("-L#{lib_path}")) + + recipe.activate + + assert(flag_elements('LDFLAGS').include?("-L#{lib_path}")) + end + + private + + def path_elements(varname) + ENV.fetch(varname, "").split(File::PATH_SEPARATOR) + end + + def flag_elements(varname) + ENV.fetch(varname, "").split + end + + def bin_path + File.join(recipe.path, "bin") + end + + def include_path + File.join(recipe.path, "include") + end + + def lib_path + File.join(recipe.path, "lib") + end +end