Skip to content

Commit

Permalink
fix up the windows paths in tests and in LDFLAGS env var
Browse files Browse the repository at this point in the history
also

- use assert_includes for better failure messages
- extract public class methods MiniPortile.native_path and .posix_path
  • Loading branch information
flavorjones committed Sep 10, 2023
1 parent aa982b2 commit d7937bd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ yaml.files = [{
}]
recipes.push(yaml)
recipe_hooks["yaml"] = lambda do |recipe|
expected = "-L" + File.join(recipe.path, "lib")
expected = "-L" + MiniPortile.native_path(File.join(recipe.path, "lib"))
$LDFLAGS.split.include?(expected) or raise(<<~MSG)
assertion failed: LDFLAGS not updated correctly:
#{$LDFLAGS}
Expand Down
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
end

if set_ldflags && File.exist?(lib_path)
full_lib_path = File.expand_path(lib_path)
full_lib_path = native_path(lib_path)
flag = "-L#{full_lib_path}"

# rely on LDFLAGS when cross-compiling
Expand Down Expand Up @@ -278,21 +296,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
42 changes: 21 additions & 21 deletions test/test_activate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,78 +31,78 @@ def teardown

def test_PATH_env_var_when_bin_does_not_exist
refute(Dir.exist?(bin_path))
refute(path_elements('PATH').include?(bin_path))
refute_includes(path_elements('PATH'), bin_path)

recipe.activate

refute(path_elements('PATH').include?(bin_path))
refute_includes(path_elements('PATH'), bin_path)
end

def test_PATH_env_var_when_bin_exists
FileUtils.mkdir_p(bin_path)
refute(path_elements('PATH').include?(bin_path))
refute_includes(path_elements('PATH'), bin_path)

recipe.activate

assert(path_elements('PATH').include?(bin_path))
assert_includes(path_elements('PATH'), 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))
refute_includes(path_elements('CPATH'), include_path)

recipe.activate

refute(path_elements('CPATH').include?(include_path))
refute_includes(path_elements('CPATH'), include_path)
end

def test_CPATH_env_var_when_include_exists
FileUtils.mkdir_p(include_path)
refute(path_elements('CPATH').include?(include_path))
refute_includes(path_elements('CPATH'), include_path)

recipe.activate

assert(path_elements('CPATH').include?(include_path))
assert_includes(path_elements('CPATH'), 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))
refute_includes(path_elements('LIBRARY_PATH'), lib_path)

recipe.activate

refute(path_elements('LIBRARY_PATH').include?(lib_path))
refute_includes(path_elements('LIBRARY_PATH'), 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))
refute_includes(path_elements('LIBRARY_PATH'), lib_path)

recipe.activate

assert(path_elements('LIBRARY_PATH').include?(lib_path))
assert_includes(path_elements('LIBRARY_PATH'), 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}"))
refute_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")

recipe.activate

refute(flag_elements('LDFLAGS').include?("-L#{lib_path}"))
refute_includes(flag_elements('LDFLAGS'), "-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}"))
refute_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")

recipe.activate

assert(flag_elements('LDFLAGS').include?("-L#{lib_path}"))
assert_includes(flag_elements('LDFLAGS'), "-L#{lib_path}")
end

def test_LDFLAGS_global_when_not_set
Expand All @@ -120,7 +120,7 @@ def test_LDFLAGS_global_when_set

recipe.activate

assert($LDFLAGS.split.include?("-L#{lib_path}"))
assert_includes($LDFLAGS.split, "-L#{lib_path}")
end

def test_LDFLAGS_global_when_set_but_option_is_turned_off
Expand All @@ -130,7 +130,7 @@ def test_LDFLAGS_global_when_set_but_option_is_turned_off
recipe.set_ldflags = false
recipe.activate

refute($LDFLAGS.split.include?("-L#{lib_path}"))
refute_includes($LDFLAGS.split, "-L#{lib_path}")
end

private
Expand All @@ -144,14 +144,14 @@ def flag_elements(varname)
end

def bin_path
File.join(recipe.path, "bin")
MiniPortile.native_path(File.join(recipe.path, "bin"))
end

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

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

0 comments on commit d7937bd

Please sign in to comment.