Skip to content

Commit

Permalink
Fix execution of bin stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso committed Jan 7, 2025
1 parent 0a85584 commit ce77917
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 177 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ jobs:
gem update --system 3.3.22 &&
${{ matrix.bootstrap }}
gem install --no-document ./gems/tiny_tds-$(cat VERSION)-${{ matrix.platform }}.gem &&
ruby -e \"require 'tiny_tds'; puts TinyTds::Gem.root_path\"
ruby -e \"require 'tiny_tds'; puts TinyTds::Gem.root_path\ &&
tsql-ttds -C && \
defncopy-ttds"
"
test-linux:
Expand Down
10 changes: 10 additions & 0 deletions ext/tiny_tds/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def install
recipe.cook_and_activate
end

# when cross-compiling, we are in a specific directory for the current Ruby version
# the final gem is put together in the "stage" folder
# which is created after extconf has been compiled for the first time ...
["defncopy", "tsql"].each do |binary_name|
next if File.file?("../../../../exe/#{binary_name}")

puts "Copying #{binary_name} into gem ..."
FileUtils.cp("#{freetds_recipe.path}/bin/#{binary_name}", "../../../../exe")
end

ENV["LDFLAGS"] = "-Wl,-rpath -Wl,#{openssl_recipe.path}/lib"
dir_config('freetds', "#{freetds_recipe.path}/include", "#{freetds_recipe.path}/lib")

Expand Down
17 changes: 8 additions & 9 deletions lib/tiny_tds/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(name)
end

def run(*args)
with_ports_paths do
with_exe_path do
return nil unless path
Kernel.system Shellwords.join(args.unshift(path))
$CHILD_STATUS.to_i
Expand All @@ -43,15 +43,15 @@ def info
private

def search_paths
ENV['PATH'].split File::PATH_SEPARATOR
ENV['PATH'].split(File::PATH_SEPARATOR) + [Gem.exe_path]
end

def with_ports_paths
def with_exe_path
old_path = ENV['PATH']

begin
ENV['PATH'] = [
Gem.ports_bin_paths,
Gem.exe_path,
old_path
].flatten.join File::PATH_SEPARATOR

Expand All @@ -66,12 +66,11 @@ def find_bin
end

def find_exe
Gem.ports_bin_paths.each do |bin|
@exts.each do |ext|
f = File.join bin, "#{name}#{ext}"
return f if File.exist?(f)
end
@exts.each do |ext|
f = File.join Gem.exe_path, "#{name}#{ext}"
return f if File.exist?(f)
end

nil
end

Expand Down
16 changes: 2 additions & 14 deletions lib/tiny_tds/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,8 @@ def root_path
File.expand_path '../../..', __FILE__
end

def ports_root_path
File.join(root_path,'ports')
end

def ports_bin_paths
Dir.glob(File.join(ports_root_path,ports_host,'**','bin'))
end

def ports_lib_paths
Dir.glob(File.join(ports_root_path,ports_host,'**','lib'))
end

def ports_host
RbConfig::CONFIG["arch"]
def exe_path
File.join(root_path, "exe")
end
end
end
Expand Down
153 changes: 0 additions & 153 deletions test/gem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ class GemTest < Minitest::Spec
gem_root ||= File.expand_path '../..', __FILE__

describe TinyTds::Gem do

# We're going to muck with some system globals so lets make sure
# they get set back later
original_platform = RbConfig::CONFIG['arch']
original_pwd = Dir.pwd

after do
RbConfig::CONFIG['arch'] = original_platform
Dir.chdir original_pwd
end

describe '#root_path' do
let(:root_path) { TinyTds::Gem.root_path }

Expand All @@ -30,147 +19,5 @@ class GemTest < Minitest::Spec
_(root_path).must_equal gem_root
end
end

describe '#ports_root_path' do
let(:ports_root_path) { TinyTds::Gem.ports_root_path }

it 'should be the ports path' do
_(ports_root_path).must_equal File.join(gem_root,'ports')
end

it 'should be the ports path no matter the cwd' do
Dir.chdir '/'

_(ports_root_path).must_equal File.join(gem_root,'ports')
end
end

describe '#ports_bin_paths' do
let(:ports_bin_paths) { TinyTds::Gem.ports_bin_paths }

describe 'when the ports directories exist' do
let(:fake_bin_paths) do
ports_host_root = File.join(gem_root, 'ports', 'fake-host-with-dirs')
[
File.join('a','bin'),
File.join('a','inner','bin'),
File.join('b','bin')
].map do |p|
File.join(ports_host_root, p)
end
end

before do
RbConfig::CONFIG['arch'] = 'fake-host-with-dirs'
fake_bin_paths.each do |path|
FileUtils.mkdir_p(path)
end
end

after do
FileUtils.remove_entry_secure(
File.join(gem_root, 'ports', 'fake-host-with-dirs'), true
)
end

it 'should return all the bin directories' do
_(ports_bin_paths.sort).must_equal fake_bin_paths.sort
end

it 'should return all the bin directories regardless of cwd' do
Dir.chdir '/'
_(ports_bin_paths.sort).must_equal fake_bin_paths.sort
end
end

describe 'when the ports directories are missing' do
before do
RbConfig::CONFIG['arch'] = 'fake-host-without-dirs'
end

it 'should return no directories' do
_(ports_bin_paths).must_be_empty
end

it 'should return no directories regardless of cwd' do
Dir.chdir '/'
_(ports_bin_paths).must_be_empty
end
end
end

describe '#ports_lib_paths' do
let(:ports_lib_paths) { TinyTds::Gem.ports_lib_paths }

describe 'when the ports directories exist' do
let(:fake_lib_paths) do
ports_host_root = File.join(gem_root, 'ports', 'fake-host-with-dirs')
[
File.join('a','lib'),
File.join('a','inner','lib'),
File.join('b','lib')
].map do |p|
File.join(ports_host_root, p)
end
end

before do
RbConfig::CONFIG['arch'] = 'fake-host-with-dirs'
fake_lib_paths.each do |path|
FileUtils.mkdir_p(path)
end
end

after do
FileUtils.remove_entry_secure(
File.join(gem_root, 'ports', 'fake-host-with-dirs'), true
)
end

it 'should return all the lib directories' do
_(ports_lib_paths.sort).must_equal fake_lib_paths.sort
end

it 'should return all the lib directories regardless of cwd' do
Dir.chdir '/'
_(ports_lib_paths.sort).must_equal fake_lib_paths.sort
end
end

describe 'when the ports directories are missing' do
before do
RbConfig::CONFIG['arch'] = 'fake-host-without-dirs'
end


it 'should return no directories' do
_(ports_lib_paths).must_be_empty
end

it 'should return no directories regardless of cwd' do
Dir.chdir '/'
_(ports_lib_paths).must_be_empty
end
end
end

describe '#ports_host' do
{
'x64-mingw-ucrt' => 'x64-mingw-ucrt',
'x64-mingw32' => 'x64-mingw32',
'x86_64-linux' => 'x86_64-linux',
}.each do |host,expected|
describe "on a #{host} architecture" do
before do
RbConfig::CONFIG['arch'] = host
end

it "should return a #{expected} ports host" do
_(TinyTds::Gem.ports_host).must_equal expected
end
end
end
end
end
end

0 comments on commit ce77917

Please sign in to comment.