Skip to content

Commit 5d1190d

Browse files
author
Kevin Paulisse
committed
Add CLI option for override script directory
1 parent 736aa11 commit 5d1190d

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# Provide an optional directory to override default built-in scripts such as git checkout
4+
# and puppet version determination.
5+
# @param parser [OptionParser object] The OptionParser argument
6+
# @param options [Hash] Options hash being constructed; this is modified in this method.
7+
OctocatalogDiff::Cli::Options::Option.newoption(:override_script_path) do
8+
has_weight 385
9+
10+
def parse(parser, options)
11+
parser.on('--override-script-path DIRNAME', 'Directory with scripts to override built-ins') do |dir|
12+
unless dir.start_with?('/')
13+
raise ArgumentError, 'Absolute path is required for --override-script-path'
14+
end
15+
16+
unless File.directory?(dir)
17+
raise Errno::ENOENT, 'Invalid --override-script-path'
18+
end
19+
20+
options[:override_script_path] = dir
21+
end
22+
end
23+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../options_helper'
4+
5+
describe OctocatalogDiff::Cli::Options do
6+
describe '#opt_override_script_path' do
7+
context 'with relative path' do
8+
it 'should raise ArgumentError' do
9+
expect do
10+
run_optparse(['--override-script-path', '../foo/bar'])
11+
end.to raise_error(ArgumentError, 'Absolute path is required for --override-script-path')
12+
end
13+
end
14+
15+
context 'with non-existing directory' do
16+
it 'should raise Errno::ENOENT' do
17+
expect(File).to receive(:'directory?').with('/foo/bar').and_return(false)
18+
expect do
19+
run_optparse(['--override-script-path', '/foo/bar'])
20+
end.to raise_error(Errno::ENOENT, /Invalid --override-script-path/)
21+
end
22+
end
23+
24+
context 'with existing directory' do
25+
it 'should establish option' do
26+
expect(File).to receive(:'directory?').with('/foo/bar').and_return(true)
27+
result = run_optparse(['--override-script-path', '/foo/bar'])
28+
expect(result[:override_script_path]).to eq('/foo/bar')
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)