Skip to content

Commit

Permalink
Clear cached files (#233)
Browse files Browse the repository at this point in the history
* Add method to remove folder

* Improve unit test for deleteCachedFiles
  • Loading branch information
3lvis authored Jul 19, 2018
1 parent b6b1762 commit d495a45
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
8 changes: 8 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -225,6 +228,7 @@
14C0AF831BD6D4230009ECBE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
14D144DD1CEED63B00048629 /* FormDataPart.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormDataPart.swift; sourceTree = "<group>"; };
444B7D081FFB7AF200B44251 /* PATCHTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PATCHTests.swift; sourceTree = "<group>"; };
445F053021007512003906C2 /* NetworkingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkingTests.swift; sourceTree = "<group>"; };
446C24621DFEE964006FA6CC /* Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
4488DBC01D9BBFCF007D7A14 /* FakeRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeRequest.swift; sourceTree = "<group>"; };
4488DBC91D9BCA0E007D7A14 /* FakeRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeRequestTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -310,6 +314,7 @@
isa = PBXGroup;
children = (
14B0CEDD1CF1D0D700049AD6 /* Resources */,
445F053021007512003906C2 /* NetworkingTests.swift */,
146EF90E1CEBCEA50059DDB1 /* GETTests.swift */,
146EF90F1CEBCEA50059DDB1 /* POSTTests.swift */,
146EF9101CEBCEA50059DDB1 /* PUTTests.swift */,
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
29 changes: 14 additions & 15 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
16 changes: 0 additions & 16 deletions Tests/DownloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
33 changes: 33 additions & 0 deletions Tests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit d495a45

Please sign in to comment.