From b224674222ea3114f9411956db960aebc3e64565 Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Thu, 30 Aug 2018 18:38:53 +0200 Subject: [PATCH] Added FileWriterTests --- Logger.xcodeproj/project.pbxproj | 4 ++ UnitTests/FileWriterTests.swift | 107 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 UnitTests/FileWriterTests.swift diff --git a/Logger.xcodeproj/project.pbxproj b/Logger.xcodeproj/project.pbxproj index 076fc31..6a86dba 100644 --- a/Logger.xcodeproj/project.pbxproj +++ b/Logger.xcodeproj/project.pbxproj @@ -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 = ""; }; 2ED077DA2132B0320058EEFC /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = ""; }; 2ED103E02135C61100EB3683 /* FileRotateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileRotateTests.swift; sourceTree = ""; }; + 2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileWriterTests.swift; sourceTree = ""; }; /* 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 */, diff --git a/UnitTests/FileWriterTests.swift b/UnitTests/FileWriterTests.swift new file mode 100644 index 0000000..cb3ebc7 --- /dev/null +++ b/UnitTests/FileWriterTests.swift @@ -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 + } +}