Skip to content

Commit 3992255

Browse files
committed
Harmonize loading resources from ports directory
1 parent 77415b7 commit 3992255

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

lib/tiny_tds.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
module TinyTds
1111
# Is this file part of a fat binary gem with bundled freetds?
1212
# This path must be enabled by add_dll_directory on Windows.
13-
gplat = ::Gem::Platform.local
14-
FREETDS_LIB_PATH = Dir[File.expand_path("../ports/#{gplat.cpu}-#{gplat.os}*/{bin,lib}", __dir__)].first
13+
FREETDS_LIB_PATH = TinyTds::Gem.ports_bin_and_lib_paths.first
1514

1615
add_dll_path = proc do |path, &block|
1716
if RUBY_PLATFORM =~ /(mswin|mingw)/i && path

lib/tiny_tds/bin.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def with_ports_paths
5050

5151
begin
5252
ENV["PATH"] = [
53-
Gem.ports_bin_paths,
53+
Gem.ports_bin_and_lib_paths,
5454
old_path
5555
].flatten.join File::PATH_SEPARATOR
5656

@@ -65,7 +65,7 @@ def find_bin
6565
end
6666

6767
def find_exe
68-
Gem.ports_bin_paths.each do |bin|
68+
Gem.ports_bin_and_lib_paths.each do |bin|
6969
@exts.each do |ext|
7070
f = File.join bin, "#{name}#{ext}"
7171
return f if File.exist?(f)

lib/tiny_tds/gem.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require "rbconfig"
2-
31
module TinyTds
42
module Gem
53
class << self
@@ -11,12 +9,14 @@ def ports_root_path
119
File.join(root_path, "ports")
1210
end
1311

14-
def ports_bin_paths
15-
Dir.glob(File.join(ports_root_path, "**", "bin"))
12+
def ports_bin_and_lib_paths
13+
Dir.glob(File.join(ports_root_path, "#{gem_platform.cpu}-#{gem_platform.os}*", "{bin,lib}"))
1614
end
1715

18-
def ports_lib_paths
19-
Dir.glob(File.join(ports_root_path, "**", "lib"))
16+
private
17+
18+
def gem_platform
19+
::Gem::Platform.local
2020
end
2121
end
2222
end

test/gem_test.rb

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ class GemTest < Minitest::Spec
77
describe TinyTds::Gem do
88
# We're going to muck with some system globals so lets make sure
99
# they get set back later
10-
original_platform = RbConfig::CONFIG["arch"]
1110
original_pwd = Dir.pwd
1211

1312
after do
14-
RbConfig::CONFIG["arch"] = original_platform
1513
Dir.chdir original_pwd
1614
end
1715

@@ -43,56 +41,53 @@ class GemTest < Minitest::Spec
4341
end
4442
end
4543

46-
describe "#ports_bin_paths" do
47-
let(:ports_bin_paths) { TinyTds::Gem.ports_bin_paths }
44+
describe "#ports_bin_and_lib_paths" do
45+
let(:ports_bin_and_lib_paths) { TinyTds::Gem.ports_bin_and_lib_paths }
4846

4947
describe "when the ports directories exist" do
50-
let(:fake_bin_paths) do
51-
ports_host_root = File.join(gem_root, "ports", "fake-host-with-dirs")
52-
[
53-
File.join("a", "bin"),
54-
File.join("a", "inner", "bin"),
55-
File.join("b", "bin")
56-
].map do |p|
48+
let(:fake_bin_and_lib_path) do
49+
ports_host_root = File.join(gem_root, "ports", "x86_64-unknown")
50+
["bin", "lib"].map do |p|
5751
File.join(ports_host_root, p)
5852
end
5953
end
6054

6155
before do
62-
RbConfig::CONFIG["arch"] = "fake-host-with-dirs"
63-
fake_bin_paths.each do |path|
56+
fake_bin_and_lib_path.each do |path|
6457
FileUtils.mkdir_p(path)
6558
end
6659
end
6760

6861
after do
6962
FileUtils.remove_entry_secure(
70-
File.join(gem_root, "ports", "fake-host-with-dirs"), true
63+
File.join(gem_root, "ports", "x86_64-unknown"), true
7164
)
7265
end
7366

7467
it "should return all the bin directories" do
75-
_(ports_bin_paths.sort).must_equal fake_bin_paths.sort
76-
end
68+
fake_platform = Gem::Platform.new("x86_64-unknown")
69+
70+
Gem::Platform.stub(:local, fake_platform) do
71+
_(ports_bin_and_lib_paths.sort).must_equal fake_bin_and_lib_path.sort
7772

78-
it "should return all the bin directories regardless of cwd" do
79-
Dir.chdir "/"
80-
_(ports_bin_paths.sort).must_equal fake_bin_paths.sort
73+
# should return the same regardless of path
74+
Dir.chdir "/"
75+
_(ports_bin_and_lib_paths.sort).must_equal fake_bin_and_lib_path.sort
76+
end
8177
end
8278
end
8379

8480
describe "when the ports directories are missing" do
85-
before do
86-
RbConfig::CONFIG["arch"] = "fake-host-without-dirs"
87-
end
88-
8981
it "should return no directories" do
90-
_(ports_bin_paths).must_be_empty
91-
end
82+
fake_platform = Gem::Platform.new("x86_64-unknown")
9283

93-
it "should return no directories regardless of cwd" do
94-
Dir.chdir "/"
95-
_(ports_bin_paths).must_be_empty
84+
Gem::Platform.stub(:local, fake_platform) do
85+
_(ports_bin_and_lib_paths).must_be_empty
86+
87+
# should be empty regardless of path
88+
Dir.chdir "/"
89+
_(ports_bin_and_lib_paths).must_be_empty
90+
end
9691
end
9792
end
9893
end

0 commit comments

Comments
 (0)