Added FileWriterTests

This commit is contained in:
Wojciech Nagrodzki 2018-08-30 18:38:53 +02:00
parent 9253166f1d
commit b224674222
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
2 changed files with 111 additions and 0 deletions

View file

@ -22,6 +22,7 @@
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */; };
2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077DA2132B0320058EEFC /* FileSystem.swift */; };
2ED103E12135C61100EB3683 /* FileRotateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E02135C61100EB3683 /* FileRotateTests.swift */; };
2ED103E32135D3FB00EB3683 /* FileWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -66,6 +67,7 @@
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>"; };
2ED103E02135C61100EB3683 /* FileRotateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileRotateTests.swift; sourceTree = "<group>"; };
2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileWriterTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -93,6 +95,7 @@
2E58D35C21316C3500BEF81A /* LogStringConvertibleTests.swift */,
2ED077D621329CA30058EEFC /* LoggetTests.swift */,
2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */,
2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */,
2ED103E02135C61100EB3683 /* FileRotateTests.swift */,
2E58D35E21316C3500BEF81A /* Info.plist */,
);
@ -243,6 +246,7 @@
buildActionMask = 2147483647;
files = (
2E58D35D21316C3500BEF81A /* LogStringConvertibleTests.swift in Sources */,
2ED103E32135D3FB00EB3683 /* FileWriterTests.swift in Sources */,
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */,
2ED103E12135C61100EB3683 /* FileRotateTests.swift in Sources */,
2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */,

View file

@ -0,0 +1,107 @@
//
// 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 FileWriterTests: XCTestCase {
let logURL = URL(fileURLWithPath: "/var/log/application.log")
func testFileOpeningFailure() {
let factory = UnopenableFileFactory()
XCTAssertThrowsError(try FileWriter(fileURL: logURL, fileSizeLimit: 0, fileFactory: factory), "file open failure") { (error) in
XCTAssertTrue(error is UnopenableFileFactory.OpenFileError)
}
}
func testKeepingFileSizeLimit() throws {
let factory = FileMockFactory()
let writer = try FileWriter(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
let data = Data(bytes: [0])
try writer.write(data)
XCTAssertEqual(factory.mock.writtenData, data)
}
func testExceedingFileSizeLimit() throws {
let factory = FileMockFactory()
let writer = try FileWriter(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
let data = Data(bytes: [0, 0])
XCTAssertThrowsError(try writer.write(data), "file size limit exceeded") { (error) in
XCTAssertTrue(error is SizeLimitedFileQuotaReached)
}
XCTAssertEqual(factory.mock.writtenData.count, 0)
}
func testSynchronizingAndClosingFile() throws {
let factory = FileMockFactory()
let writer = try FileWriter(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
writer.synchronizeAndCloseFile()
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
}
}
private class UnopenableFileFactory: FileFactory {
struct OpenFileError: Error {}
func makeInstance(forWritingTo: URL) throws -> File {
throw OpenFileError()
}
}
private class FileMockFactory: FileFactory {
let mock = FileMock()
func makeInstance(forWritingTo: URL) throws -> File {
return mock
}
}
private class FileMock: File {
private(set) var writtenData = Data()
private(set) var synchronizeFileCallCount = 0
private(set) var closeFileCallCount = 0
func seekToEndOfFile() -> UInt64 {
return 0
}
func swift_write(_ data: Data) throws {
self.writtenData.append(data)
}
func synchronizeFile() {
synchronizeFileCallCount += 1
}
func closeFile() {
closeFileCallCount += 1
}
}