12
12
@_spi ( Experimental) public import Testing
13
13
public import Foundation
14
14
15
+ #if SWT_TARGET_OS_APPLE && canImport(UniformTypeIdentifiers)
16
+ private import UniformTypeIdentifiers
17
+ #endif
18
+
15
19
#if !SWT_NO_FILE_IO
16
20
extension URL {
17
21
/// The file system path of the URL, equivalent to `path`.
@@ -30,6 +34,15 @@ extension URL {
30
34
31
35
// MARK: - Attaching files
32
36
37
+ #if SWT_TARGET_OS_APPLE && canImport(UniformTypeIdentifiers)
38
+ @available ( _uttypesAPI, * )
39
+ extension UTType {
40
+ /// A type that represents a `.tgz` archive, or `nil` if the system does not
41
+ /// recognize that content type.
42
+ fileprivate static let tgz = UTType ( " org.gnu.gnu-zip-tar-archive " )
43
+ }
44
+ #endif
45
+
33
46
@_spi ( Experimental)
34
47
extension Test . Attachment {
35
48
/// Initialize an instance of this type with the contents of the given URL.
@@ -57,25 +70,33 @@ extension Test.Attachment {
57
70
let url = url. resolvingSymlinksInPath ( )
58
71
let isDirectory = try url. resourceValues ( forKeys: [ . isDirectoryKey] ) . isDirectory!
59
72
60
- let attachableValue : any Test . Attachable & Sendable
61
- if isDirectory {
62
- attachableValue = try await _DirectoryContentAttachableProxy ( contentsOfDirectoryAt: url)
73
+ // Determine the preferred name of the attachment if one was not provided.
74
+ var preferredName = if let preferredName {
75
+ preferredName
76
+ } else if case let lastPathComponent = url. lastPathComponent, !lastPathComponent. isEmpty {
77
+ lastPathComponent
63
78
} else {
64
- // Load the file.
65
- attachableValue = try Data ( contentsOf: url, options: [ . mappedIfSafe] )
79
+ Self . defaultPreferredName
66
80
}
67
81
68
- // Determine the preferred name of the attachment if one was not provided.
69
- var preferredName = preferredName
70
- if preferredName == nil , case let lastPathComponent = url. lastPathComponent, !lastPathComponent. isEmpty {
71
- if isDirectory {
72
- preferredName = ( lastPathComponent as NSString ) . appendingPathExtension ( " tar.gz " )
73
- } else {
74
- preferredName = lastPathComponent
75
- }
76
- }
82
+ if isDirectory {
83
+ // Ensure the preferred name of the archive has an appropriate extension.
84
+ preferredName = {
85
+ #if SWT_TARGET_OS_APPLE && canImport(UniformTypeIdentifiers)
86
+ if #available( _uttypesAPI, * ) , let tgz = UTType . tgz {
87
+ return ( preferredName as NSString ) . appendingPathExtension ( for: tgz)
88
+ }
89
+ #endif
90
+ return ( preferredName as NSString ) . appendingPathExtension ( " tgz " ) ?? preferredName
91
+ } ( )
77
92
78
- self . init ( attachableValue, named: preferredName, sourceLocation: sourceLocation)
93
+ let attachableValue = try await _DirectoryContentAttachableProxy ( contentsOfDirectoryAt: url)
94
+ self . init ( attachableValue, named: preferredName, sourceLocation: sourceLocation)
95
+ } else {
96
+ // Load the file.
97
+ let attachableValue = try Data ( contentsOf: url, options: [ . mappedIfSafe] )
98
+ self . init ( attachableValue, named: preferredName, sourceLocation: sourceLocation)
99
+ }
79
100
}
80
101
}
81
102
@@ -101,12 +122,12 @@ private struct _DirectoryContentAttachableProxy: Test.Attachable {
101
122
/// directories cannot be compressed on this platform.
102
123
///
103
124
/// This initializer asynchronously compresses the contents of `directoryURL`
104
- /// into an archive (currently of `.tar.gz ` format, although this is subject
105
- /// to change) and stores a mapped copy of that archive.
125
+ /// into an archive (currently of `.tgz ` format, although this is subject to
126
+ /// change) and stores a mapped copy of that archive.
106
127
init ( contentsOfDirectoryAt directoryURL: URL ) async throws {
107
128
url = directoryURL
108
129
109
- let temporaryName = " \( UUID ( ) . uuidString) .tar.gz "
130
+ let temporaryName = " \( UUID ( ) . uuidString) .tgz "
110
131
let temporaryURL = FileManager . default. temporaryDirectory. appendingPathComponent ( temporaryName)
111
132
112
133
#if !SWT_NO_PROCESS_SPAWNING
0 commit comments