mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Added FileRotateTests
This commit is contained in:
parent
ab7ec33418
commit
9253166f1d
2 changed files with 185 additions and 0 deletions
|
@ -21,6 +21,7 @@
|
||||||
2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D621329CA30058EEFC /* LoggetTests.swift */; };
|
2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D621329CA30058EEFC /* LoggetTests.swift */; };
|
||||||
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */; };
|
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */; };
|
||||||
2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077DA2132B0320058EEFC /* FileSystem.swift */; };
|
2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077DA2132B0320058EEFC /* FileSystem.swift */; };
|
||||||
|
2ED103E12135C61100EB3683 /* FileRotateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E02135C61100EB3683 /* FileRotateTests.swift */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXContainerItemProxy section */
|
/* Begin PBXContainerItemProxy section */
|
||||||
|
@ -64,6 +65,7 @@
|
||||||
2ED077D621329CA30058EEFC /* LoggetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggetTests.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>"; };
|
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 /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = "<group>"; };
|
||||||
|
2ED103E02135C61100EB3683 /* FileRotateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileRotateTests.swift; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
|
@ -91,6 +93,7 @@
|
||||||
2E58D35C21316C3500BEF81A /* LogStringConvertibleTests.swift */,
|
2E58D35C21316C3500BEF81A /* LogStringConvertibleTests.swift */,
|
||||||
2ED077D621329CA30058EEFC /* LoggetTests.swift */,
|
2ED077D621329CA30058EEFC /* LoggetTests.swift */,
|
||||||
2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */,
|
2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */,
|
||||||
|
2ED103E02135C61100EB3683 /* FileRotateTests.swift */,
|
||||||
2E58D35E21316C3500BEF81A /* Info.plist */,
|
2E58D35E21316C3500BEF81A /* Info.plist */,
|
||||||
);
|
);
|
||||||
path = UnitTests;
|
path = UnitTests;
|
||||||
|
@ -241,6 +244,7 @@
|
||||||
files = (
|
files = (
|
||||||
2E58D35D21316C3500BEF81A /* LogStringConvertibleTests.swift in Sources */,
|
2E58D35D21316C3500BEF81A /* LogStringConvertibleTests.swift in Sources */,
|
||||||
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */,
|
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */,
|
||||||
|
2ED103E12135C61100EB3683 /* FileRotateTests.swift in Sources */,
|
||||||
2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */,
|
2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
|
181
UnitTests/FileRotateTests.swift
Normal file
181
UnitTests/FileRotateTests.swift
Normal file
|
@ -0,0 +1,181 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2018 Wojciech Nagrodzki
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
|
||||||
|
import XCTest
|
||||||
|
@testable import Logger
|
||||||
|
|
||||||
|
class FileRotateTests: XCTestCase {
|
||||||
|
|
||||||
|
let logURL = URL(fileURLWithPath: "/var/log/application.log")
|
||||||
|
let log1URL = URL(fileURLWithPath: "/var/log/application.log.1")
|
||||||
|
let log2URL = URL(fileURLWithPath: "/var/log/application.log.2")
|
||||||
|
let log3URL = URL(fileURLWithPath: "/var/log/application.log.3")
|
||||||
|
|
||||||
|
func test_1rotation_0files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>()
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_1rotation_1file() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_1rotation_2files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL, log1URL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_1rotation_3files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL, log1URL, log2URL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL, log2URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_2rotations_0files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>()
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_2rotations_1file() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_2rotations_2files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL, log1URL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL, log2URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_2rotations_3files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL, log1URL, log2URL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL, log2URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_2rotations_4files() {
|
||||||
|
let fileSystem = FileSystemMock(files: [logURL, log1URL, log2URL, log3URL])
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 2, fileSystem: fileSystem)
|
||||||
|
try? logrotate.rotate()
|
||||||
|
|
||||||
|
let actual = fileSystem.files
|
||||||
|
let expected = Set<URL>([log1URL, log2URL, log3URL])
|
||||||
|
XCTAssertEqual(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testErrorPropagation() {
|
||||||
|
let fileSystem = BrokenFileSystem()
|
||||||
|
let logrotate = FileRotate(fileURL: logURL, rotations: 1, fileSystem: fileSystem)
|
||||||
|
|
||||||
|
XCTAssertThrowsError(try logrotate.rotate(), "An error when removing or moving an item") { (error) in
|
||||||
|
XCTAssertTrue(error is BrokenFileSystem.IOError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FileSystemMock: FileSystem {
|
||||||
|
|
||||||
|
private(set) var files = Set<URL>()
|
||||||
|
|
||||||
|
init(files: Set<URL>) {
|
||||||
|
self.files = files
|
||||||
|
}
|
||||||
|
|
||||||
|
func itemExists(at URL: URL) -> Bool {
|
||||||
|
return files.contains(URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeItem(at URL: URL) throws {
|
||||||
|
files.remove(URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func moveItem(at srcURL: URL, to dstURL: URL) throws {
|
||||||
|
files.remove(srcURL)
|
||||||
|
files.insert(dstURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFile(at URL: URL) -> Bool {
|
||||||
|
files.insert(URL)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class BrokenFileSystem: FileSystem {
|
||||||
|
|
||||||
|
struct IOError: Error { }
|
||||||
|
|
||||||
|
func itemExists(at URL: URL) -> Bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeItem(at URL: URL) throws {
|
||||||
|
throw IOError()
|
||||||
|
}
|
||||||
|
|
||||||
|
func moveItem(at srcURL: URL, to dstURL: URL) throws {
|
||||||
|
throw IOError()
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFile(at URL: URL) -> Bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue