@@ -32,7 +32,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
3232
3333 /// The current working directory of the process, which is used to relativize URLs of files found
3434 /// during iteration.
35- private let workingDirectory = URL ( fileURLWithPath : " . " )
35+ private let workingDirectory : URL
3636
3737 /// Keep track of the current directory we're recursing through.
3838 private var currentDirectory = URL ( fileURLWithPath: " " )
@@ -46,8 +46,13 @@ public struct FileIterator: Sequence, IteratorProtocol {
4646 /// Create a new file iterator over the given list of file URLs.
4747 ///
4848 /// 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
5156 self . urls = urls. filter ( inputShouldBeProcessed ( at: ) )
5257 self . urlIterator = self . urls. makeIterator ( )
5358 self . followSymlinks = followSymlinks
@@ -158,12 +163,13 @@ public struct FileIterator: Sequence, IteratorProtocol {
158163 // if the user passes paths that are relative to the current working directory, they will
159164 // be displayed as relative paths. Otherwise, they will still be displayed as absolute
160165 // 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)
167173 default :
168174 break
169175 }
0 commit comments