Skip to content

Commit 5603c13

Browse files
authored
Merge pull request #334 from puppetlabs/cat-1991-skip_missing_dir_check
(CAT-1991) - Skip missing dirs invalid_dir method
2 parents a6dd0f0 + 729b60d commit 5603c13

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ RSpec/MultipleDescribes:
138138
RSpec/MultipleExpectations:
139139
Max: 15
140140

141-
# Offense count: 107
141+
# Offense count: 109
142142
# Configuration parameters: AllowSubject.
143143
RSpec/MultipleMemoizedHelpers:
144-
Max: 17
144+
Max: 19
145145

146146
# Offense count: 55
147147
# Configuration parameters: AllowedGroups.

lib/pwsh.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def initialize(cmd, args = [], options = {})
108108
@powershell_command = cmd
109109
@powershell_arguments = args
110110

111-
raise "Bad configuration for ENV['lib']=#{ENV['lib']} - invalid path" if Pwsh::Util.invalid_directories?(ENV['lib'])
111+
warn "Bad configuration for ENV['lib']=#{ENV['lib']} - invalid path" if Pwsh::Util.invalid_directories?(ENV['lib'])
112112

113113
if Pwsh::Util.on_windows?
114114
# Named pipes under Windows will automatically be mounted in \\.\pipe\...

lib/pwsh/util.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ def on_windows?
1515
!!(host_os =~ /mswin|mingw/)
1616
end
1717

18-
# Verify paths specified are valid directories which exist.
19-
#
20-
# @return [Bool] true if any directories specified do not exist
18+
# Verify paths specified are valid directories.
19+
# Skips paths which do not exist.
20+
# @return [Bool] true if any paths specified are not valid directories
2121
def invalid_directories?(path_collection)
22-
invalid_paths = false
23-
24-
return invalid_paths if path_collection.nil? || path_collection.empty?
22+
return false if path_collection.nil? || path_collection.empty?
2523

26-
paths = on_windows? ? path_collection.split(';') : path_collection.split(':')
27-
paths.each do |path|
28-
invalid_paths = true unless File.directory?(path) || path.empty?
29-
end
24+
delimiter = on_windows? ? ';' : ':'
25+
paths = path_collection.split(delimiter)
3026

31-
invalid_paths
27+
paths.any? { |path| !path.empty? && File.exist?(path) && !File.directory?(path) }
3228
end
3329

3430
# Return a string or symbol converted to snake_case

spec/unit/pwsh/util_spec.rb

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@
8787
end
8888

8989
describe '.invalid_directories?' do
90-
let(:valid_path_a) { 'C:/some/folder' }
91-
let(:valid_path_b) { 'C:/another/folder' }
92-
let(:valid_paths) { 'C:/some/folder;C:/another/folder' }
93-
let(:invalid_path) { 'C:/invalid/path' }
94-
let(:mixed_paths) { 'C:/some/folder;C:/invalid/path;C:/another/folder' }
95-
let(:empty_string) { '' }
96-
let(:empty_members) { 'C:/some/folder;;C:/another/folder' }
90+
let(:valid_path_a) { 'C:/some/folder' }
91+
let(:valid_path_b) { 'C:/another/folder' }
92+
let(:valid_paths) { 'C:/some/folder;C:/another/folder' }
93+
let(:invalid_path) { 'C:/invalid/path' }
94+
let(:mixed_paths) { 'C:/some/folder;C:/another/folder;C:/invalid/path' }
95+
let(:empty_string) { '' }
96+
let(:file_path) { 'C:/some/folder/file.txt' }
97+
let(:non_existent_dir) { 'C:/some/dir/that/doesnt/exist' }
98+
let(:empty_members) { 'C:/some/folder;;C:/another/folder' }
9799

98100
it 'returns false if passed nil' do
99101
expect(described_class.invalid_directories?(nil)).to be false
@@ -103,40 +105,70 @@
103105
expect(described_class.invalid_directories?('')).to be false
104106
end
105107

