Skip to content

Commit 8387255

Browse files
authored
Merge pull request #1 from merlos/features/add-diskcache
Features/add diskcache Includes DiskCache + Unit testing + TravisCI builds
2 parents 9ba83ab + 3f23542 commit 8387255

15 files changed

Lines changed: 945 additions & 36 deletions

.travis.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
# * https://www.objc.io/issues/6-build-tools/travis-ci/
33
# * https://github.com/supermarin/xcpretty#usage
44

5-
osx_image: xcode7.3
6-
language: objective-c
5+
osx_image: xcode10.2
6+
language: swift
77
# cache: cocoapods
88
# podfile: Example/Podfile
9-
# before_install:
9+
before_install:
1010
# - gem install cocoapods # Since Travis is not always on latest version
1111
# - pod install --project-directory=Example
12+
- gem install xcpretty
1213
script:
13-
- set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Example/MapCache.xcworkspace -scheme MapCache-Example -sdk iphonesimulator9.3 ONLY_ACTIVE_ARCH=NO | xcpretty
14+
- xcodebuild -version
15+
- xcodebuild -showsdks
16+
- set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Example/MapCache.xcworkspace -scheme MapCache-Example -sdk iphonesimulator12.2 ONLY_ACTIVE_ARCH=NO | xcpretty
1417
- pod lib lint

