Skip to content

Commit 5d4ce3e

Browse files
committed
Move Files and Folder types into test utility target
1 parent 2566321 commit 5d4ce3e

22 files changed

+92
-56
lines changed

Sources/SwiftDocC/Utility/DataStructures/File.swift renamed to Sources/SwiftDocCTestUtilities/FilesAndFolders.swift

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import Foundation
1616
*/
1717

1818
/// An abstract representation of a file (or folder).
19-
protocol File {
19+
public protocol File {
2020
/// The name of the file.
2121
var name: String { get }
2222

2323
/// Writes the file to a given URL.
2424
func write(to url: URL) throws
2525
}
2626

27-
extension File {
27+
public extension File {
2828
/// Writes the file inside of a folder and returns the URL that it was written to.
2929
@discardableResult
3030
func write(inside url: URL) throws -> URL {
@@ -35,12 +35,12 @@ extension File {
3535
}
3636

3737
/// An item which provides data.
38-
protocol DataRepresentable {
38+
public protocol DataRepresentable {
3939
func data() throws -> Data
4040
}
4141

4242
/// `DataRepresentable` can automatically write itself to disk via `Data.write(to:)`
43-
extension DataRepresentable {
43+
public extension DataRepresentable {
4444
func write(to url: URL) throws {
4545
try data().write(to: url)
4646
}
@@ -49,17 +49,22 @@ extension DataRepresentable {
4949
// MARK: -
5050

5151
/// An abstract representation of a folder, containing some files or folders.
52-
struct Folder: File {
53-
let name: String
52+
public struct Folder: File {
53+
public init(name: String, content: [File]) {
54+
self.name = name
55+
self.content = content
56+
}
57+
58+
public let name: String
5459

5560
/// The files and sub folders that this folder contains.
56-
let content: [File]
61+
public let content: [File]
5762

58-
func appendingFile(_ newFile: File) -> Folder {
63+
public func appendingFile(_ newFile: File) -> Folder {
5964
return Folder(name: name, content: content + [newFile])
6065
}
6166

62-
func write(to url: URL) throws {
67+
public func write(to url: URL) throws {
6368
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
6469
for file in content {
6570
try file.write(inside: url)
@@ -69,7 +74,7 @@ struct Folder: File {
6974

7075
extension Folder {
7176
/// Returns a flat list of a folder's recursive listing for testing purposes.
72-
var recursiveContent: [File] {
77+
public var recursiveContent: [File] {
7378
var result = content
7479
for file in content {
7580
if let content = (file as? Folder)?.recursiveContent {
@@ -81,13 +86,13 @@ extension Folder {
8186
}
8287

8388
/// A representation of an Info.plist file.
84-
struct InfoPlist: File, DataRepresentable {
85-
let name = "Info.plist"
89+
public struct InfoPlist: File, DataRepresentable {
90+
public let name = "Info.plist"
8691

8792
/// The information that the Into.plist file contains.
88-
let content: Content
93+
public let content: Content
8994

90-
init(displayName: String, identifier: String, versionString: String = "1.0", developmentRegion: String = "en") {
95+
public init(displayName: String, identifier: String, versionString: String = "1.0", developmentRegion: String = "en") {
9196
self.content = Content(
9297
displayName: displayName,
9398
identifier: identifier,
@@ -96,11 +101,11 @@ struct InfoPlist: File, DataRepresentable {
96101
)
97102
}
98103

99-
struct Content: Codable, Equatable {
100-
let displayName: String
101-
let identifier: String
102-
let versionString: String
103-
let developmentRegion: String
104+
public struct Content: Codable, Equatable {
105+
public let displayName: String
106+
public let identifier: String
107+
public let versionString: String
108+
public let developmentRegion: String
104109

105110
fileprivate init(displayName: String, identifier: String, versionString: String, developmentRegion: String) {
106111
self.displayName = displayName
@@ -117,7 +122,7 @@ struct InfoPlist: File, DataRepresentable {
117122
}
118123
}
119124

120-
func data() throws -> Data {
125+
public func data() throws -> Data {
121126
// TODO: Replace this with PropertListEncoder (see below) when it's available in swift-corelibs-foundation
122127
// https://github.com/apple/swift-corelibs-foundation/commit/d2d72f88d93f7645b94c21af88a7c9f69c979e4f
123128
let infoPlist = [
@@ -136,74 +141,85 @@ struct InfoPlist: File, DataRepresentable {
136141
}
137142

138143
/// A representation of a text file with some UTF-8 content.
139-
struct TextFile: File, DataRepresentable {
140-
let name: String
144+
public struct TextFile: File, DataRepresentable {
145+
public init(name: String, utf8Content: String) {
146+
self.name = name
147+
self.utf8Content = utf8Content
148+
}
149+
150+
public let name: String
141151

142152
/// The UTF8 content of the file.
143-
let utf8Content: String
153+
public let utf8Content: String
144154

145-
func data() throws -> Data {
155+
public func data() throws -> Data {
146156
return utf8Content.data(using: .utf8)!
147157
}
148158
}
149159

150160
/// A representation of a text file with some UTF-8 content.
151-
struct JSONFile<Content: Codable>: File, DataRepresentable {
152-
let name: String
161+
public struct JSONFile<Content: Codable>: File, DataRepresentable {
162+
public init(name: String, content: Content) {
163+
self.name = name
164+
self.content = content
165+
}
166+
167+
public let name: String
153168

154169
/// The UTF8 content of the file.
155-
let content: Content
170+
public let content: Content
156171

157-
func data() throws -> Data {
172+
public func data() throws -> Data {
158173
return try JSONEncoder().encode(content)
159174
}
160175
}
161176

162177
/// A copy of another file on disk somewhere.
163-
struct CopyOfFile: File, DataRepresentable {
164-
enum Error: DescribedError {
178+
public struct CopyOfFile: File, DataRepresentable {
179+
enum Error: LocalizedError {
165180
case notAFile(URL)
166181
var errorDescription: String {
167182
switch self {
168-
case .notAFile(let url): return "Original url is not a file: \(url.path.singleQuoted)"
183+
case .notAFile(let url): return "Original url is not a file: '\(url.path)'"
169184
}
170185
}
171186
}
172187

173188
/// The original file.
174-
let original: URL
175-
let name: String
189+
public let original: URL
190+
public let name: String
176191

177-
init(original: URL, newName: String? = nil) {
192+
public init(original: URL, newName: String? = nil) {
178193
self.original = original
179194
self.name = newName ?? original.lastPathComponent
180195
}
181196

182-
func data() throws -> Data {
197+
public func data() throws -> Data {
183198
// Note that `CopyOfFile` always reads a file from disk and so it's okay
184199
// to use `FileManager.default` directly here instead of `FileManagerProtocol`.
185-
guard !FileManager.default.directoryExists(atPath: original.path) else { throw Error.notAFile(original) }
200+
var isDirectory: ObjCBool = false
201+
guard FileManager.default.fileExists(atPath: original.path, isDirectory: &isDirectory), !isDirectory.boolValue else { throw Error.notAFile(original) }
186202
return try Data(contentsOf: original)
187203
}
188204

189-
func write(to url: URL) throws {
205+
public func write(to url: URL) throws {
190206
try FileManager.default.copyItem(at: original, to: url)
191207
}
192208
}
193209

194-
struct CopyOfFolder: File {
210+
public struct CopyOfFolder: File {
195211
/// The original file.
196212
let original: URL
197-
let name: String
213+
public let name: String
198214
let shouldCopyFile: (URL) -> Bool
199215

200-
init(original: URL, newName: String? = nil, filter shouldCopyFile: @escaping (URL) -> Bool = { _ in true }) {
216+
public init(original: URL, newName: String? = nil, filter shouldCopyFile: @escaping (URL) -> Bool = { _ in true }) {
201217
self.original = original
202218
self.name = newName ?? original.lastPathComponent
203219
self.shouldCopyFile = shouldCopyFile
204220
}
205221

206-
func write(to url: URL) throws {
222+
public func write(to url: URL) throws {
207223
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
208224
for filePath in try FileManager.default.contentsOfDirectory(atPath: original.path) {
209225
// `contentsOfDirectory(atPath)` includes hidden files, skipHiddenFiles option doesn't help on Linux.
@@ -217,37 +233,37 @@ struct CopyOfFolder: File {
217233
}
218234

219235
/// A file backed by `Data`.
220-
struct DataFile: File, DataRepresentable {
221-
var name: String
236+
public struct DataFile: File, DataRepresentable {
237+
public var name: String
222238
var _data: Data
223239

224-
init(name: String, data: Data) {
240+
public init(name: String, data: Data) {
225241
self.name = name
226242
self._data = data
227243
}
228244

229-
func data() throws -> Data {
245+
public func data() throws -> Data {
230246
return _data
231247
}
232248
}
233249

234250
/// A temporary folder which can write files to a temporary location on disk and
235251
/// will delete itself when its instance is released from memory.
236-
class TempFolder: File {
237-
let name: String
238-
let url: URL
252+
public class TempFolder: File {
253+
public let name: String
254+
public let url: URL
239255

240256
/// The files and sub folders that this folder contains.
241257
let content: [File]
242258

243-
func write(to url: URL) throws {
259+
public func write(to url: URL) throws {
244260
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
245261
for file in content {
246262
try file.write(inside: url)
247263
}
248264
}
249265

250-
init(content: [File], atRoot root: URL) throws {
266+
public init(content: [File], atRoot root: URL) throws {
251267
self.content = content
252268

253269
url = root

Tests/SwiftDocCTests/Infrastructure/BundleDiscoveryTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import XCTest
1212
@testable import SwiftDocC
13+
import SwiftDocCTestUtilities
1314

1415
class BundleDiscoveryTests: XCTestCase {
1516

Tests/SwiftDocCTests/Infrastructure/DocumentationContext+RootPageTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import XCTest
1212
import SymbolKit
1313
@testable import SwiftDocC
14+
import SwiftDocCTestUtilities
1415

1516
class DocumentationContext_RootPageTests: XCTestCase {
1617
func testNoSGFBundle() throws {

Tests/SwiftDocCTests/Infrastructure/DocumentationContextTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import XCTest
1212
import SymbolKit
1313
@testable import SwiftDocC
1414
import Markdown
15+
import SwiftDocCTestUtilities
1516

1617
func diffDescription(lhs: String, rhs: String) -> String {
1718
let leftLines = lhs.components(separatedBy: .newlines)

Tests/SwiftDocCTests/Infrastructure/NodeTagsTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import XCTest
1212
@testable import SwiftDocC
13+
import SwiftDocCTestUtilities
1314

1415
class NodeTagsTests: XCTestCase {
1516
func testSPIMetadata() throws {

Tests/SwiftDocCTests/LinkTargets/LinkDestinationSummaryTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import XCTest
1212
@testable import SwiftDocC
13+
import SwiftDocCTestUtilities
1314

1415
class ExternalLinkableTests: XCTestCase {
1516

Tests/SwiftDocCTests/Semantics/SymbolTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import XCTest
1212
@testable import SymbolKit
1313
@testable import SwiftDocC
1414
import Markdown
15+
import SwiftDocCTestUtilities
1516

1617
class SymbolTests: XCTestCase {
1718

Tests/SwiftDocCUtilitiesTests/ConvertActionStaticHostableTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import XCTest
1212
import Foundation
1313
@testable import SwiftDocC
1414
@testable import SwiftDocCUtilities
15+
import SwiftDocCTestUtilities
1516

1617
class ConvertActionStaticHostableTests: StaticHostingBaseTests {
1718
/// Creates a DocC archive and then archives it with options to produce static content which is then validated.

Tests/SwiftDocCUtilitiesTests/ConvertActionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,8 +2467,8 @@ extension Folder {
24672467
if fileURL.lastPathComponent == "Info.plist",
24682468
let infoPlistData = FileManager.default.contents(atPath: fileURL.path),
24692469
let infoPlist = try? PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: Any],
2470-
let displayName = infoPlist[InfoPlist.Content.CodingKeys.displayName.rawValue] as? String,
2471-
let identifier = infoPlist[InfoPlist.Content.CodingKeys.identifier.rawValue] as? String {
2470+
let displayName = infoPlist["CFBundleDisplayName"] as? String,
2471+
let identifier = infoPlist["CFBundleIdentifier"] as? String {
24722472
content.append(InfoPlist(displayName: displayName, identifier: identifier))
24732473
} else {
24742474
content.append(CopyOfFile(original: fileURL, newName: fileURL.lastPathComponent))

Tests/SwiftDocCUtilitiesTests/FolderStructure.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
@testable import SwiftDocC
1212
@testable import SwiftDocCUtilities
1313
import XCTest
14+
import SwiftDocCTestUtilities
1415

1516
/*
1617
This file contains a test helper API for working with folder hierarchies, with the ability to:
@@ -69,10 +70,10 @@ extension InfoPlist: AssertableFile {
6970
}
7071
let infoPlist = try PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: String]
7172

72-
let displayName = infoPlist?[Content.CodingKeys.displayName.rawValue]
73-
let identifier = infoPlist?[Content.CodingKeys.identifier.rawValue]
74-
let versionString = infoPlist?[Content.CodingKeys.versionString.rawValue]
75-
let developmentRegion = infoPlist?[Content.CodingKeys.developmentRegion.rawValue]
73+
let displayName = infoPlist?["CFBundleIdentifier"]
74+
let identifier = infoPlist?["CFBundleVersion"]
75+
let versionString = infoPlist?["CFBundleDevelopmentRegion"]
76+
let developmentRegion = infoPlist?["CFBundleDisplayName"]
7677

7778
XCTAssert(displayName == content.displayName && identifier == content.identifier && versionString == content.versionString && developmentRegion == content.developmentRegion,
7879
"File '\(name)' should contain the correct information.", file: (file), line: line)

Tests/SwiftDocCUtilitiesTests/FolderStructureTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import XCTest
1212
@testable import SwiftDocC
13+
import SwiftDocCTestUtilities
1314

1415
class FolderStructureTests: XCTestCase {
1516

Tests/SwiftDocCUtilitiesTests/HTMLTemplateDirectory.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import Foundation
1212
@testable import SwiftDocC
13+
import SwiftDocCTestUtilities
1314

1415
/// A folder that represents a fake html-build directory for testing.
1516
extension Folder {

Tests/SwiftDocCUtilitiesTests/PreviewActionIntegrationTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import XCTest
1212
@testable import SwiftDocC
1313
@testable import SwiftDocCUtilities
14+
import SwiftDocCTestUtilities
1415

1516
class PreviewActionIntegrationTests: XCTestCase {
1617
func json(contentsOf url: URL) throws -> [String: Any] {

Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewHTTPHandlerTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Foundation
1212
import XCTest
1313
@testable import SwiftDocC
1414
@testable import SwiftDocCUtilities
15+
import SwiftDocCTestUtilities
1516

1617
import NIO
1718
import NIOHTTP1

Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Foundation
1313

1414
@testable import SwiftDocCUtilities
1515
@testable import SwiftDocC
16+
import SwiftDocCTestUtilities
1617

1718
@testable import NIO
1819
@testable import NIOHTTP1

0 commit comments

Comments
 (0)