From d495a451465529f83fd50a38ad6d9ca9ad907603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= <3lvis@users.noreply.github.com> Date: Thu, 19 Jul 2018 15:09:11 +0200 Subject: [PATCH] Clear cached files (#233) * Add method to remove folder * Improve unit test for deleteCachedFiles --- Demo.xcodeproj/project.pbxproj | 8 ++++++++ Sources/Networking.swift | 29 ++++++++++++++--------------- Tests/DownloadTests.swift | 16 ---------------- Tests/NetworkingTests.swift | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 31 deletions(-) diff --git a/Demo.xcodeproj/project.pbxproj b/Demo.xcodeproj/project.pbxproj index 037e71f..3c71055 100755 --- a/Demo.xcodeproj/project.pbxproj +++ b/Demo.xcodeproj/project.pbxproj @@ -99,6 +99,9 @@ 444B7D091FFB7AF200B44251 /* PATCHTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 444B7D081FFB7AF200B44251 /* PATCHTests.swift */; }; 444B7D0A1FFB7AF200B44251 /* PATCHTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 444B7D081FFB7AF200B44251 /* PATCHTests.swift */; }; 444B7D0B1FFB7AF200B44251 /* PATCHTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 444B7D081FFB7AF200B44251 /* PATCHTests.swift */; }; + 445F053121007512003906C2 /* NetworkingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 445F053021007512003906C2 /* NetworkingTests.swift */; }; + 445F053221007512003906C2 /* NetworkingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 445F053021007512003906C2 /* NetworkingTests.swift */; }; + 445F053321007512003906C2 /* NetworkingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 445F053021007512003906C2 /* NetworkingTests.swift */; }; 446C24631DFEE964006FA6CC /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 446C24621DFEE964006FA6CC /* Helpers.swift */; }; 446C24641DFEE964006FA6CC /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 446C24621DFEE964006FA6CC /* Helpers.swift */; }; 446C24651DFEE964006FA6CC /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 446C24621DFEE964006FA6CC /* Helpers.swift */; }; @@ -225,6 +228,7 @@ 14C0AF831BD6D4230009ECBE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 14D144DD1CEED63B00048629 /* FormDataPart.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormDataPart.swift; sourceTree = ""; }; 444B7D081FFB7AF200B44251 /* PATCHTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PATCHTests.swift; sourceTree = ""; }; + 445F053021007512003906C2 /* NetworkingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkingTests.swift; sourceTree = ""; }; 446C24621DFEE964006FA6CC /* Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = ""; }; 4488DBC01D9BBFCF007D7A14 /* FakeRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeRequest.swift; sourceTree = ""; }; 4488DBC91D9BCA0E007D7A14 /* FakeRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeRequestTests.swift; sourceTree = ""; }; @@ -310,6 +314,7 @@ isa = PBXGroup; children = ( 14B0CEDD1CF1D0D700049AD6 /* Resources */, + 445F053021007512003906C2 /* NetworkingTests.swift */, 146EF90E1CEBCEA50059DDB1 /* GETTests.swift */, 146EF90F1CEBCEA50059DDB1 /* POSTTests.swift */, 146EF9101CEBCEA50059DDB1 /* PUTTests.swift */, @@ -809,6 +814,7 @@ 1488E23F1CF138D700C6FBA3 /* SHA1.m in Sources */, 1413F4771CE7968E00482096 /* JSON.swift in Sources */, 4488DBCC1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */, + 445F053321007512003906C2 /* NetworkingTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -919,6 +925,7 @@ 1488E23E1CF138D700C6FBA3 /* SHA1.m in Sources */, 1474C1C81CA2988900E15D84 /* Networking.swift in Sources */, 4488DBCA1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */, + 445F053121007512003906C2 /* NetworkingTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -953,6 +960,7 @@ 44B5D39F1D19B9E5004378B9 /* SHA1.m in Sources */, 44B5D3A01D19B9E5004378B9 /* Networking.swift in Sources */, 4488DBCB1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */, + 445F053221007512003906C2 /* NetworkingTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/Networking.swift b/Sources/Networking.swift index 33d7400..e526a48 100644 --- a/Sources/Networking.swift +++ b/Sources/Networking.swift @@ -287,31 +287,30 @@ open class Networking { _ = semaphore.wait(timeout: DispatchTime.now() + 60.0) } + /// Removes the stored credentials and cached data. + public func reset() { + cache.removeAllObjects() + fakeRequests.removeAll() + token = nil + headerFields = nil + authorizationHeaderKey = "Authorization" + authorizationHeaderValue = nil + + Networking.deleteCachedFiles() + } + /// Deletes the downloaded/cached files. public static func deleteCachedFiles() { #if os(tvOS) - let directory = FileManager.SearchPathDirectory.cachesDirectory + let directory = FileManager.SearchPathDirectory.cachesDirectory #else - let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory + let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory #endif if let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first { let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString) - if FileManager.default.exists(at: folderURL) { _ = try? FileManager.default.remove(at: folderURL) } } } - - /// Removes the stored credentials and cached data. - public func reset() { - cache.removeAllObjects() - fakeRequests.removeAll() - token = nil - headerFields = nil - authorizationHeaderKey = "Authorization" - authorizationHeaderValue = nil - - Networking.deleteCachedFiles() - } } diff --git a/Tests/DownloadTests.swift b/Tests/DownloadTests.swift index e26bbfa..b7c0056 100644 --- a/Tests/DownloadTests.swift +++ b/Tests/DownloadTests.swift @@ -306,20 +306,4 @@ class DownloadTests: XCTestCase { } } } - - func testDeleteDownloadedFiles() { - let networking = Networking(baseURL: baseURL) - networking.downloadImage("/image/png") { _ in - #if os(tvOS) - let directory = FileManager.SearchPathDirectory.cachesDirectory - #else - let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory - #endif - let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first! - let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString) - XCTAssertTrue(FileManager.default.exists(at: folderURL)) - Networking.deleteCachedFiles() - XCTAssertFalse(FileManager.default.exists(at: folderURL)) - } - } } diff --git a/Tests/NetworkingTests.swift b/Tests/NetworkingTests.swift index 4bf1b27..c248c3b 100644 --- a/Tests/NetworkingTests.swift +++ b/Tests/NetworkingTests.swift @@ -266,4 +266,37 @@ class NetworkingTests: XCTestCase { XCTAssertEqual(networking.authorizationHeaderKey, "Authorization") XCTAssertNil(networking.authorizationHeaderValue) } + + func testDeleteCachedFiles() { + #if os(tvOS) + let directory = FileManager.SearchPathDirectory.cachesDirectory + #else + let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory + #endif + let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first! + let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString) + + let networking = Networking(baseURL: baseURL) + networking.downloadImage("/image/png") { _ in + let image = Image.find(named: "sample.jpg", inBundle: Bundle(for: NetworkingTests.self)) + let data = image.jpgData() + let filename = cachesURL.appendingPathComponent("sample.jpg") + try? data?.write(to: filename) + + XCTAssertTrue(FileManager.default.exists(at: cachesURL)) + XCTAssertTrue(FileManager.default.exists(at: folderURL)) + XCTAssertTrue(FileManager.default.exists(at: filename)) + + Networking.deleteCachedFiles() + + // Caches folder should be there + XCTAssertTrue(FileManager.default.exists(at: cachesURL)) + + // Files under networking domain are gone + XCTAssertFalse(FileManager.default.exists(at: folderURL)) + + // Saved image should be there + XCTAssertTrue(FileManager.default.exists(at: filename)) + } + } }