108+
it 'returns false if a file path is provided' do
109+
expect(described_class.invalid_directories?(file_path)).to be false
110+
end
111+
106112
it 'returns false if one valid path is provided' do
107113
expect(described_class).to receive(:on_windows?).and_return(true)
114+
expect(File).to receive(:exist?).with(valid_path_a).and_return(true)
108115
expect(File).to receive(:directory?).with(valid_path_a).and_return(true)
109116
expect(described_class.invalid_directories?(valid_path_a)).to be false
110117
end
111118

112119
it 'returns false if a collection of valid paths is provided' do
113120
expect(described_class).to receive(:on_windows?).and_return(true)
114121
expect(File).to receive(:directory?).with(valid_path_a).and_return(true)
122+
expect(File).to receive(:exist?).with(valid_path_a).and_return(true)
115123
expect(File).to receive(:directory?).with(valid_path_b).and_return(true)
124+
expect(File).to receive(:exist?).with(valid_path_b).and_return(true)
116125
expect(described_class.invalid_directories?(valid_paths)).to be false
117126
end
118127

119128
it 'returns true if there is only one path and it is invalid' do
120129
expect(described_class).to receive(:on_windows?).and_return(true)
130+
expect(File).to receive(:exist?).with(invalid_path).and_return(true)
121131
expect(File).to receive(:directory?).with(invalid_path).and_return(false)
122132
expect(described_class.invalid_directories?(invalid_path)).to be true
123133
end
124134

125135
it 'returns true if the collection has on valid and one invalid member' do
126136
expect(described_class).to receive(:on_windows?).and_return(true)
137+
expect(File).to receive(:exist?).with(valid_path_a).and_return(true)
127138
expect(File).to receive(:directory?).with(valid_path_a).and_return(true)
139+
expect(File).to receive(:exist?).with(valid_path_b).and_return(true)
128140
expect(File).to receive(:directory?).with(valid_path_b).and_return(true)
141+
expect(File).to receive(:exist?).with(invalid_path).and_return(true)
129142
expect(File).to receive(:directory?).with(invalid_path).and_return(false)
130143
expect(described_class.invalid_directories?(mixed_paths)).to be true
131144
end
132145

133146
it 'returns false if collection has empty members but other entries are valid' do
134147
expect(described_class).to receive(:on_windows?).and_return(true)
148+
expect(File).to receive(:exist?).with(valid_path_a).and_return(true)
135149
expect(File).to receive(:directory?).with(valid_path_a).and_return(true)
150+
expect(File).to receive(:exist?).with(valid_path_b).and_return(true)
136151
expect(File).to receive(:directory?).with(valid_path_b).and_return(true)
137152
allow(File).to receive(:directory?).with('')
138153
expect(described_class.invalid_directories?(empty_members)).to be false
139154
end
155+
156+
it 'returns true if a collection has valid members but also contains a file path' do
157+
expect(described_class).to receive(:on_windows?).and_return(true)
158+
expect(File).to receive(:exist?).with(valid_path_a).and_return(true)
159+
expect(File).to receive(:directory?).with(valid_path_a).and_return(true)
160+
expect(File).to receive(:exist?).with(file_path).and_return(true)
161+
expect(File).to receive(:directory?).with(file_path).and_return(false)
162+
expect(described_class.invalid_directories?("#{valid_path_a};#{file_path}")).to be true
163+
end
164+
165+
it 'returns false if a collection has valid members but contains a non-existent dir path' do
166+
expect(described_class).to receive(:on_windows?).and_return(true)
167+
expect(File).to receive(:exist?).with(valid_path_a).and_return(true)
168+
expect(File).to receive(:directory?).with(valid_path_a).and_return(true)
169+
expect(File).to receive(:exist?).with(non_existent_dir).and_return(false)
170+
expect(described_class.invalid_directories?("#{valid_path_a};#{non_existent_dir}")).to be false
171+
end
140172
end
141173

142174
describe '.snake_case' do

0 commit comments

Comments
 (0)