-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_which.rb
71 lines (62 loc) · 1.77 KB
/
test_which.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
require_relative 'test_helper'
# Test the Which class
class TestWhich < MiniTest::Test
let_mock :cmd, :pathext, :dirname,
:foo_potential_bin_path,
:bar_potential_bin_path,
:baz_potential_bin_path
def expect_path_queried(dirname, binpath, executable, directory,
extension = '')
@mocks[:file].expects(:join).with(dirname, "#{cmd}#{extension}")
.returns(binpath)
@mocks[:file].expects(:executable?).with(binpath)
.returns(executable)
@mocks[:file].expects(:directory?).with(binpath)
.returns(directory)
.at_least(0)
end
def mock_unix
@mocks[:env] = {
'PATH' => 'foo:bar:baz',
}
[
['foo', foo_potential_bin_path, true, true],
['bar', bar_potential_bin_path, false, nil],
['baz', baz_potential_bin_path, true, false],
].each do |dirname, binpath, executable, directory|
expect_path_queried(dirname, binpath, executable, directory)
end
end
def mock_windows
@mocks[:env] = {
'PATHEXT' => '.COM;.EXE;.BAT',
'PATH' => 'foo;bar;baz',
}
expect_path_queried('foo', foo_potential_bin_path, true, false, '.COM')
end
def test_which_unix
which = get_test_object(':') do
mock_unix
end
assert_equal(baz_potential_bin_path, which.which(cmd))
end
def test_which_windows
which = get_test_object(';') do
mock_windows
end
assert_equal(foo_potential_bin_path, which.which(cmd))
end
def create_mocks(separator)
{
env: mock('env'),
file: mock('file'),
separator: separator,
}
end
def get_test_object(separator, &twiddle_mocks)
@mocks = create_mocks(separator)
yield unless twiddle_mocks.nil?
Quality::Which.new(**@mocks)
end
end