Skip to content

Commit

Permalink
Snapshot 4
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Oct 26, 2024
1 parent 3fed699 commit 443f5ee
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 224 deletions.
110 changes: 110 additions & 0 deletions lib/tebako/cache_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require "fileutils"
require_relative "version"

# Tebako - an executable packager
module Tebako
# Cache management
class CacheManager
def initialize(deps, src_dir, out_dir)
@deps = deps
@src_dir = src_dir
@out_dir = out_dir
end

def clean_cache
puts "Cleaning tebako packaging environment"
# Using File.join(deps, "") to ensure that the slashes are appropriate
FileUtils.rm_rf([File.join(@deps, ""), File.join(@out_dir, "")], secure: true)
end

def clean_output
puts "Cleaning CMake cache and Ruby build files"

nmr = "src/_ruby_*"
nms = "stash_*"
FileUtils.rm_rf(Dir.glob(File.join(@deps, nmr)), secure: true)
FileUtils.rm_rf(Dir.glob(File.join(@deps, nms)), secure: true)

# Using File.join(output_folder, "") to ensure that the slashes are appropriate
FileUtils.rm_rf(File.join(@out_dir, ""), secure: true)
end

def ensure_version_file
version_file_path = File.join(@deps, E_VERSION_FILE)

Check warning on line 60 in lib/tebako/cache_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cache_manager.rb#L60

Added line #L60 was not covered by tests

begin
File.write(version_file_path, version_key)

Check warning on line 63 in lib/tebako/cache_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cache_manager.rb#L63

Added line #L63 was not covered by tests
# puts "Set version information for tebako packaging environment to #{Tebako::VERSION}"
rescue StandardError => e
puts "An error occurred while creating or updating #{E_VERSION_FILE}: #{e.message}"

Check warning on line 66 in lib/tebako/cache_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cache_manager.rb#L65-L66

Added lines #L65 - L66 were not covered by tests
end
end

def version_key
@version_key ||= "#{Tebako::VERSION} at #{@src_dir}"
end

def version_cache
version_file_path = File.join(@deps, E_VERSION_FILE)
file_version = File.open(version_file_path, &:readline).strip
file_version.match(/(?<version>.+) at (?<source>.+)/)

Check warning on line 77 in lib/tebako/cache_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cache_manager.rb#L75-L77

Added lines #L75 - L77 were not covered by tests
end

def version_cache_check
match_data = version_cache
return version_unknown unless match_data

if match_data[:version] != Tebako::VERSION
version_mismatch(match_data[:version])
elsif match_data[:source] != @src_dir
version_source_mismatch(match_data[:source])
end
rescue StandardError
version_unknown
end

def version_mismatch(cached_version)
puts "Tebako cache was created by a gem version #{cached_version} " \
"and cannot be used for gem version #{Tebako::VERSION}"
clean_cache
end

def version_source_mismatch(cached_source)
puts "CMake cache was created for a different source directory '#{cached_source}' " \
"and cannot be used for '#{@src_dir}'"
clean_output
end

def version_unknown
puts "CMake cache version was not recognized, cleaning up"
clean_cache
end
end
end
11 changes: 7 additions & 4 deletions lib/tebako/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require "thor"
require "yaml"

require_relative "cache_manager"
require_relative "cli_helpers"
require_relative "cli_rubies"
require_relative "error"
Expand Down Expand Up @@ -100,11 +101,12 @@ def hash
method_option :patchelf, aliases: "-P", type: :boolean,
desc: RGP_DESCRIPTION
def press
version_cache_check unless options[:devmode]
cm = Tebako::CacheManager.new(deps, source, output_folder)
cm.version_cache_check unless options[:devmode]

puts press_announce
do_press
ensure_version_file
cm.ensure_version_file
rescue Tebako::Error => e
puts "Tebako script failed: #{e.message} [#{e.error_code}]"
exit e.error_code
Expand All @@ -115,11 +117,12 @@ def press
enum: Tebako::CliRubies::RUBY_VERSIONS.keys,
desc: "Tebako package Ruby version, #{Tebako::CliRubies::DEFAULT_RUBY_VERSION} by default."
def setup
version_cache_check unless options[:devmode]
cm = Tebako::CacheManager.new(deps, source, output_folder)
cm.version_cache_check unless options[:devmode]

puts "Setting up tebako packaging environment"
do_setup
ensure_version_file
cm.ensure_version_file
rescue Tebako::Error => e
puts "Tebako script failed: #{e.message} [#{e.error_code}]"
exit e.error_code
Expand Down
70 changes: 0 additions & 70 deletions lib/tebako/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,6 @@ def cfg_options
"#{remove_glibc_private} -DTEBAKO_VERSION:STRING=\"#{v_parts[0]}.#{v_parts[1]}.#{v_parts[2]}\""
end

def clean_cache
puts "Cleaning tebako packaging environment"
# Using File.join(deps, "") to ensure that the slashes are appropriate
FileUtils.rm_rf([File.join(deps, ""), File.join(output_folder, "")], secure: true)
end

def clean_output
puts "Cleaning CMake cache and Ruby build files"

nmr = "src/_ruby_*"
nms = "stash_*"
FileUtils.rm_rf(Dir.glob(File.join(deps, nmr)), secure: true)
FileUtils.rm_rf(Dir.glob(File.join(deps, nms)), secure: true)

# Using File.join(output_folder, "") to ensure that the slashes are appropriate
FileUtils.rm_rf(File.join(output_folder, ""), secure: true)
end

