@@ -32,7 +32,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
32
32
33
33
/// The current working directory of the process, which is used to relativize URLs of files found
34
34
/// during iteration.
35
- private let workingDirectory = URL ( fileURLWithPath : " . " )
35
+ private let workingDirectory : URL
36
36
37
37
/// Keep track of the current directory we're recursing through.
38
38
private var currentDirectory = URL ( fileURLWithPath: " " )
@@ -46,8 +46,13 @@ public struct FileIterator: Sequence, IteratorProtocol {
46
46
/// Create a new file iterator over the given list of file URLs.
47
47
///
48
48
/// The given URLs may be files or directories. If they are directories, the iterator will recurse
49
- /// into them.
50
- public init ( urls: [ URL ] , followSymlinks: Bool ) {
49
+ /// into them. Symlinks are never followed on Windows platforms as Foundation doesn't support it.
50
+ /// - Parameters:
51
+ /// - urls: `Array` of files or directories to iterate.
52
+ /// - followSymlinks: `Bool` to indicate if symbolic links should be followed when iterating.
53
+ /// - workingDirectory: `URL` that indicates the current working directory. Used for testing.
54
+ public init ( urls: [ URL ] , followSymlinks: Bool , workingDirectory: URL = URL ( fileURLWithPath: " . " ) ) {
55
+ self . workingDirectory = workingDirectory
51
56
self . urls = urls. filter ( inputShouldBeProcessed ( at: ) )
52
57
self . urlIterator = self . urls. makeIterator ( )
53
58
self . followSymlinks = followSymlinks
@@ -158,12 +163,13 @@ public struct FileIterator: Sequence, IteratorProtocol {
158
163
// if the user passes paths that are relative to the current working directory, they will
159
164
// be displayed as relative paths. Otherwise, they will still be displayed as absolute
160
165
// paths.
161
- let relativePath =
162
- path. hasPrefix ( workingDirectory. path)
163
- ? String ( path. dropFirst ( workingDirectory. path. count + 1 ) )
164
- : path
165
- output =
166
- URL ( fileURLWithPath: relativePath, isDirectory: false , relativeTo: workingDirectory)
166
+ let relativePath : String
167
+ if !workingDirectory. isRoot, path. hasPrefix ( workingDirectory. path) {
168
+ relativePath = String ( path. dropFirst ( workingDirectory. path. count) . drop ( while: { $0 == " / " || $0 == #"\"# } ) )
169
+ } else {
170
+ relativePath = path
171
+ }
172
+ output = URL ( fileURLWithPath: relativePath, isDirectory: false , relativeTo: workingDirectory)
167
173
default :
168
174
break
169
175
}
0 commit comments