|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Test script to verify the installed ffi-libarchive-binary gem works correctly. |
| 5 | +# This script runs AFTER installing the gem via `gem install`, |
| 6 | +# NOT from the source directory. |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# SPEC_EXAMPLES_PATH=/path/to/spec/examples ruby test_installed_gem.rb |
| 10 | + |
| 11 | +require "tempfile" |
| 12 | +require "fileutils" |
| 13 | + |
| 14 | +def windows? |
| 15 | + RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/ |
| 16 | +end |
| 17 | + |
| 18 | +# Path to spec/examples directory |
| 19 | +# Can be overridden via SPEC_EXAMPLES_PATH environment variable |
| 20 | +SPEC_EXAMPLES = ENV.fetch("SPEC_EXAMPLES_PATH") do |
| 21 | + # Default: relative to this script (works when script is in .github/scripts/) |
| 22 | + File.expand_path("../../spec/examples", __dir__) |
| 23 | +end |
| 24 | + |
| 25 | +# Test 1: Verify the gem can be loaded |
| 26 | +puts "Test 1: Loading ffi-libarchive-binary..." |
| 27 | +begin |
| 28 | + require "ffi-libarchive-binary" |
| 29 | + puts " PASS: ffi-libarchive-binary loaded successfully" |
| 30 | +rescue LoadError => e |
| 31 | + puts " FAIL: Could not load ffi-libarchive-binary: #{e.message}" |
| 32 | + exit 1 |
| 33 | +end |
| 34 | + |
| 35 | +# Test 2: Verify libarchive library is accessible |
| 36 | +puts "Test 2: Checking library path..." |
| 37 | +lib_path = LibarchiveBinary.lib_path |
| 38 | +if File.exist?(lib_path) |
| 39 | + puts " PASS: Library found at #{lib_path}" |
| 40 | +else |
| 41 | + puts " FAIL: Library not found at #{lib_path}" |
| 42 | + puts " Contents of lib directory:" |
| 43 | + lib_dir = File.dirname(lib_path) |
| 44 | + Dir.glob("#{lib_dir}/*").each { |f| puts " - #{f}" } if Dir.exist?(lib_dir) |
| 45 | + exit 1 |
| 46 | +end |
| 47 | + |
| 48 | +# Test 3: Verify Archive module is functional |
| 49 | +puts "Test 3: Testing Archive module..." |
| 50 | +begin |
| 51 | + puts " Archive::EXTRACT_PERM = #{Archive::EXTRACT_PERM}" |
| 52 | + puts " PASS: Archive module is functional" |
| 53 | +rescue => e |
| 54 | + puts " FAIL: Archive module error: #{e.message}" |
| 55 | + exit 1 |
| 56 | +end |
| 57 | + |
| 58 | +# Test 4: Test archive extraction (7z self-extracting) |
| 59 | +puts "Test 4: Testing archive extraction..." |
| 60 | +test_archive = File.join(SPEC_EXAMPLES, "fonts_7z.exe") |
| 61 | +if File.exist?(test_archive) |
| 62 | + Dir.mktmpdir do |target| |
| 63 | + begin |
| 64 | + Dir.chdir(target) do |
| 65 | + flags = Archive::EXTRACT_PERM |
| 66 | + reader = Archive::Reader.open_filename(test_archive) |
| 67 | + |
| 68 | + reader.each_entry do |entry| |
| 69 | + reader.extract(entry, flags.to_i) |
| 70 | + end |
| 71 | + |
| 72 | + reader.close |
| 73 | + end |
| 74 | + |
| 75 | + extracted_file = File.join(target, "Fonts", "Marlett.ttf") |
| 76 | + if File.exist?(extracted_file) |
| 77 | + puts " PASS: Archive extracted successfully, found #{extracted_file}" |
| 78 | + else |
| 79 | + puts " FAIL: Extraction did not create expected file #{extracted_file}" |
| 80 | + puts " Files created:" |
| 81 | + Dir.glob("#{target}/**/*").each { |f| puts " - #{f}" } |
| 82 | + exit 1 |
| 83 | + end |
| 84 | + rescue => e |
| 85 | + puts " FAIL: Archive extraction error: #{e.message}" |
| 86 | + puts e.backtrace.first(5).join("\n") |
| 87 | + exit 1 |
| 88 | + end |
| 89 | + end |
| 90 | +else |
| 91 | + puts " SKIP: Test archive not found at #{test_archive}" |
| 92 | +end |
| 93 | + |
| 94 | +# Test 5: Test XAR/pkg extraction (non-Windows only) |
| 95 | +unless windows? |
| 96 | + puts "Test 5: Testing XAR/pkg extraction..." |
| 97 | + pkg_archive = File.join(SPEC_EXAMPLES, "archive.pkg") |
| 98 | + if File.exist?(pkg_archive) |
| 99 | + Dir.mktmpdir do |target| |
| 100 | + begin |
| 101 | + Dir.chdir(target) do |
| 102 | + flags = Archive::EXTRACT_PERM |
| 103 | + Archive.read_open_filename(pkg_archive) do |ar| |
| 104 | + ar.each_entry do |entry| |
| 105 | + ar.extract(entry, flags.to_i) |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + payload = File.join(target, "Payload") |
| 111 | + if File.exist?(payload) |
| 112 | + puts " PASS: PKG extracted successfully, found #{payload}" |
| 113 | + else |
| 114 | + puts " FAIL: PKG extraction did not create expected Payload file" |
| 115 | + exit 1 |
| 116 | + end |
| 117 | + rescue => e |
| 118 | + puts " FAIL: PKG extraction error: #{e.message}" |
| 119 | + puts e.backtrace.first(5).join("\n") |
| 120 | + exit 1 |
| 121 | + end |
| 122 | + end |
| 123 | + else |
| 124 | + puts " SKIP: PKG archive not found at #{pkg_archive}" |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +puts "" |
| 129 | +puts "All tests passed!" |
| 130 | +exit 0 |
0 commit comments