diff --git a/enzevalos_iphone.xcodeproj/project.pbxproj b/enzevalos_iphone.xcodeproj/project.pbxproj index 2bb3f33718060a21611204657bad42a74b8dcc3d..9c90e357dd82e49f8209a66b3b94e08f799a7c5a 100644 --- a/enzevalos_iphone.xcodeproj/project.pbxproj +++ b/enzevalos_iphone.xcodeproj/project.pbxproj @@ -274,9 +274,7 @@ 8428A86F1F436A1E007649A5 /* InviteFriendViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86B1F436A1E007649A5 /* InviteFriendViewController.swift */; }; 8428A8701F436A1E007649A5 /* LinearBadgeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86C1F436A1E007649A5 /* LinearBadgeViewController.swift */; }; 8428A8711F436A1E007649A5 /* GamificationStatusViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86D1F436A1E007649A5 /* GamificationStatusViewController.swift */; }; - 8428A8761F436A9A007649A5 /* GamificationStatusViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86D1F436A1E007649A5 /* GamificationStatusViewController.swift */; }; 8428A8771F436A9D007649A5 /* LinearBadgeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86C1F436A1E007649A5 /* LinearBadgeViewController.swift */; }; - 8428A8781F436AA1007649A5 /* InviteFriendViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86B1F436A1E007649A5 /* InviteFriendViewController.swift */; }; 8428A8791F436AA4007649A5 /* BadgeCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86A1F436A1E007649A5 /* BadgeCase.swift */; }; 8428A87A1F436AA9007649A5 /* ArrowTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A8611F436A11007649A5 /* ArrowTableViewCell.swift */; }; 8428A87B1F436AAC007649A5 /* UserNameGamificationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A8641F436A11007649A5 /* UserNameGamificationTableViewCell.swift */; }; @@ -369,7 +367,12 @@ F1984D721E1D327200804E1E /* IconsStyleKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1984D711E1D327200804E1E /* IconsStyleKit.swift */; }; F1984D741E1E92B300804E1E /* LabelStyleKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1984D731E1E92B300804E1E /* LabelStyleKit.swift */; }; F1ACF21C1E0C1C6800C1B843 /* ContactViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1ACF21B1E0C1C6800C1B843 /* ContactViewController.swift */; }; + F1ADF0751FBDF7D20017AE31 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = A111F6AC1FA77B170060AFDE /* Logger.swift */; }; + F1ADF0761FBDF8120017AE31 /* PersistentMail +CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 472F39281E1277D2009260FB /* PersistentMail +CoreDataClass.swift */; }; + F1ADF0771FBDF9B40017AE31 /* Mail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47691A891ECB56D1004BCFC5 /* Mail.swift */; }; F1AF938F1E2D04BA00755128 /* CustomCells.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1AF938E1E2D04BA00755128 /* CustomCells.swift */; }; + F1E918D71FBDEECB00D60418 /* LoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1E918D61FBDEECA00D60418 /* LoggerTests.swift */; }; + F1E918D81FBDF11200D60418 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = A111F6AC1FA77B170060AFDE /* Logger.swift */; }; F1F070281FA0DF3F004A860A /* Inbox.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F1F070261FA0DF3F004A860A /* Inbox.storyboard */; }; F678F71DFB726D912D039FFF /* Pods_enzevalos_iphoneTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9B9CE43043CF806E1C02FCA /* Pods_enzevalos_iphoneTests.framework */; }; /* End PBXBuildFile section */ @@ -665,6 +668,7 @@ F1984D731E1E92B300804E1E /* LabelStyleKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LabelStyleKit.swift; sourceTree = "<group>"; }; F1ACF21B1E0C1C6800C1B843 /* ContactViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContactViewController.swift; sourceTree = "<group>"; }; F1AF938E1E2D04BA00755128 /* CustomCells.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomCells.swift; sourceTree = "<group>"; }; + F1E918D61FBDEECA00D60418 /* LoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerTests.swift; sourceTree = "<group>"; }; F1F070271FA0DF3F004A860A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Inbox.storyboard; sourceTree = "<group>"; }; /* End PBXFileReference section */ @@ -1109,6 +1113,7 @@ children = ( A13526901D955BE000D3BFE1 /* enzevalos_iphoneTests.swift */, A13526921D955BE000D3BFE1 /* Info.plist */, + F1E918D61FBDEECA00D60418 /* LoggerTests.swift */, ); path = enzevalos_iphoneTests; sourceTree = "<group>"; @@ -1793,6 +1798,7 @@ 3E9708F31FAC95F5005825C9 /* NSMutableArray+PGPUtils.m in Sources */, 3E9708F41FAC95F5005825C9 /* PGPSecretSubKeyPacket.m in Sources */, 3E9708F51FAC95F5005825C9 /* PGPUserIDPacket.m in Sources */, + F1ADF0751FBDF7D20017AE31 /* Logger.swift in Sources */, 3E9708F61FAC95F5005825C9 /* Folder+CoreDataProperties.swift in Sources */, 3E9708F71FAC95F5005825C9 /* PersistentMail +CoreDataClass.swift in Sources */, 3E9708F81FAC95F5005825C9 /* PGPTrustPacket.m in Sources */, @@ -2010,14 +2016,15 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + F1ADF0771FBDF9B40017AE31 /* Mail.swift in Sources */, 8428A8771F436A9D007649A5 /* LinearBadgeViewController.swift in Sources */, 8428A87A1F436AA9007649A5 /* ArrowTableViewCell.swift in Sources */, A13526911D955BE000D3BFE1 /* enzevalos_iphoneTests.swift in Sources */, 8428A87B1F436AAC007649A5 /* UserNameGamificationTableViewCell.swift in Sources */, - 8428A8781F436AA1007649A5 /* InviteFriendViewController.swift in Sources */, - 8428A8761F436A9A007649A5 /* GamificationStatusViewController.swift in Sources */, 8428A8831F436AC9007649A5 /* GamificationDataUnitTest.swift in Sources */, 8428A8811F436AC0007649A5 /* GamificationData.swift in Sources */, + F1ADF0761FBDF8120017AE31 /* PersistentMail +CoreDataClass.swift in Sources */, + F1E918D81FBDF11200D60418 /* Logger.swift in Sources */, 8428A87F1F436AB8007649A5 /* CircleView.swift in Sources */, 8428A87E1F436AB5007649A5 /* BadgeCaseCollectionViewCell.swift in Sources */, 8428A8821F436AC3007649A5 /* Badges.swift in Sources */, @@ -2025,6 +2032,7 @@ 8428A87D1F436AB2007649A5 /* SubBadgeHeaderTableViewCell.swift in Sources */, 8428A87C1F436AAF007649A5 /* SubBadgeTableViewCell.swift in Sources */, 8428A8791F436AA4007649A5 /* BadgeCase.swift in Sources */, + F1E918D71FBDEECB00D60418 /* LoggerTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/enzevalos_iphone.xcodeproj/xcshareddata/xcschemes/enzevalos_iphone.xcscheme b/enzevalos_iphone.xcodeproj/xcshareddata/xcschemes/enzevalos_iphone.xcscheme index 0b0fd8bb7775a1f403d52b58ca3d043516f37e1b..e4c6beb64e4c577bf3845b22f07129ea9ddc9a0e 100644 --- a/enzevalos_iphone.xcodeproj/xcshareddata/xcschemes/enzevalos_iphone.xcscheme +++ b/enzevalos_iphone.xcodeproj/xcshareddata/xcschemes/enzevalos_iphone.xcscheme @@ -38,6 +38,11 @@ BlueprintName = "enzevalos_iphoneTests" ReferencedContainer = "container:enzevalos_iphone.xcodeproj"> </BuildableReference> + <SkippedTests> + <Test + Identifier = "GamificationDataUnitTest"> + </Test> + </SkippedTests> </TestableReference> <TestableReference skipped = "NO"> diff --git a/enzevalos_iphone/Logger.swift b/enzevalos_iphone/Logger.swift index c04ec3bd575f9109bcfa7c3e39df2586ec2e1a81..0ec068b71950b4c8bd54c9a3981a787dc5add194 100644 --- a/enzevalos_iphone/Logger.swift +++ b/enzevalos_iphone/Logger.swift @@ -10,7 +10,7 @@ import Foundation class Logger { - static let fileName = "log.json" + static let defaultFileName = "log.json" static func log(mailSent event: Event, from: String, to: [String], cc: [String], bcc: [String], bodyLength: Int, isEncrypted: Bool, decryptedBodyLength: Int, decryptedWithOldPrivateKey: Bool = false, isSigned: Bool, isCorrectlySigned: Bool = true, signingKeyID: String, myKeyID: String, secureAddresses: [String] = [], encryptedForKeyIDs: [String] = []) { @@ -138,30 +138,34 @@ class Logger { return result } - static func saveToDisk() { - - var json = "some text" + static func saveToDisk(json: String, fileName: String = defaultFileName) { if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { let fileURL = dir.appendingPathComponent(fileName) - // reading - do { - let currentContent = try String(contentsOf: fileURL, encoding: .utf8) - if !currentContent.isEmpty { - json = "\(json)\n\(currentContent)" + if FileManager.default.fileExists(atPath: fileURL.path) { + // append + do { + let fileHandle = try FileHandle(forUpdating: fileURL) + + fileHandle.seekToEndOfFile() + if let encoded = "\n\(json)".data(using: .utf8) { + fileHandle.write(encoded) + } + fileHandle.closeFile() + } + catch { + print("Error while appending to logfile: \(error.localizedDescription)") + } + } else { + // write new + do { + try json.write(to: fileURL, atomically: false, encoding: .utf8) + } + catch { + print("Error while writing logfile: \(error.localizedDescription)") } - } - catch { - print("Error while reading logfile: \(error.localizedDescription)") - } - // writing - do { - try json.write(to: fileURL, atomically: false, encoding: .utf8) - } - catch { - print("Error while writing logfile: \(error.localizedDescription)") } } else { @@ -169,7 +173,7 @@ class Logger { } } - static func sendLog() { + static func sendLog(fileName: String = defaultFileName) { if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { @@ -197,7 +201,7 @@ class Logger { clearLog() } - static func clearLog() { + static func clearLog(fileName: String = defaultFileName) { if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { let fileURL = dir.appendingPathComponent(fileName) diff --git a/enzevalos_iphoneTests/LoggerTests.swift b/enzevalos_iphoneTests/LoggerTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..06b5085808cbcc4c7aa07421be98bea791fccb36 --- /dev/null +++ b/enzevalos_iphoneTests/LoggerTests.swift @@ -0,0 +1,47 @@ +// +// LoggerTests.swift +// enzevalos_iphoneTests +// +// Created by Joscha on 16.11.17. +// Copyright © 2017 fu-berlin. All rights reserved. +// + +import XCTest + +class LoggerTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testExample() { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct results. + } + + func testPerformanceExample() { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + + func testLogWriting() { + let testString = "This is a test String" + let testFile = "testLog.json" + Logger.saveToDisk(fileName: testFile, json: testString) + + if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { + + let fileURL = dir.appendingPathComponent(testFile) + + XCTAssert(FileManager.default.fileExists(atPath: fileURL.path)) + } + } +}