-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFileHandlerTests.swift
More file actions
193 lines (155 loc) · 6.01 KB
/
FileHandlerTests.swift
File metadata and controls
193 lines (155 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import Foundation
import Logging
import Testing
@testable import xcodeinstall
// MARK: - File Handler Tests
@Suite("FileHandlerTests", .serialized)
struct FileHandlerTests {
// MARK: - Test Environment
let log: Logger = Logger(label: "FileHandlerTests")
var fileManager: FileManager
let test_data: String = "test data éèà€ 🎧"
init() {
self.fileManager = FileManager.default
}
// MARK: - Helper Methods
private func createSrcFile(inDirectory tempDirectory: URL) throws -> URL {
let srcFile: URL = tempDirectory.appendingPathComponent("temp.txt")
let _ = fileManager.createFile(atPath: srcFile.path, contents: test_data.data(using: .utf8))
return srcFile
}
}
// MARK: - Test Cases
extension FileHandlerTests {
@Test("Test Moving Files Successfully")
func testMoveSucceed() async throws {
try withTemporaryDirectory { url in
// Given
let srcFile = try createSrcFile(inDirectory: url)
// When
let dstFile: URL = url.appendingPathComponent("temp2.txt")
let fh = FileHandler(log: log)
try fh.move(from: srcFile, to: dstFile)
// Then
// srcFile does not exist
#expect(!fileManager.fileExists(atPath: srcFile.path))
// dstFile exists
#expect(fileManager.fileExists(atPath: dstFile.path))
// dstFile contains "test data"
let data: String = try String(contentsOf: dstFile, encoding: .utf8)
#expect(data == test_data)
// Cleanup
try fileManager.removeItem(at: dstFile)
}
}
@Test("Test Moving Files When Destination Already Exists")
func testMoveDstExists() async throws {
try withTemporaryDirectory { url in
// Given
let test_data2: String = "data already exists"
let srcFile = try createSrcFile(inDirectory: url)
// dst exists and has a different content
let dstFile: URL = url.appendingPathComponent("temp2.txt")
let _ = fileManager.createFile(atPath: dstFile.path, contents: test_data2.data(using: .utf8))
// When
let fh = FileHandler(log: log)
try fh.move(from: srcFile, to: dstFile)
// Then
// srcFile does not exist
#expect(!fileManager.fileExists(atPath: srcFile.path))
// dstFile exists
#expect(fileManager.fileExists(atPath: dstFile.path))
// dstFile contains "test data" (overwritten)
let data: String = try String(contentsOf: dstFile, encoding: .utf8)
#expect(data == test_data)
// Cleanup
try fileManager.removeItem(at: dstFile)
}
}
@Test("Test Moving Files with Invalid Destination")
func testMoveDstInvalid() async throws {
try withTemporaryDirectory { url in
// Given
let srcFile = try createSrcFile(inDirectory: url)
// dst file does not exist in an invalid location
let dstFile = URL(fileURLWithPath: "/does_not_exist/tmp.txt")
// When/Then
let fh = FileHandler(log: log)
do {
try fh.move(from: srcFile, to: dstFile)
Issue.record("Should have thrown an error")
} catch {
// Expected error
#expect(fileManager.fileExists(atPath: srcFile.path))
#expect(!fileManager.fileExists(atPath: dstFile.path))
}
// Cleanup
try? fileManager.removeItem(at: srcFile)
}
}
@Test("Test Checking File Size")
@MainActor
func testCheckFileSize() throws {
try withTemporaryDirectory { url in
// Given
let fileToCheck = try createSrcFile(inDirectory: url)
// When
let fh = FileHandler(log: log)
let expectedFileSize = test_data.data(using: .utf8)?.count
// Then
#expect(expectedFileSize != nil)
if let expectedFileSize = expectedFileSize {
let result = try fh.checkFileSize(file: fileToCheck, fileSize: expectedFileSize)
#expect(result)
}
// Cleanup
try fileManager.removeItem(at: fileToCheck)
}
}
@Test("Test Checking File Size for Non-Existent File")
@MainActor
func testCheckFileSizeNotExist() throws {
// Given
let fileToCheck = URL(fileURLWithPath: "/does_not_exist/tmp.txt")
// When/Then
let fh = FileHandler(log: log)
let error = #expect(throws: FileHandlerError.self) {
_ = try fh.checkFileSize(file: fileToCheck, fileSize: 42)
}
#expect(error == FileHandlerError.fileDoesNotExist)
}
@Test("Test File Exists Check - Positive")
@MainActor
func testFileExistsYes() throws {
try withTemporaryDirectory { url in
// Given
let fileToCheck = try createSrcFile(inDirectory: url)
// When
let fh = FileHandler(log: log)
let expectedFileSize = test_data.data(using: .utf8)?.count
// Then
#expect(expectedFileSize != nil)
if let expectedFileSize = expectedFileSize {
let exists = fh.fileExists(file: fileToCheck, fileSize: expectedFileSize)
#expect(exists)
}
// Cleanup
try fileManager.removeItem(at: fileToCheck)
}
}
@Test("Test File Exists Check - Negative")
@MainActor
func testFileExistsNo() {
// Given
let fileToCheck = URL(fileURLWithPath: "/does_not_exist/tmp.txt")
// When
let fh = FileHandler(log: log)
let expectedFileSize = test_data.data(using: .utf8)?.count
// Then
#expect(expectedFileSize != nil)
if let expectedFileSize = expectedFileSize {
let exists = fh.fileExists(file: fileToCheck, fileSize: expectedFileSize)
#expect(!exists)
}
}
}