Skip to content

Commit eba47dc

Browse files
author
Kevin Paulisse
committed
Begin scriptrunner class to locate and execute scripts
1 parent e3e2d91 commit eba47dc

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
# Execute a built-in script (which can also be overridden with a user-supplied script)
4+
5+
require 'fileutils'
6+
require 'open3'
7+
require 'shellwords'
8+
9+
module OctocatalogDiff
10+
module Util
11+
# This is a utility class to execute a built-in script.
12+
class ScriptRunner
13+
attr_reader :script, :logger
14+
15+
def initialize(opts = {})
16+
default_script = opts.fetch(:default_script)
17+
working_dir = opts.fetch(:working_dir)
18+
override_script_path = opts[:override_script_path]
19+
@logger = opts.fetch(:logger)
20+
21+
@script = find_script(default_script, override_script_path)
22+
assert_directory_exists(working_dir)
23+
end
24+
25+
def run(opts = {})
26+
end
27+
28+
private
29+
30+
def find_script(default_script, override_script_path)
31+
if override_script_path
32+
script_test = File.join(override_script_path, File.basename(default_script))
33+
if File.file?(script_test)
34+
logger.debug "Selecting #{script_test} from override script path"
35+
return script_test
36+
else
37+
logger.debug "Did not find #{script_test} in override script path"
38+
end
39+
end
40+
41+
script = File.expand_path("../../../scripts/#{default_script}", File.dirname(__FILE__))
42+
return script if File.file?(script)
43+
44+
raise Errno::ENOENT, "Unable to locate default script '#{default_script}'"
45+
end
46+
47+
def assert_directory_exists(dir)
48+
return if File.directory?(dir)
49+
raise Errno::ENOENT, "Invalid directory '#{dir}'"
50+
end
51+
end
52+
end
53+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require OctocatalogDiff::Spec.require_path('/util/scriptrunner')
5+
6+
describe OctocatalogDiff::Util::ScriptRunner do
7+
describe '#initialize' do
8+
before(:each) do
9+
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger
10+
end
11+
12+
it 'should raise error when script cannot be found' do
13+
opts = {
14+
default_script: 'git-extract/THIS-DOES-NOT-EXIST',
15+
working_dir: File.dirname(__FILE__),
16+
logger: @logger
17+
}
18+
expect { described_class.new(opts) }.to raise_error(Errno::ENOENT)
19+
end
20+
21+
it 'should raise error when working directory cannot be found' do
22+
opts = {
23+
default_script: 'git-extract/git-extract.sh',
24+
working_dir: File.join(File.dirname(__FILE__), 'THIS-DOES-NOT-EXIST'),
25+
logger: @logger
26+
}
27+
expect { described_class.new(opts) }.to raise_error(Errno::ENOENT)
28+
end
29+
30+
it 'should use override script when it is found' do
31+
opts = {
32+
default_script: 'asdflkjasf/scriptrunner_spec.rb',
33+
working_dir: File.join(File.dirname(__FILE__)),
34+
override_script_path: File.join(File.dirname(__FILE__)),
35+
logger: @logger
36+
}
37+
obj = described_class.new(opts)
38+
expect(obj.script).to eq(__FILE__)
39+
end
40+
41+
it 'should use default script when override is not found' do
42+
opts = {
43+
default_script: 'git-extract/git-extract.sh',
44+
working_dir: File.join(File.dirname(__FILE__)),
45+
override_script_path: File.join(File.dirname(__FILE__)),
46+
logger: @logger
47+
}
48+
obj = described_class.new(opts)
49+
answer = File.expand_path('../../../../scripts/git-extract/git-extract.sh', File.dirname(__FILE__))
50+
expect(obj.script).to eq(answer)
51+
end
52+
53+
it 'should use default script when override is not provided' do
54+
opts = {
55+
default_script: 'git-extract/git-extract.sh',
56+
working_dir: File.join(File.dirname(__FILE__)),
57+
logger: @logger
58+
}
59+
obj = described_class.new(opts)
60+
answer = File.expand_path('../../../../scripts/git-extract/git-extract.sh', File.dirname(__FILE__))
61+
expect(obj.script).to eq(answer)
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)