Example/MapCache.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
1515
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
1616
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; };
17+
893072CC22A47E4A008612A1 /* DiskCacheSpecs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 893072CB22A47E4A008612A1 /* DiskCacheSpecs.swift */; };
18+
893072CE22A47F06008612A1 /* String+DiskCacheSpecs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 893072CD22A47F06008612A1 /* String+DiskCacheSpecs.swift */; };
1719
897DE7EE22976D7E0054A9A1 /* MapCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 897DE7ED22976D7E0054A9A1 /* MapCacheTests.swift */; };
1820
C96E40763BA125EDD01D8619 /* Pods_MapCache_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E93B0D3BBC838295972E4B6C /* Pods_MapCache_Tests.framework */; };
1921
/* End PBXBuildFile section */
@@ -45,6 +47,8 @@
4547
607FACEA1AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4648
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
4749
86806D7962E8E2CA29F7E1EF /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
50+
893072CB22A47E4A008612A1 /* DiskCacheSpecs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiskCacheSpecs.swift; sourceTree = "<group>"; };
51+
893072CD22A47F06008612A1 /* String+DiskCacheSpecs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+DiskCacheSpecs.swift"; sourceTree = "<group>"; };
4852
897DE7ED22976D7E0054A9A1 /* MapCacheTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapCacheTests.swift; sourceTree = "<group>"; };
4953
98B68CF07B3AC619DB7870E1 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
5054
E93B0D3BBC838295972E4B6C /* Pods_MapCache_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_MapCache_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -126,6 +130,7 @@
126130
607FACE81AFB9204008FA782 /* Tests */ = {
127131
isa = PBXGroup;
128132
children = (
133+
893072CA22A47D36008612A1 /* DiskCache */,
129134
607FACEB1AFB9204008FA782 /* Tests.swift */,
130135
897DE7ED22976D7E0054A9A1 /* MapCacheTests.swift */,
131136
607FACE91AFB9204008FA782 /* Supporting Files */,
@@ -151,6 +156,15 @@
151156
name = "Podspec Metadata";
152157
sourceTree = "<group>";
153158
};
159+
893072CA22A47D36008612A1 /* DiskCache */ = {
160+
isa = PBXGroup;
161+
children = (
162+
893072CB22A47E4A008612A1 /* DiskCacheSpecs.swift */,
163+
893072CD22A47F06008612A1 /* String+DiskCacheSpecs.swift */,
164+
);
165+
path = DiskCache;
166+
sourceTree = "<group>";
167+
};
154168
B6CB91938DE7FBFF8535B923 /* Pods */ = {
155169
isa = PBXGroup;
156170
children = (
@@ -357,6 +371,8 @@
357371
buildActionMask = 2147483647;
358372
files = (
359373
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */,
374+
893072CC22A47E4A008612A1 /* DiskCacheSpecs.swift in Sources */,
375+
893072CE22A47F06008612A1 /* String+DiskCacheSpecs.swift in Sources */,
360376
897DE7EE22976D7E0054A9A1 /* MapCacheTests.swift in Sources */,
361377
);
362378
runOnlyForDeploymentPostprocessing = 0;

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 45 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
//
2+
// DiskCacheSpecs.swift
3+
// MapCache_Tests
4+
//
5+
// Created by merlos on 06/02/2019.
6+
// Copyright © 2019 CocoaPods. All rights reserved.
7+
//
8+
9+
import Quick
10+
import Nimble
11+
@testable import MapCache
12+
13+
class DiskCacheSpecs: QuickSpec {
14+
override func spec() {
15+
16+
/// Removes the cache folder
17+
func removeCache(cacheName: String) {
18+
let diskCache = DiskCache(withName: cacheName)
19+
do {
20+
try FileManager.default.removeItem(at: diskCache.folderURL)
21+
} catch {
22+
print ("ERROR removing DiskCache folder")
23+
}
24+
let exists = FileManager.default.fileExists(atPath: diskCache.path)
25+
expect(exists) == false
26+
}
27+
28+
describe("DiskCache initialization") {
29+
it("can create the cache folder") {
30+
let diskCache = DiskCache(withName: "path")
31+
var isDirectory : ObjCBool = false
32+
//print(diskCache.folderURL)
33+
let exists = FileManager.default.fileExists(atPath: diskCache.path, isDirectory: &isDirectory)
34+
expect(exists) == true
35+
expect(isDirectory.boolValue) == true
36+
}
37+
38+
afterEach() {
39+
let diskCache = DiskCache(withName: "path")
40+
do {
41+
try FileManager.default.removeItem(at: diskCache.folderURL)
42+
} catch {
43+
print ("ERROR removing DiskCache folder")
44+
}
45+
let exists = FileManager.default.fileExists(atPath: diskCache.path)
46+
expect(exists) == false
47+
}
48+
}
49+
50+
describe("a DiskCache") {
51+
52+
let cacheName = "path2"
53+
var diskCache: DiskCache!
54+
let filename1 = "http://www."
55+
let data1 = "1234567890".data(using: .utf8)
56+
57+
// Set a long file name
58+
var longFileName: String = "1234567890"
59+
60+
for i in 0...NAME_MAX {
61+
longFileName.append(String(i))
62+
}
63+
let dataLongFile = "1234567890".data(using: .utf8)
64+
65+
beforeEach {
66+
diskCache = DiskCache(withName: cacheName)
67+
}
68+
69+
afterEach {
70+
removeCache(cacheName: cacheName)
71+
}
72+
73+
it("can add a file") {
74+
let data = data1
75+
let filename = filename1
76+
diskCache.setData(data!, forKey: filename)
77+
let filePath = diskCache.folderURL.appendingPathComponent(filename.toMD5()).path
78+
expect(FileManager.default.fileExists(atPath: filePath)).toEventually(equal(true))
79+
}
80+
81+
it("can add a file with a very long name") {
82+
let data = dataLongFile
83+
let filename = longFileName
84+
85+
diskCache.setData(data!, forKey: filename)
86+
let filePath = diskCache.folderURL.appendingPathComponent(filename.MD5Filename()).path
87+
expect(FileManager.default.fileExists(atPath: filePath)).toEventually(equal(true))
88+
}
89+
90+
it("keeps track of its size") {
91+
let size = diskCache.calculateSize()
92+
expect(size) == 0
93+
diskCache.setDataSync(data1!, forKey: filename1)
94+
expect(diskCache.size).toEventually(equal(10))
95+
}
96+
97+
it("can find a file that is in the cache") {
98+
diskCache.setData(data1!, forKey: filename1)
99+
expect(diskCache.size).toEventually(equal(10))
100+
diskCache.fetchData(forKey: filename1, failure: {error in return}, success: {
101+
expect($0) == data1
102+
})
103+
104+
}
105+
106+
it("cannot find a file that is not in the cache") {
107+
diskCache.fetchData(forKey: "filename1", failure: { error in
108+
guard let error = error as NSError? else {
109+
return
110+
}
111+
expect(error.code).to(equal(NSFileReadNoSuchFileError))
112+
return
113+
}, success: {
114+
expect($0) == data1
115+
})
116+
}
117+
118+
//it("can handle weird names2") {
119+
// let diskCache2 = DiskCache(withName: "weird")
120+
// let weird1 = "ºª|!@#·$%&¬/()= ?'¿¡^`[]+*¨´{}ç;,.-<>€"
121+
// print("weird: \(diskCache2.path(forKey: weird1))")
122+
// diskCache2.setData(data1!, forKey: weird1)
123+
//}
124+
125+
it("can handle weird names") {
126+
let weird1 = "ºª|!@#·$%&¬/()= ?'¿¡^`[]+*¨´{}ç;,.-<>€"
127+
//print("weird: \(diskCache.path(forKey: weird1))")
128+
expect(diskCache.size).toEventually(equal(0))
129+
diskCache.setData(data1!, forKey: weird1)
130+
let filePath = diskCache.path(forKey: weird1)
131+
expect(FileManager.default.fileExists(atPath: filePath)).toEventually(equal(true))
132+
expect(diskCache.size).toEventually(equal(10))
133+
diskCache.fetchData(forKey: weird1, failure: { error in
134+
guard let error = error as NSError? else {
135+
expect(1) > 2
136+
return
137+
}
138+
expect(error.code).toNot(equal(NSFileReadNoSuchFileError))
139+
return
140+
}, success: {
141+
expect($0) == data1
142+
})
143+
}
144+
145+
it("can remove the file from the cache") {
146+
// add the file
147+
diskCache.setData(data1!, forKey: filename1)
148+
expect(diskCache.size).toEventually(equal(10))
149+
// remove the file
150+
diskCache.removeData(withKey: filename1)
151+
expect(diskCache.size).toEventually(equal(0))
152+
diskCache.fetchData(forKey: filename1, failure: { error in
153+
guard let error = error as NSError? else {
154+
expect(1) > 2
155+
return
156+
}
157+
expect(error.code).to(equal(NSFileReadNoSuchFileError))
158+
return
159+
}, success: {
160+
// if the test goes right it should never come here
161+
expect($0.isEmpty) == true //make it fail
162+
})
163+
}
164+
165+
it("can remove all items from the cache") {
166+
diskCache.setData(data1!, forKey: filename1)
167+
diskCache.setData(dataLongFile!, forKey: longFileName)
168+
expect(diskCache.size).toEventually(equal(20))
169+
diskCache.removeAllData({})
170+
expect(diskCache.size).toEventually(equal(0))
171+
}
172+
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)