Skip to content

Commit

Permalink
test: backfill coverage for MiniPortile#activate
Browse files Browse the repository at this point in the history
also:

- extract public methods `native_path` and `posix_path`
- fix LDFLAGS path on windows to use native path separators
  • Loading branch information
flavorjones committed Sep 13, 2023
1 parent 3255d3f commit 6cf86a3
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/mini_portile2/mini_portile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ def self.target_cpu
RbConfig::CONFIG['target_cpu']
end

def self.native_path(path)
path = File.expand_path(path)
if File::ALT_SEPARATOR
path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
else
path
end
end

def self.posix_path(path)
path = File.expand_path(path)
if File::ALT_SEPARATOR
"/" + path.tr(File::ALT_SEPARATOR, File::SEPARATOR).tr(":", File::SEPARATOR)
else
path
end
end

def initialize(name, version, **kwargs)
@name = name
@version = version
Expand Down Expand Up @@ -240,7 +258,7 @@ def activate

# rely on LDFLAGS when cross-compiling
if File.exist?(lib_path) && (@host != @original_host)
full_path = File.expand_path(lib_path)
full_path = native_path(lib_path)

old_value = ENV.fetch("LDFLAGS", "")

Expand All @@ -265,21 +283,11 @@ def make_cmd
private

def native_path(path)
path = File.expand_path(path)
if File::ALT_SEPARATOR
path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
else
path
end
MiniPortile.native_path(path)
end

def posix_path(path)
path = File.expand_path(path)
if File::ALT_SEPARATOR
"/" + path.tr(File::ALT_SEPARATOR, File::SEPARATOR).tr(":", File::SEPARATOR)
else
path
end
MiniPortile.posix_path(path)
end

def tmp_path
Expand Down
139 changes: 139 additions & 0 deletions test/test_activate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
require File.expand_path('../helper', __FILE__)

class TestActivate < TestCase
attr_reader :recipe

def setup
super

@save_env = %w[PATH CPATH LIBRARY_PATH LDFLAGS].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
ENV["PATH"] = "foo"
refute(Dir.exist?(bin_path))
refute_includes(path_elements('PATH'), bin_path)

recipe.activate

refute_includes(path_elements('PATH'), bin_path)
end

def test_PATH_env_var_when_bin_exists
ENV["PATH"] = "foo"
FileUtils.mkdir_p(bin_path)
refute_includes(path_elements('PATH'), bin_path)

recipe.activate

assert_includes(path_elements('PATH'), bin_path)
assert_equal(path_elements('PATH').first, bin_path)
end

def test_CPATH_env_var_when_include_does_not_exist
ENV["CPATH"] = "foo"
refute(Dir.exist?(include_path))
refute_includes(path_elements('CPATH'), include_path)

recipe.activate

refute_includes(path_elements('CPATH'), include_path)
end

def test_CPATH_env_var_when_include_exists
ENV["CPATH"] = "foo"
FileUtils.mkdir_p(include_path)
refute_includes(path_elements('CPATH'), include_path)

recipe.activate

assert_includes(path_elements('CPATH'), include_path)
assert_equal(path_elements('CPATH').first, include_path)
end

def test_LIBRARY_PATH_env_var_when_lib_does_not_exist
ENV["LIBRARY_PATH"] = "foo"
refute(Dir.exist?(lib_path))
refute_includes(path_elements('LIBRARY_PATH'), lib_path)

recipe.activate

refute_includes(path_elements('LIBRARY_PATH'), lib_path)
end

def test_LIBRARY_PATH_env_var_when_lib_exists
ENV["LIBRARY_PATH"] = "foo"
FileUtils.mkdir_p(lib_path)
refute_includes(path_elements('LIBRARY_PATH'), lib_path)

recipe.activate

assert_includes(path_elements('LIBRARY_PATH'), lib_path)
assert_equal(path_elements('LIBRARY_PATH').first, lib_path)
end

def test_LDFLAGS_env_var_when_not_cross_compiling
ENV["LDFLAGS"] = "-lfoo"
FileUtils.mkdir_p(lib_path)
assert_equal(recipe.host, recipe.original_host) # assert on setup)

refute_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")

recipe.activate

refute_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")
end

def test_LDFLAGS_env_var_when_cross_compiling
ENV["LDFLAGS"] = "-lfoo"
recipe.host = recipe.original_host + "-x" # make them not-equal
FileUtils.mkdir_p(lib_path)

refute_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")

recipe.activate

assert_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")
assert_equal(flag_elements('LDFLAGS').first, "-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
MiniPortile.native_path(File.join(recipe.path, "bin"))
end

def include_path
MiniPortile.native_path(File.join(recipe.path, "include"))
end

def lib_path
MiniPortile.native_path(File.join(recipe.path, "lib"))
end
end

0 comments on commit 6cf86a3

Please sign in to comment.