mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-06 12:31:51 +02:00
Rename FileSystem to OSFileManager
This commit is contained in:
parent
81393b67da
commit
1eb496f343
6 changed files with 58 additions and 58 deletions
|
@ -19,7 +19,7 @@
|
|||
2EBF4B592122B598008E4117 /* Logrotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B562122B598008E4117 /* Logrotate.swift */; };
|
||||
2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D621329CA30058EEFC /* LoggetTests.swift */; };
|
||||
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */; };
|
||||
2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077DA2132B0320058EEFC /* FileSystem.swift */; };
|
||||
2ED077DB2132B0320058EEFC /* OSFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077DA2132B0320058EEFC /* OSFileManager.swift */; };
|
||||
2ED103E12135C61100EB3683 /* LogrotateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E02135C61100EB3683 /* LogrotateTests.swift */; };
|
||||
2ED103E32135D3FB00EB3683 /* SizeLimitedFileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */; };
|
||||
2ED103E52138553B00EB3683 /* DiskLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */; };
|
||||
|
@ -62,7 +62,7 @@
|
|||
2EBF4B562122B598008E4117 /* Logrotate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logrotate.swift; sourceTree = "<group>"; };
|
||||
2ED077D621329CA30058EEFC /* LoggetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggetTests.swift; sourceTree = "<group>"; };
|
||||
2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgregateLoggerTests.swift; sourceTree = "<group>"; };
|
||||
2ED077DA2132B0320058EEFC /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = "<group>"; };
|
||||
2ED077DA2132B0320058EEFC /* OSFileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSFileManager.swift; sourceTree = "<group>"; };
|
||||
2ED103E02135C61100EB3683 /* LogrotateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogrotateTests.swift; sourceTree = "<group>"; };
|
||||
2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizeLimitedFileTests.swift; sourceTree = "<group>"; };
|
||||
2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiskLoggerTests.swift; sourceTree = "<group>"; };
|
||||
|
@ -148,7 +148,7 @@
|
|||
2EBF4B542122B598008E4117 /* DiskLogger.swift */,
|
||||
2EBF4B552122B598008E4117 /* SizeLimitedFile.swift */,
|
||||
2EBF4B562122B598008E4117 /* Logrotate.swift */,
|
||||
2ED077DA2132B0320058EEFC /* FileSystem.swift */,
|
||||
2ED077DA2132B0320058EEFC /* OSFileManager.swift */,
|
||||
);
|
||||
path = DiskLogger;
|
||||
sourceTree = "<group>";
|
||||
|
@ -263,7 +263,7 @@
|
|||
2EBF4B4C2122AF53008E4117 /* NullLogger.swift in Sources */,
|
||||
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */,
|
||||
2EBF4B4B2122AF53008E4117 /* ConsoleLogger.swift in Sources */,
|
||||
2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */,
|
||||
2ED077DB2132B0320058EEFC /* OSFileManager.swift in Sources */,
|
||||
2EBF4B4A2122AF53008E4117 /* AgregateLogger.swift in Sources */,
|
||||
2EBF4B592122B598008E4117 /* Logrotate.swift in Sources */,
|
||||
2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */,
|
||||
|
|
|
@ -51,7 +51,7 @@ public final class DiskLogger: Logger {
|
|||
private let fileURL: URL
|
||||
private let fileSizeLimit: UInt64
|
||||
private let rotations: Int
|
||||
private let fileSystem: FileSystem
|
||||
private let fileManager: OSFileManager
|
||||
private let sizeLimitedFileFactory: SizeLimitedFileFactory
|
||||
private let logrotateFactory: LogrotateFactory
|
||||
private let formatter: DateFormatter
|
||||
|
@ -66,14 +66,14 @@ public final class DiskLogger: Logger {
|
|||
/// - fileSizeLimit: Maximum size log file can reach in bytes. Attempt to exceeding that limit triggers log files rotation.
|
||||
/// - rotations: Number of times log files are rotated before being removed.
|
||||
public convenience init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int) {
|
||||
self.init(fileURL: fileURL, fileSizeLimit: fileSizeLimit, rotations: rotations, fileSystem: FileManager.default, sizeLimitedFileFactory: FileWriterFactory(), logrotateFactory: FileRotateFactory())
|
||||
self.init(fileURL: fileURL, fileSizeLimit: fileSizeLimit, rotations: rotations, fileManager: FileManager.default, sizeLimitedFileFactory: FileWriterFactory(), logrotateFactory: FileRotateFactory())
|
||||
}
|
||||
|
||||
init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileSystem: FileSystem, sizeLimitedFileFactory: SizeLimitedFileFactory, logrotateFactory: LogrotateFactory) {
|
||||
init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileManager: OSFileManager, sizeLimitedFileFactory: SizeLimitedFileFactory, logrotateFactory: LogrotateFactory) {
|
||||
self.fileURL = fileURL
|
||||
self.fileSizeLimit = fileSizeLimit
|
||||
self.rotations = rotations
|
||||
self.fileSystem = fileSystem
|
||||
self.fileManager = fileManager
|
||||
self.sizeLimitedFileFactory = sizeLimitedFileFactory
|
||||
self.logrotateFactory = logrotateFactory
|
||||
formatter = DateFormatter()
|
||||
|
@ -113,8 +113,8 @@ public final class DiskLogger: Logger {
|
|||
|
||||
private func openSizeLimitedFile() throws {
|
||||
guard sizeLimitedFile == nil else { return }
|
||||
if fileSystem.itemExists(at: fileURL) == false {
|
||||
_ = fileSystem.createFile(at: fileURL)
|
||||
if fileManager.itemExists(at: fileURL) == false {
|
||||
_ = fileManager.createFile(at: fileURL)
|
||||
}
|
||||
sizeLimitedFile = try sizeLimitedFileFactory.makeInstance(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ public final class DiskLogger: Logger {
|
|||
|
||||
private class FileRotateFactory: LogrotateFactory {
|
||||
func makeInstance(fileURL: URL, rotations: Int) -> Logrotate {
|
||||
return LogrotateImpl(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default)
|
||||
return LogrotateImpl(fileURL: fileURL, rotations: rotations, fileManager: FileManager.default)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,18 +42,18 @@ final class LogrotateImpl {
|
|||
|
||||
private let fileURL: URL
|
||||
private let rotations: Int
|
||||
private let fileSystem: FileSystem
|
||||
private let fileManager: OSFileManager
|
||||
|
||||
/// Initializes new Logrotate instance.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - fileURL: URL of the log file.
|
||||
/// - rotations: Number of times log files are rotated before being removed.
|
||||
init(fileURL: URL, rotations: Int, fileSystem: FileSystem) {
|
||||
init(fileURL: URL, rotations: Int, fileManager: OSFileManager) {
|
||||
precondition(rotations > 0)
|
||||
self.fileURL = fileURL
|
||||
self.rotations = rotations
|
||||
self.fileSystem = fileSystem
|
||||
self.fileManager = fileManager
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,12 +68,12 @@ extension LogrotateImpl: Logrotate {
|
|||
let toDelete = rotatedURLs.last!
|
||||
let toMove = zip(allURLs, rotatedURLs).reversed()
|
||||
|
||||
if fileSystem.itemExists(at: toDelete) {
|
||||
try fileSystem.removeItem(at: toDelete)
|
||||
if fileManager.itemExists(at: toDelete) {
|
||||
try fileManager.removeItem(at: toDelete)
|
||||
}
|
||||
for (oldURL, newURL) in toMove {
|
||||
guard fileSystem.itemExists(at: oldURL) else { continue }
|
||||
try fileSystem.moveItem(at: oldURL, to: newURL)
|
||||
guard fileManager.itemExists(at: oldURL) else { continue }
|
||||
try fileManager.moveItem(at: oldURL, to: newURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,14 +24,14 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
public protocol FileSystem {
|
||||
public protocol OSFileManager {
|
||||
func itemExists(at URL: URL) -> Bool
|
||||
func removeItem(at URL: URL) throws
|
||||
func moveItem(at srcURL: URL, to dstURL: URL) throws
|
||||
func createFile(at URL: URL) -> Bool
|
||||
}
|
||||
|
||||
extension FileManager: FileSystem {
|
||||
extension FileManager: OSFileManager {
|
||||
|
||||
public func itemExists(at URL: URL) -> Bool {
|
||||
return fileExists(atPath: URL.path)
|
|
@ -33,13 +33,13 @@ class DiskLoggerTests: XCTestCase {
|
|||
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||
expectation.expectedFulfillmentCount = 1
|
||||
|
||||
let filesystem = FileSystemStub()
|
||||
let fimeManager = FileManagerStub()
|
||||
let sizeLimitedFileFactory = SizeLimitedFileMockFactory(writeCall: expectation)
|
||||
let logrotateFactory = LogrotateMockFactory()
|
||||
let logger = DiskLogger(fileURL: logURL,
|
||||
fileSizeLimit: 1024,
|
||||
rotations: 1,
|
||||
fileSystem: filesystem,
|
||||
fileManager: fimeManager,
|
||||
sizeLimitedFileFactory: sizeLimitedFileFactory,
|
||||
logrotateFactory: logrotateFactory)
|
||||
|
||||
|
@ -61,13 +61,13 @@ class DiskLoggerTests: XCTestCase {
|
|||
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||
expectation.expectedFulfillmentCount = 2
|
||||
|
||||
let filesystem = FileSystemStub()
|
||||
let fimeManager = FileManagerStub()
|
||||
let sizeLimitedFileFactory = SizeLimitedFileMockFactory(writeCall: expectation)
|
||||
let logrotateFactory = LogrotateMockFactory()
|
||||
let logger = DiskLogger(fileURL: logURL,
|
||||
fileSizeLimit: 91,
|
||||
rotations: 1,
|
||||
fileSystem: filesystem,
|
||||
fileManager: fimeManager,
|
||||
sizeLimitedFileFactory: sizeLimitedFileFactory,
|
||||
logrotateFactory: logrotateFactory)
|
||||
|
||||
|
@ -90,13 +90,13 @@ class DiskLoggerTests: XCTestCase {
|
|||
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||
expectation.expectedFulfillmentCount = 2
|
||||
|
||||
let filesystem = FileSystemStub()
|
||||
let fimeManager = FileManagerStub()
|
||||
let sizeLimitedFileFactory = UnwritableFileStubFactory(writeCall: expectation)
|
||||
let logrotateFactory = LogrotateMockFactory()
|
||||
let logger = DiskLogger(fileURL: logURL,
|
||||
fileSizeLimit: 91,
|
||||
rotations: 1,
|
||||
fileSystem: filesystem,
|
||||
fileManager: fimeManager,
|
||||
sizeLimitedFileFactory: sizeLimitedFileFactory,
|
||||
logrotateFactory: logrotateFactory)
|
||||
|
||||
|
@ -116,7 +116,7 @@ class DiskLoggerTests: XCTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private class FileSystemStub: FileSystem {
|
||||
private class FileManagerStub: OSFileManager {
|
||||
func itemExists(at URL: URL) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -33,98 +33,98 @@ class LogrotateTests: XCTestCase {
|
|||
let log3URL = URL(fileURLWithPath: "/var/log/application.log.3")
|
||||
|
||||
func test_1rotation_0files() {
|
||||
let fileSystem = FileSystemMock(files: [])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>()
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_1rotation_1file() {
|
||||
let fileSystem = FileSystemMock(files: [logURL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_1rotation_2files() {
|
||||
let fileSystem = FileSystemMock(files: [logURL, log1URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL, log1URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_1rotation_3files() {
|
||||
let fileSystem = FileSystemMock(files: [logURL, log1URL, log2URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL, log1URL, log2URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL, log2URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_2rotations_0files() {
|
||||
let fileSystem = FileSystemMock(files: [])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>()
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_2rotations_1file() {
|
||||
let fileSystem = FileSystemMock(files: [logURL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_2rotations_2files() {
|
||||
let fileSystem = FileSystemMock(files: [logURL, log1URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL, log1URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL, log2URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_2rotations_3files() {
|
||||
let fileSystem = FileSystemMock(files: [logURL, log1URL, log2URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL, log1URL, log2URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL, log2URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_2rotations_4files() {
|
||||
let fileSystem = FileSystemMock(files: [logURL, log1URL, log2URL, log3URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||
let fimeManager = FileManagerMock(files: [logURL, log1URL, log2URL, log3URL])
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 2, fileManager: fimeManager)
|
||||
try? logrotate.rotate()
|
||||
|
||||
let actual = fileSystem.files
|
||||
let actual = fimeManager.files
|
||||
let expected = Set<URL>([log1URL, log2URL, log3URL])
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func testErrorPropagation() {
|
||||
let fileSystem = BrokenFileSystem()
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||
let fimeManager = BrokenFileSystem()
|
||||
let logrotate = LogrotateImpl(fileURL: logURL, rotations: 1, fileManager: fimeManager)
|
||||
|
||||
XCTAssertThrowsError(try logrotate.rotate(), "An error when removing or moving an item") { (error) in
|
||||
XCTAssertTrue(error is BrokenFileSystem.IOError)
|
||||
|
@ -132,7 +132,7 @@ class LogrotateTests: XCTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private class FileSystemMock: FileSystem {
|
||||
private class FileManagerMock: OSFileManager {
|
||||
|
||||
private(set) var files = Set<URL>()
|
||||
|
||||
|
@ -159,7 +159,7 @@ private class FileSystemMock: FileSystem {
|
|||
}
|
||||
}
|
||||
|
||||
private class BrokenFileSystem: FileSystem {
|
||||
private class BrokenFileSystem: OSFileManager {
|
||||
|
||||
struct IOError: Error { }
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue