Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Dec 18, 2024
1 parent b737eb2 commit 0e0eed6
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 33 deletions.
15 changes: 10 additions & 5 deletions lib/tebako/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ def hash
#{" " * 65}# If this parameter is not set, the application will start in the current directory of the host file system.
DESC

REF_DESCRIPTION = <<~DESC
"Referenced tebako run-time package; 'tebako-runtime' by default".
This option specifies the tebako runtime to be used by the application on Windows and if mode is 'application' only .
DESC

RGP_DESCRIPTION = <<~DESC
Activates removal a reference to GLIBC_PRIVATE version of libpthread from tebako package. This allows Linux Gnu packages to run against versions of
#{" " * 65}# libpthread that differ from the version used for packaging. For example, package created at Ubuntu 20 system can be used on Ubuntu 22. This option works on Gnu Linux with
#{" " * 65}# Gnu toolchain only (not for LLVM/clang). The feature is exeprimental, we may consider other approach in the future.
Current working directory for packaged application. This directory shall be specified relative to root.
#{" " * 65}# If this parameter is not set, the application will start in the current directory of the host file system.
DESC

desc "press", "Press tebako image"
Expand All @@ -94,15 +98,16 @@ def hash
desc: "Tebako memfs logging level, 'error' by default"
method_option :output, type: :string, aliases: "-o", required: false,
desc: "Tebako package file name, entry point base file name in the current folder by default"
method_option :"entry-point", type: :string, aliases: ["-e", "--entry"], required: false,
desc: "Ruby application entry point"
method_option :root, type: :string, aliases: "-r", required: false, desc: "Root folder of the Ruby application"
method_option :Ruby, type: :string, aliases: "-R", required: false,
enum: Tebako::RubyVersion::RUBY_VERSIONS.keys,
desc: "Tebako package Ruby version, #{Tebako::RubyVersion::DEFAULT_RUBY_VERSION} by default"
method_option :patchelf, aliases: "-P", type: :boolean, desc: RGP_DESCRIPTION
method_option :mode, type: :string, aliases: "-m", required: false, enum: %w[bundle both runtime application],
desc: "Tebako press mode, 'bundle' by default"
method_option :"entry-point", type: :string, aliases: ["-e", "--entry"], required: false,
desc: "Ruby application entry point"
method_option :ref, type: :string, aliases: "-u", required: false, desc: REF_DESCRIPTION

def press
validate_press_options
Expand Down
9 changes: 5 additions & 4 deletions lib/tebako/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ module Tebako
# Cli helpers
module CliHelpers
def do_press(options_manager)
puts options_manager.press_announce
scenario_manager = Tebako::ScenarioManager.new(options_manager.root, options_manager.fs_entrance)
puts options_manager.press_announce(scenario_manager.msys?)

if options_manager.mode == "both" || options_manager.mode == "runtime" || options_manager.mode == "bundle"
do_press_runtime(options_manager, scenario_manager)
end

if options_manager.mode == "both" || options_manager.mode == "application"
do_press_application(options_manager, scenario_manager)
end

if options_manager.mode == "both" || options_manager.mode == "runtime" || options_manager.mode == "bundle"
do_press_runtime(options_manager, scenario_manager)
end
true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tebako/codegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def deploy_crt_implib(opt, scm)
if scm.msys?
crt = <<~SUBST
Tebako::Packager.create_implib("#{opt.ruby_src_dir}", "#{opt.data_src_dir}",
"#{File.basename(opt.package)}", rv)
"#{opt.package}", rv)
SUBST
end
crt
Expand Down
20 changes: 16 additions & 4 deletions lib/tebako/options_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ def prefix
end
end

def press_announce
def press_announce(is_msys)
case mode
when "application"
press_announce_application
press_announce_application(is_msys)
when "both"
press_announce_both
when "bundle"
Expand All @@ -208,14 +208,22 @@ def press_announce
end
end

def press_announce_application
def press_announce_ref(is_msys)
if is_msys
" referencing runtime at '#{ref}'"

Check warning on line 213 in lib/tebako/options_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/options_manager.rb#L213

Added line #L213 was not covered by tests
else
""
end
end

def press_announce_application(is_msys)
<<~ANN
Running tebako press at #{prefix}
Mode: 'application'
Ruby version: '#{@ruby_ver}'
Project root: '#{root}'
Application entry point: '#{fs_entrance}'
Package file name: '#{package}.tebako'
Package file name: '#{package}.tebako'#{press_announce_ref(is_msys)}
Package working directory: '#{cwd_announce}'
ANN
end
Expand Down Expand Up @@ -265,6 +273,10 @@ def relative?(path)
Pathname.new(path).relative?
end

def ref
@ref ||= @options["ref"].nil? ? "tebako-runtime" : @options["ref"].gsub("\\", "/")

Check warning on line 277 in lib/tebako/options_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/options_manager.rb#L277

Added line #L277 was not covered by tests
end

def remove_glibc_private
@remove_glibc_private ||= if RUBY_PLATFORM.end_with?("linux") || RUBY_PLATFORM.end_with?("linux-gnu")
"-DREMOVE_GLIBC_PRIVATE=#{@options["patchelf"] ? "ON" : "OFF"}"
Expand Down
5 changes: 3 additions & 2 deletions lib/tebako/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ module Packager
class << self
# Create implib
def create_implib(src_dir, package_src_dir, app_name, ruby_ver)
create_def(src_dir, app_name)
a_name = File.basename(app_name, ".*")
create_def(src_dir, a_name)
puts " ... creating Windows import library"
params = ["dlltool", "-d", def_fname(src_dir, app_name), "-D", out_fname(app_name), "--output-lib",
params = ["dlltool", "-d", def_fname(src_dir, a_name), "-D", out_fname(a_name), "--output-lib",
lib_fname(package_src_dir, ruby_ver)]
BuildHelpers.run_with_capture(params)
end
Expand Down
14 changes: 12 additions & 2 deletions lib/tebako/packager_lite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def codegen
Tebako::Codegen.generate_package_descriptor(@opts, @scm)
end

def create_implib
rv = Tebako::RubyVersion.new(@opts.ruby_ver)
bname = if @opts.mode == "application"
@opts.ref

Check warning on line 53 in lib/tebako/packager_lite.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager_lite.rb#L53

Added line #L53 was not covered by tests
else # @opts.mode == "both"
@opts.package
end
Tebako::Packager.create_implib(@opts.ruby_src_dir, @opts.data_src_dir, bname, rv)
end

def create_package
deploy
FileUtils.rm_f(name)
Expand All @@ -56,8 +66,8 @@ def create_package

def deploy
Tebako::Packager.init(@opts.stash_dir, @opts.data_src_dir, @opts.data_pre_dir, @opts.data_bin_dir)
Tebako::Packager.deploy(@opts.data_src_dir, @opts.data_pre_dir, @opts.rv,
@opts.root, @scm.fs_entrance, @opts.cwd)
create_implib if @scm.msys?
Tebako::Packager.deploy(@opts.data_src_dir, @opts.data_pre_dir, @opts.rv, @opts.root, @scm.fs_entrance, @opts.cwd)
end

def name
Expand Down
2 changes: 1 addition & 1 deletion lib/tebako/scenario_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Tebako
class ScenarioManager
def initialize(fs_root, fs_entrance)
initialize_root(fs_root)
initialize_entry_point(fs_entrance)
initialize_entry_point(fs_entrance || "stub.rb")
end

attr_reader :fs_entry_point, :fs_mount_point, :fs_entrance
Expand Down
10 changes: 5 additions & 5 deletions spec/options_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
Package file name: '#{pckg}.tebako'
Package working directory: '#{options["cwd"]}'
ANN
expect(options_manager.press_announce).to eq(expected_announce)
expect(options_manager.press_announce(false)).to eq(expected_announce)
end
end

Expand All @@ -426,7 +426,7 @@
Loging level: '#{options["log-level"]}'
Package working directory: '<Host current directory>'
ANN
expect(options_manager.press_announce).to eq(expected_announce)
expect(options_manager.press_announce(false)).to eq(expected_announce)
end
end

Expand All @@ -451,7 +451,7 @@
Loging level: '#{options["log-level"]}'
Package working directory: '#{options["cwd"]}'
ANN
expect(options_manager.press_announce).to eq(expected_announce)
expect(options_manager.press_announce(false)).to eq(expected_announce)
end
end

Expand All @@ -476,7 +476,7 @@
Loging level: '#{options["log-level"]}'
Package working directory: '<Host current directory>'
ANN
expect(options_manager.press_announce).to eq(expected_announce)
expect(options_manager.press_announce(false)).to eq(expected_announce)
end
end

Expand All @@ -498,7 +498,7 @@
Runtime file name: '#{pckg}'
Loging level: '#{options["log-level"]}'
ANN
expect(options_manager.press_announce).to eq(expected_announce)
expect(options_manager.press_announce(false)).to eq(expected_announce)
end
end
end
Expand Down
37 changes: 30 additions & 7 deletions spec/packager_lite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
RSpec.describe Tebako::PackagerLite do
let(:options_manager) do
double("OptionsManager", stash_dir: "/tmp/stash", data_src_dir: "/tmp/src", data_pre_dir: "/tmp/pre",
data_bin_dir: "/tmp/bin", deps_bin_dir: "/tmp/deps_bin",
package: "test_package", rv: "3.0.0", root: "/", cwd: "/app")
data_bin_dir: "/tmp/bin", deps_bin_dir: "/tmp/deps_bin", mode: "both",
package: "test_package", rv: "3.2.5", ruby_ver: "3.2.5", root: "/", cwd: "/app",
ruby_src_dir: "/tmp/ruby_src")
end
let(:scenario_manager) { double("ScenarioManager", fs_entrance: "/entry") }