def deps
@deps ||= File.join(prefix, "deps")
end
Expand All @@ -105,17 +87,6 @@ def do_setup
true
end

def ensure_version_file
version_file_path = File.join(deps, E_VERSION_FILE)

begin
File.write(version_file_path, version_key)
# puts "Set version information for tebako packaging environment to #{Tebako::VERSION}"
rescue StandardError => e
puts "An error occurred while creating or updating #{E_VERSION_FILE}: #{e.message}"
end
end

def fs_current
fs_current = Dir.pwd
if RUBY_PLATFORM =~ /msys|mingw|cygwin/
Expand Down Expand Up @@ -252,46 +223,5 @@ def source
c_path = Pathname.new(__FILE__).realpath
@source ||= File.expand_path("../../..", c_path)
end

def version_key
@version_key ||= "#{Tebako::VERSION} at #{source}"
end

def version_cache
version_file_path = File.join(deps, E_VERSION_FILE)
file_version = File.open(version_file_path, &:readline).strip
file_version.match(/(?<version>.+) at (?<source>.+)/)
end

def version_cache_check
match_data = version_cache

return version_unknown unless match_data

if match_data[:version] != Tebako::VERSION
version_mismatch(match_data[:version])
elsif match_data[:source] != source
version_source_mismatch(match_data[:source])
end
rescue StandardError
version_unknown
end

def version_mismatch(cached_version)
puts "Tebako cache was created by a gem version #{cached_version} " \
"and cannot be used for gem version #{Tebako::VERSION}"
clean_cache
end

def version_source_mismatch(cached_source)
puts "CMake cache was created for a different source directory '#{cached_source}' " \
"and cannot be used for '#{source}'"
clean_output
end

def version_unknown
puts "CMake cache version was not recognized, cleaning up"
clean_cache
end
end
end
129 changes: 129 additions & 0 deletions spec/cache_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require "yaml"
require "tebako/cache_manager"

# rubocop:disable Metrics/BlockLength

RSpec.describe Tebako::CacheManager do
let(:deps) { "/path/to/deps" }
let(:output_folder) { "/path/to/output" }
let(:source) { "/path/to/source" }
let(:cache_manager) { Tebako::CacheManager.new(deps, source, output_folder) }

it "cleans the cache by removing the appropriate directories" do
expect(FileUtils).to receive(:rm_rf).with([File.join(deps, ""), File.join(output_folder, "")], secure: true)
cache_manager.clean_cache
end
it "cleans the output by removing the appropriate files and directories" do
nmr = "src/_ruby_*"
nms = "stash_*"
expect(FileUtils).to receive(:rm_rf).with(Dir.glob(File.join(deps, nmr)), secure: true)
expect(FileUtils).to receive(:rm_rf).with(Dir.glob(File.join(deps, nms)), secure: true)
expect(FileUtils).to receive(:rm_rf).with(File.join(output_folder, ""), secure: true)
cache_manager.clean_output
end

describe "#version_key" do
it "returns the correct version key" do
expect(cache_manager.version_key).to eq("#{Tebako::VERSION} at /path/to/source")
end
end

describe "#version_cache_check" do
context "when version cache is unknown" do
let(:match_data) { nil }

it "calls version_unknown" do
allow(cache_manager).to receive(:version_cache).and_return(:match_data)
expect(cache_manager).to receive(:version_unknown)
cache_manager.version_cache_check
end
end

context "when version does not match" do
let(:match_data) { { version: "0.7.0", source: "/path/to/source" } }
it "calls version_mismatch" do
allow(cache_manager).to receive(:version_cache).and_return(match_data)
expect(cache_manager).to receive(:version_mismatch).with("0.7.0")
cache_manager.version_cache_check
end
end

context "when source does not match" do
let(:match_data) { { version: Tebako::VERSION, source: "/different/source" } }

it "calls version_source_mismatch" do
allow(cache_manager).to receive(:version_cache).and_return(match_data)
expect(cache_manager).to receive(:version_source_mismatch).with("/different/source")
cache_manager.version_cache_check
end
end

context "when version and source match" do
let(:match_data) { { version: Tebako::VERSION, source: "/path/to/source" } }

it "does not call any mismatch methods" do
allow(cache_manager).to receive(:version_cache).and_return(match_data)
expect(self).not_to receive(:version_unknown)
expect(self).not_to receive(:version_mismatch)
expect(self).not_to receive(:version_source_mismatch)
cache_manager.version_cache_check
end
end
end

describe "#version_mismatch" do
it "prints the correct message and cleans the cache" do
expect(cache_manager).to receive(:puts)
.with("Tebako cache was created by a gem version 0.9.0 and cannot be used for gem version #{Tebako::VERSION}")
expect(cache_manager).to receive(:clean_cache)
cache_manager.version_mismatch("0.9.0")
end
end

describe "#version_source_mismatch" do
it "prints the correct message and cleans the output" do
expect(cache_manager).to receive(:puts)
.with("CMake cache was created for a different source directory '/different/source' " \
"and cannot be used for '/path/to/source'")
expect(cache_manager).to receive(:clean_output)
allow(cache_manager).to receive(:source).and_return("/path/to/source")
cache_manager.version_source_mismatch("/different/source")
end
end
describe "#version_unknown" do
it "prints the correct message and cleans the cache" do
expect(cache_manager).to receive(:puts).with("CMake cache version was not recognized, cleaning up")
expect(cache_manager).to receive(:clean_cache)
cache_manager.version_unknown
end
end
end

# rubocop:enable Metrics/BlockLength
Loading

0 comments on commit 443f5ee

Please sign in to comment.