Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

A path that ends in a slash names the entry itself

Țugui Dragoș31b7deb041ff
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
index 075eac69..4dea8b38 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
@@ -120,7 +120,7 @@ public enum PathPattern {
// (`App [Beta]`) is not read as a character class.
let isPattern = path.contains("*") || path.contains("?")
if !isPattern, FileManager.default.fileExists(atPath: path) {
- let url = URL(filePath: path)
+ let url = entry(named: path)
return isIrreplaceable(url, home: root) ? [] : [url]
}
guard isPattern || path.contains("[") else { return [] }
@@ -135,8 +135,14 @@ public enum PathPattern {
guard code == 0 || code == GLOB_NOSPACE, results.gl_pathv != nil else { return [] }
return (0..<Int(results.gl_pathc)).prefix(maximum).compactMap { index in
guard let pointer = results.gl_pathv[index] else { return nil }
- let url = URL(filePath: String(cString: pointer))
+ let url = entry(named: String(cString: pointer))
return isIrreplaceable(url, home: root) ? nil : url
}
}
+
+ /// The entry `path` names. A path that ends in `/` names folders only, and the kernel follows a link to what it
+ /// leads to when the slash is kept, so the entry is named without it.
+ private static func entry(named path: String) -> URL {
+ URL(filePath: path.count > 1 && path.hasSuffix("/") ? String(path.dropLast()) : path)
+ }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift
index a42e28fd..c74c70d6 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift
@@ -12,6 +12,22 @@ struct PathPatternTests {
#expect(PathPattern.expand("Library/Caches/missing", home: directory.url).isEmpty)
}
+ /// A pattern that ends in `/` matches folders only, and each comes back named as the entry itself, without the
+ /// slash, so a link among them is a link and not the folder it leads to.
+ @Test func aPatternEndingInASlashMatchesFoldersByTheirOwnNames() throws {
+ let directory = try TemporaryDirectory()
+ let web = try directory.directory(".virtualenvs/web")
+ try directory.file(".virtualenvs/postactivate")
+ let elsewhere = try directory.directory("Volumes/Fast/env")
+ let link = directory.url.appending(path: ".virtualenvs/fast")
+ try FileManager.default.createSymbolicLink(at: link, withDestinationURL: elsewhere)
+
+ let matches = PathPattern.expand(".virtualenvs/*/", home: directory.url).map { $0.path(percentEncoded: false) }
+
+ #expect(Set(matches) == Set([web, link].map(PathPattern.comparablePath(of:))))
+ #expect(PathPattern.expand(".virtualenvs/web/", home: directory.url).map { $0.path(percentEncoded: false) } == [PathPattern.comparablePath(of: web)])
+ }
+
@Test func expandsWildcardsIntoEveryFolderThatExists() throws {
let directory = try TemporaryDirectory()
try directory.directory("Library/Caches/Google/AndroidStudio2026.1")