Skip to content

Commit 142f409

Browse files
authored
FoundationEssentials: relax error handling for SHGetFolderPath (#676)
When running in CI, we would see failures due to the environment variables being changed. This relaxes the precondition to allow the test failure to propagate.
1 parent e473ab7 commit 142f409

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Sources/FoundationEssentials/FileManager/SearchPaths/FileManager+WindowsSearchPaths.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414

1515
import WinSDK
1616

17-
private func _url(for id: KNOWNFOLDERID) -> URL {
17+
private func _url(for id: KNOWNFOLDERID) -> URL? {
1818
var pszPath: PWSTR?
1919
let hr: HRESULT = withUnsafePointer(to: id) { id in
2020
SHGetKnownFolderPath(id, KF_FLAG_DEFAULT, nil, &pszPath)
2121
}
22-
precondition(SUCCEEDED(hr), "SHGetKnownFolderPath failed \(String(hr, radix: 16))")
22+
guard SUCCEEDED(hr) else { return nil }
2323
defer { CoTaskMemFree(pszPath) }
2424
return URL(filePath: String(decodingCString: pszPath!, as: UTF16.self), directoryHint: .isDirectory)
2525
}
2626

2727
func _WindowsSearchPathURL(for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask) -> URL? {
2828
switch (directory, domain) {
2929
case (.autosavedInformationDirectory, .userDomainMask):
30-
_url(for: FOLDERID_LocalAppData).appending(component: "Autosave Information", directoryHint: .isDirectory)
30+
_url(for: FOLDERID_LocalAppData)?
31+
.appending(component: "Autosave Information", directoryHint: .isDirectory)
3132

3233
case (.desktopDirectory, .userDomainMask):
3334
_url(for: FOLDERID_Desktop)

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ final class FileManagerTests : XCTestCase {
726726
.downloadsDirectory,
727727
.moviesDirectory,
728728
.musicDirectory,
729-
.picturesDirectory,
730729
.sharedPublicDirectory
731730
], exists: true)
732731

@@ -775,6 +774,12 @@ final class FileManagerTests : XCTestCase {
775774
#if !os(watchOS) && !os(tvOS)
776775
assertSearchPaths([.trashDirectory], exists: (isMacOS && isFramework) || (!isDarwin && !isWindows))
777776
#endif
777+
778+
// .picturesDirectory does not exist in CI, though it does exist in user
779+
// desktop scenarios.
780+
#if !os(Windows)
781+
assertSearchPaths([.picturesDirectory], exists: true)
782+
#endif
778783

779784
// .applicationScriptsDirectory is only available on macOS and only produces paths in the framework build
780785
#if os(macOS)

0 commit comments

Comments
 (0)