Skip to content

Commit 45254e2

Browse files
committed
Let scriptrunner support an existing tempdir
1 parent 4069cb3 commit 45254e2

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

lib/octocatalog-diff/util/scriptrunner.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ class ScriptRunner
1313
# For an exception running the script
1414
class ScriptException < RuntimeError; end
1515

16-
attr_reader :script, :script_src, :logger, :stdout, :stderr, :exitcode
16+
attr_reader :script, :script_src, :logger, :stdout, :stderr, :exitcode, :existing_tempdir
1717

1818
# Create the object - the object is a configured script, which can be executed multiple
1919
# times with different environment varibles.
2020
#
2121
# @param opts [Hash] Options hash
2222
# opts[:default_script] (Required) Path to script, relative to `scripts` directory
2323
# opts[:logger] (Optional) Logger object
24+
# opts[:existing_tempdir] (Optional) An existing temporary directory (helpful when parallelizing)
2425
# opts[:override_script_path] (Optional) Directory where a similarly-named script MAY exist
2526
def initialize(opts = {})
2627
@logger = opts[:logger]
2728
@script_src = find_script(opts.fetch(:default_script), opts[:override_script_path])
29+
@existing_tempdir = opts[:existing_tempdir]
2830
@script = temp_script(@script_src)
2931
@stdout = nil
3032
@stderr = nil
@@ -91,14 +93,20 @@ def log(priority, message, logger = @logger)
9193
# @return [String] Path to tempfile containing script
9294
def temp_script(script)
9395
raise Errno::ENOENT, "Script '#{script}' not found" unless File.file?(script)
94-
temp_dir = Dir.mktmpdir('ocd-scriptrunner-')
95-
at_exit do
96-
begin
97-
FileUtils.remove_entry_secure temp_dir
98-
rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
99-
# OK if the directory doesn't exist since we're trying to remove it anyway
96+
temp_dir = if existing_tempdir
97+
Dir.mktmpdir('ocd-scriptrunner-', existing_tempdir)
98+
else
99+
temp_dir_local = Dir.mktmpdir('ocd-scriptrunner-')
100+
at_exit do
101+
begin
102+
FileUtils.remove_entry_secure temp_dir_local
103+
rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
104+
# OK if the directory doesn't exist since we're trying to remove it anyway
105+
end
100106
end
107+
temp_dir_local
101108
end
109+
102110
temp_file = File.join(temp_dir, File.basename(script))
103111
File.open(temp_file, 'w') { |f| f.write(File.read(script)) }
104112
FileUtils.chmod 0o755, temp_file

spec/octocatalog-diff/tests/util/scriptrunner_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,43 @@
5757
end
5858
end
5959

60+
describe '#temp_script' do
61+
context 'when running under --parallel' do
62+
before(:each) do
63+
@base_tempdir = Dir.mktmpdir('ocd-tempdir')
64+
end
65+
66+
after(:each) do
67+
OctocatalogDiff::Spec.clean_up_tmpdir(@base_tempdir)
68+
end
69+
70+
it 'should create a temporary directory within the existing tempdir' do
71+
opts = {
72+
default_script: 'env/env.sh',
73+
existing_tempdir: @base_tempdir
74+
}
75+
subject = described_class.new(opts)
76+
script = subject.send(:temp_script, subject.script_src)
77+
78+
regex = Regexp.new('\\A' + Regexp.escape(@base_tempdir) + '/ocd-scriptrunner[^/]+/env.sh\\z')
79+
expect(script).to match(regex)
80+
end
81+
end
82+
83+
context 'when not running under --parallel' do
84+
it 'should create a new temporary directory and clean it up at_exit' do
85+
opts = {
86+
default_script: 'env/env.sh'
87+
}
88+
subject = described_class.new(opts)
89+
script = subject.send(:temp_script, subject.script_src)
90+
91+
regex = Regexp.new('/ocd-scriptrunner[^/]+/env.sh\\z')
92+
expect(script).to match(regex)
93+
end
94+
end
95+
end
96+
6097
describe '#run' do
6198
before(:each) do
6299
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger

0 commit comments

Comments
 (0)