Skip to content

Commit 4ebf0f7

Browse files
committed
Consistently apply .tgz to the preferred name of a compressed directory
1 parent d643ecf commit 4ebf0f7

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

Sources/Overlays/_Testing_Foundation/Attachments/Test.Attachment+URL.swift

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
@_spi(Experimental) public import Testing
1313
public import Foundation
1414

15+
#if SWT_TARGET_OS_APPLE && canImport(UniformTypeIdentifiers)
16+
private import UniformTypeIdentifiers
17+
#endif
18+
1519
#if !SWT_NO_FILE_IO
1620
extension URL {
1721
/// The file system path of the URL, equivalent to `path`.
@@ -30,6 +34,15 @@ extension URL {
3034

3135
// MARK: - Attaching files
3236

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+
3346
@_spi(Experimental)
3447
extension Test.Attachment {
3548
/// Initialize an instance of this type with the contents of the given URL.
@@ -57,25 +70,33 @@ extension Test.Attachment {
5770
let url = url.resolvingSymlinksInPath()
5871
let isDirectory = try url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory!
5972

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
6378
} else {
64-
// Load the file.
65-
attachableValue = try Data(contentsOf: url, options: [.mappedIfSafe])
79+
Self.defaultPreferredName
6680
}
6781

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+
}()
7792

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+
}
79100
}
80101
}
81102

@@ -101,12 +122,12 @@ private struct _DirectoryContentAttachableProxy: Test.Attachable {
101122
/// directories cannot be compressed on this platform.
102123
///
103124
/// 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.
106127
init(contentsOfDirectoryAt directoryURL: URL) async throws {
107128
url = directoryURL
108129

109-
let temporaryName = "\(UUID().uuidString).tar.gz"
130+
let temporaryName = "\(UUID().uuidString).tgz"
110131
let temporaryURL = FileManager.default.temporaryDirectory.appendingPathComponent(temporaryName)
111132

112133
#if !SWT_NO_PROCESS_SPAWNING

Tests/TestingTests/AttachmentTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct AttachmentTests {
8282

8383
@Test func writeAttachmentWithMultiplePathExtensions() throws {
8484
let attachableValue = MySendableAttachable(string: "<!doctype html>")
85-
let attachment = Test.Attachment(attachableValue, named: "loremipsum.tar.gz.gif.jpeg.html")
85+
let attachment = Test.Attachment(attachableValue, named: "loremipsum.tgz.gif.jpeg.html")
8686

8787
// Write the attachment to disk once to ensure the original filename is not
8888
// available and we add a suffix.
@@ -98,7 +98,7 @@ struct AttachmentTests {
9898
remove(filePath)
9999
}
100100
let fileName = try #require(filePath.split { $0 == "/" || $0 == #"\"# }.last)
101-
#expect(fileName == "loremipsum-\(suffix).tar.gz.gif.jpeg.html")
101+
#expect(fileName == "loremipsum-\(suffix).tgz.gif.jpeg.html")
102102
try compare(attachableValue, toContentsOfFileAtPath: filePath)
103103
}
104104

@@ -270,7 +270,7 @@ struct AttachmentTests {
270270
return
271271
}
272272

273-
#expect(attachment.preferredName == "\(temporaryDirectoryName).tar.gz")
273+
#expect(attachment.preferredName == "\(temporaryDirectoryName).tgz")
274274
valueAttached()
275275
}
276276

0 commit comments

Comments
 (0)