Expand Down Expand Up @@ -68,6 +69,9 @@
it "calls Packager methods to create the package" do
packager_lite = described_class.new(options_manager, scenario_manager)
allow(packager_lite).to receive(:codegen).and_return("codegen_result")
allow(scenario_manager).to receive(:msys?).and_return(true)
allow(Tebako::Packager).to receive(:create_def)
allow(Tebako::Packager).to receive(:create_implib)
packager_lite.create_package
expect(FileUtils).to have_received(:rm_f).with("test_package.tebako")
expect(Tebako::Packager).to have_received(:mkdwarfs).with("/tmp/deps_bin", "test_package.tebako", "/tmp/src",
Expand All @@ -76,11 +80,30 @@
end

describe "#deploy" do
it "calls Packager methods to deploy the package" do
packager_lite = described_class.new(options_manager, scenario_manager)
packager_lite.deploy
expect(Tebako::Packager).to have_received(:init).with("/tmp/stash", "/tmp/src", "/tmp/pre", "/tmp/bin")
expect(Tebako::Packager).to have_received(:deploy).with("/tmp/src", "/tmp/pre", "3.0.0", "/", "/entry", "/app")
context "when msys? is true" do
it "calls create_implib and Packager methods to deploy the package" do
allow(scenario_manager).to receive(:msys?).and_return(true)
packager_lite = described_class.new(options_manager, scenario_manager)
allow(packager_lite).to receive(:create_implib)
allow(scenario_manager).to receive(:msys?).and_return(true)
packager_lite.deploy
expect(packager_lite).to have_received(:create_implib)
expect(Tebako::Packager).to have_received(:init).with("/tmp/stash", "/tmp/src", "/tmp/pre", "/tmp/bin")
expect(Tebako::Packager).to have_received(:deploy).with("/tmp/src", "/tmp/pre", "3.2.5", "/", "/entry", "/app")
end
end

context "when msys? is false" do
it "does not call create_implib but calls Packager methods to deploy the package" do
allow(scenario_manager).to receive(:msys?).and_return(false)
packager_lite = described_class.new(options_manager, scenario_manager)
allow(packager_lite).to receive(:create_implib)
allow(scenario_manager).to receive(:msys?).and_return(false)
packager_lite.deploy
expect(packager_lite).not_to have_received(:create_implib)
expect(Tebako::Packager).to have_received(:init).with("/tmp/stash", "/tmp/src", "/tmp/pre", "/tmp/bin")
expect(Tebako::Packager).to have_received(:deploy).with("/tmp/src", "/tmp/pre", "3.2.5", "/", "/entry", "/app")
end
end
end

Expand Down
57 changes: 57 additions & 0 deletions spec/stripper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,63 @@
described_class.send(:strip_bs, src_dir)
end
end

describe ".strip_fi" do
let(:scm) { double("scm", exe_suffix: ".exe") }
let(:src_dir) { "/path/to/src" }
let(:files) do
Tebako::Stripper::BIN_FILES.flat_map { |f|
["#{src_dir}/bin/#{f}", "#{src_dir}/bin/#{f}.cmd",
"#{src_dir}/bin/#{f}.bat"]
} + ["#{src_dir}/bin/ruby.exe", "#{src_dir}/bin/rubyw.exe"]
end

it "removes the correct files" do
expect(FileUtils).to receive(:rm).with(files, force: true)
described_class.send(:strip_fi, scm, src_dir)
end
end

describe ".strip_li" do
let(:scm) { double("scm", msys?: false, macos?: false) }
let(:src_dir) { "/path/to/src" }
let(:file) { "/path/to/src/file.so" }

before do
allow(Find).to receive(:find).and_yield(file)
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:extname).and_return(".so")
end

it "strips the correct files" do
expect(described_class).to receive(:strip_file).with(file)
described_class.send(:strip_li, scm, src_dir)
end

it "removes files with DELETE_EXTENSIONS" do
allow(File).to receive(:extname).and_return(".o")
expect(FileUtils).to receive(:rm).with(file)
described_class.send(:strip_li, scm, src_dir)
end
end

describe ".strip_extensions" do
let(:scm) { double("scm", msys?: false, macos?: false) }

it "returns the correct extensions for non-msys and non-macos" do
expect(described_class.send(:strip_extensions, scm)).to eq(["so"])
end

it "returns the correct extensions for msys" do
allow(scm).to receive(:msys?).and_return(true)
expect(described_class.send(:strip_extensions, scm)).to eq(%w[so dll])
end

it "returns the correct extensions for macos" do
allow(scm).to receive(:macos?).and_return(true)
expect(described_class.send(:strip_extensions, scm)).to eq(%w[so dylib bundle])
end
end
end

# rubocop:enable Metrics/BlockLength
2 changes: 1 addition & 1 deletion tests/scripts/functional-tests-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ test_tebako_press_19() {
}

# ......................................................................
# 20. 20. Net/http Ruby script
# 20. Net/http Ruby script
test_tebako_press_20() {
echo "==> Net/http Ruby script"
press_runner "${DIR_TESTS}/test-20" "tebako-test-run.rb" "test-20-package"
Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ test_tebako_press_19() {
}

# ......................................................................
# 20. 20. Net/http Ruby script
# 20. Net/http Ruby script
test_tebako_press_20() {
echo "==> Net/http Ruby script"
press_runner "${DIR_TESTS}/test-20" "tebako-test-run.rb" "test-20-package"
Expand Down

0 comments on commit 0e0eed6

Please sign in to comment.