Skip to content

Commit c2819c4

Browse files
author
Kevin Paulisse
committed
Move working directory to run method, update tests
1 parent 6efa0a6 commit c2819c4

3 files changed

Lines changed: 45 additions & 21 deletions

File tree

lib/octocatalog-diff/util/scriptrunner.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,27 @@ module Util
1212
class ScriptRunner
1313
attr_reader :script, :logger
1414

15+
# Create the object - the object is a configured script, which can be executed multiple
16+
# times with different environment varibles.
17+
#
18+
# @param opts [Hash] Options hash
19+
# opts[:default_script] (Required) Path to script, relative to `scripts` directory
20+
# opts[:logger] (Required) Logger object
21+
# opts[:override_script_path] (Optional) Directory where a similarly-named script MAY exist
1522
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]
1923
@logger = opts.fetch(:logger)
20-
21-
@script = find_script(default_script, override_script_path)
22-
assert_directory_exists(working_dir)
24+
@script = find_script(opts.fetch(:default_script), opts[:override_script_path])
2325
end
2426

27+
# Execute the script from a given working directory, with additional environment variables
28+
# specified in the options hash.
29+
#
30+
# @param opts [Hash] Options hash
31+
# opts[:working_dir] (Required) Directory where script is to be executed
32+
# opts[<STRING>] (Optional) Environment variable
2533
def run(opts = {})
34+
working_dir = opts.fetch(:working_dir)
35+
assert_directory_exists(working_dir)
2636
end
2737

2838
private

scripts/env/env.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
# This script echoes back the environment. This is used for spec testing
4+
# and possible debugging.
5+
6+
env | sort

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,13 @@
1111

1212
it 'should raise error when script cannot be found' do
1313
opts = {
14-
default_script: 'git-extract/THIS-DOES-NOT-EXIST',
14+
default_script: 'env/THIS-DOES-NOT-EXIST',
1515
working_dir: File.dirname(__FILE__),
1616
logger: @logger
1717
}
1818
expect { described_class.new(opts) }.to raise_error(Errno::ENOENT)
1919
end
2020

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-
3021
it 'should use override script when it is found' do
3122
opts = {
3223
default_script: 'asdflkjasf/scriptrunner_spec.rb',
@@ -41,27 +32,44 @@
4132

4233
it 'should use default script when override is not found' do
4334
opts = {
44-
default_script: 'git-extract/git-extract.sh',
35+
default_script: 'env/env.sh',
4536
working_dir: File.join(File.dirname(__FILE__)),
4637
override_script_path: File.join(File.dirname(__FILE__)),
4738
logger: @logger
4839
}
4940
obj = described_class.new(opts)
50-
answer = File.expand_path('../../../../scripts/git-extract/git-extract.sh', File.dirname(__FILE__))
41+
answer = File.expand_path('../../../../scripts/env/env.sh', File.dirname(__FILE__))
5142
expect(obj.script).to eq(answer)
52-
expect(@logger_str.string).to match(/Did not find.+git-extract.sh in override script path/)
43+
expect(@logger_str.string).to match(/Did not find.+env.sh in override script path/)
5344
end
5445

5546
it 'should use default script when override is not provided' do
5647
opts = {
57-
default_script: 'git-extract/git-extract.sh',
48+
default_script: 'env/env.sh',
5849
working_dir: File.join(File.dirname(__FILE__)),
5950
logger: @logger
6051
}
6152
obj = described_class.new(opts)
62-
answer = File.expand_path('../../../../scripts/git-extract/git-extract.sh', File.dirname(__FILE__))
53+
answer = File.expand_path('../../../../scripts/env/env.sh', File.dirname(__FILE__))
6354
expect(obj.script).to eq(answer)
6455
expect(@logger_str.string).to eq('')
6556
end
6657
end
58+
59+
describe '#run' do
60+
before(:each) do
61+
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger
62+
@described_obj = described_class.new(
63+
default_script: 'env/env.sh',
64+
logger: @logger
65+
)
66+
end
67+
68+
it 'should raise error when working directory cannot be found' do
69+
opts = {
70+
working_dir: File.join(File.dirname(__FILE__), 'THIS-DOES-NOT-EXIST')
71+
}
72+
expect { @described_obj.run(opts) }.to raise_error(Errno::ENOENT)
73+
end
74+
end
6775
end

0 commit comments

Comments
 (0)