Skip to content

Commit

Permalink
feat: create tmpdir at 'tebako_original_pwd' if all standard options …
Browse files Browse the repository at this point in the history
…fail
  • Loading branch information
maxirmx committed Aug 10, 2024
1 parent 86448bc commit 6fe3d0e
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Gemfile.lock

# rspec failure tracking
.rspec_status

.vscode
51 changes: 32 additions & 19 deletions lib/tebako-runtime/memfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,46 @@
# Module TebakoRuntime
# Methods to extract files from memfs to temporary folder
module TebakoRuntime
COMPILER_MEMFS = RUBY_PLATFORM =~ /mswin|mingw/ ? "A:/__tebako_memfs__" : "/__tebako_memfs__"
COMPILER_MEMFS_LIB_CACHE = Pathname.new(Dir.mktmpdir("tebako-runtime-"))
def self.initialize_compiler_memfs_lib_cache
Pathname.new(Dir.mktmpdir("tebako-runtime-"))
rescue StandardError
return nil unless defined?($tebako_original_pwd) && !$tebako_original_pwd.nil? # rubocop:disable Style/GlobalVars

class << self
def extract(file, wild, extract_path)
files = if wild
Dir.glob("#{File.dirname(file)}/*#{File.extname(file)}")
else
[file]
end
FileUtils.cp_r files, extract_path
begin
Pathname.new(Dir.mktmpdir("tebako-runtime-", $tebako_original_pwd)) # rubocop:disable Style/GlobalVars
rescue StandardError
nil
end
end

COMPILER_MEMFS = RUBY_PLATFORM =~ /mswin|mingw/ ? "A:/__tebako_memfs__" : "/__tebako_memfs__"
COMPILER_MEMFS_LIB_CACHE = initialize_compiler_memfs_lib_cache
exit if COMPILER_MEMFS_LIB_CACHE.nil?

# wild == true means "also extract other files with the same extension"
def extract_memfs(file, wild: false, cache_path: COMPILER_MEMFS_LIB_CACHE)
is_quoted = file.quoted?
file = file.unquote if is_quoted
return is_quoted ? file.quote : file unless File.exist?(file) && file.start_with?(COMPILER_MEMFS)
def self.extract(file, wild, extract_path)
files = if wild
Dir.glob("#{File.dirname(file)}/*#{File.extname(file)}")
else
[file]
end
FileUtils.cp_r files, extract_path
end

memfs_extracted_file = cache_path + File.basename(file)
extract(file, wild, cache_path) unless memfs_extracted_file.exist?
# wild == true means "also extract other files with the same extension"
def self.extract_memfs(file, wild: false, cache_path: COMPILER_MEMFS_LIB_CACHE)
is_quoted = file.quoted?
file = file.unquote if is_quoted
return is_quoted ? file.quote : file unless File.exist?(file) && file.start_with?(COMPILER_MEMFS)

is_quoted ? memfs_extracted_file.to_path.quote : memfs_extracted_file.to_path
end
memfs_extracted_file = cache_path + File.basename(file)
extract(file, wild, cache_path) unless memfs_extracted_file.exist?

is_quoted ? memfs_extracted_file.to_path.quote : memfs_extracted_file.to_path
end
end

at_exit do
return if TebakoRuntime::COMPILER_MEMFS_LIB_CACHE.nil?

FileUtils.remove_dir(TebakoRuntime::COMPILER_MEMFS_LIB_CACHE.to_path, true)
end
2 changes: 1 addition & 1 deletion lib/tebako-runtime/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# POSSIBILITY OF SUCH DAMAGE.

module TebakoRuntime
VERSION = "0.5.2"
VERSION = "0.5.3"
end
81 changes: 81 additions & 0 deletions spec/memfs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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 "rspec"
require "pathname"
require "tempfile"

# rubocop:disable Style/GlobalVars
# Mock the global variable

$tebako_original_pwd = nil
RSpec.describe "COMPILER_MEMFS_LIB_CACHE initialization" do # rubocop:disable Metrics/BlockLength
before do
# Reset the global variable before each test
$tebako_original_pwd = nil
end

it "creates a temporary directory successfully" do
allow(Dir).to receive(:mktmpdir).and_return("/tmp/tebako-runtime-1234")
memfs_cache = TebakoRuntime.send(:initialize_compiler_memfs_lib_cache)
expect(memfs_cache.to_s).to eq("/tmp/tebako-runtime-1234")
end

it "handles failure to create temporary directory and uses $tebako_original_pwd" do
allow(Dir).to receive(:mktmpdir).and_raise(StandardError)
$tebako_original_pwd = "/tmp"
allow(Dir).to receive(:mktmpdir).with("tebako-runtime-",
$tebako_original_pwd).and_return("/tmp/tebako-runtime-5678")

memfs_cache = TebakoRuntime.send(:initialize_compiler_memfs_lib_cache)
expect(memfs_cache.to_s).to eq("/tmp/tebako-runtime-5678")
end

it "handles failure to create temporary directory and $tebako_original_pwd is nil" do
allow(Dir).to receive(:mktmpdir).and_raise(StandardError)
$tebako_original_pwd = nil

memfs_cache = TebakoRuntime.send(:initialize_compiler_memfs_lib_cache)
expect(memfs_cache).to be_nil
end

it "handles failure to create temporary directory botha at standard locations and with $tebako_original_pwd is nil" do
call_count = 0
allow(Dir).to receive(:mktmpdir) do
call_count += 1
raise StandardError if call_count <= 2

"/tmp/tebako-runtime-1234"
end

$tebako_original_pwd = "/tmp"

memfs_cache = TebakoRuntime.send(:initialize_compiler_memfs_lib_cache)
expect(memfs_cache).to be_nil
end
end
# rubocop:enable Style/GlobalVars
2 changes: 2 additions & 0 deletions spec/pass_through_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require "tebako-runtime"

# rubocop:disable Metrics/BlockLength
RSpec.describe TebakoRuntime do
it "provides a stub for ffi gem in pass through mode on Windows" do
Expand Down
1 change: 1 addition & 0 deletions spec/runtime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# POSSIBILITY OF SUCH DAMAGE.

require "pathname"
require "tebako-runtime"

# rubocop:disable Metrics/BlockLength
RSpec.describe TebakoRuntime do
Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
require "simplecov-cobertura"
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter

require "tebako-runtime"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down
2 changes: 2 additions & 0 deletions spec/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require "tebako-runtime"

RSpec.describe String do
it "recognizes quoted strings" do
expect('"quoted"'.quoted?).to be(true)
Expand Down

0 comments on commit 6fe3d0e

Please sign in to comment.