|
20 | 20 | return [NSFileManager defaultManager]; |
21 | 21 | }); |
22 | 22 |
|
| 23 | + let(resourcePath, ^id{ |
| 24 | + return [[NSBundle bundleForClass:G8Tesseract.class].resourcePath stringByAppendingString:@"/"]; |
| 25 | + }); |
| 26 | + |
| 27 | + let(tessdataFolderName, ^id{ |
| 28 | + return @"tessdata"; |
| 29 | + }); |
| 30 | + |
| 31 | + let(tessdataFolderPathFromTheBundle, ^id{ |
| 32 | + return [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"/"]; |
| 33 | + }); |
| 34 | + |
23 | 35 | context(@"nil dataPath", ^{ |
24 | 36 |
|
25 | 37 | it(@"", ^{ |
26 | 38 | [[fileManager shouldNot] receive:@selector(createSymbolicLinkAtPath:withDestinationPath:error:)]; |
27 | 39 | G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages]; |
28 | 40 | [[tesseract shouldNot] beNil]; |
| 41 | + |
| 42 | + [[tesseract.absoluteDataPath should] equal:resourcePath]; |
29 | 43 | }); |
30 | 44 | }); |
31 | 45 |
|
|
34 | 48 | NSString *cachesPath = cachesPaths.firstObject; |
35 | 49 | return cachesPath; |
36 | 50 | }); |
| 51 | + |
| 52 | + let(tessdataPath, ^id{ |
| 53 | + return @"foo/bar"; |
| 54 | + }); |
| 55 | + |
| 56 | + let(cachesTessDataPath, ^id{ |
| 57 | + return [cashesPath stringByAppendingPathComponent:tessdataPath]; |
| 58 | + }); |
37 | 59 |
|
38 | 60 | context(@"not nil dataPath", ^{ |
39 | 61 |
|
40 | | - let(tessdataPath, ^id{ |
41 | | - return @"tes"; |
42 | | - }); |
| 62 | + // helper |
| 63 | + BOOL (^doFoldersContainTheSameElements)(void) = ^(void){ |
| 64 | + NSError *error = nil; |
| 65 | + NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:tessdataFolderPathFromTheBundle error:&error]; |
| 66 | + [[contentsOfTessdataFromTheBundle should] haveCountOfAtLeast:1]; |
| 67 | + if (error != nil) { |
| 68 | + NSLog(@"Error getting the content of the Tessdata folder from the app bundle: %@", error); |
| 69 | + } |
| 70 | + |
| 71 | + NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath:[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] error:&error]; |
| 72 | + [[contentsOfTheTessdataPathFolder should] haveCountOfAtLeast:1]; |
| 73 | + if (error != nil) { |
| 74 | + NSLog(@"Error getting the content of the Tessdata folder from the Caches folder: %@", error); |
| 75 | + } |
| 76 | + |
| 77 | + return [contentsOfTheTessdataPathFolder isEqualToArray:contentsOfTessdataFromTheBundle]; |
| 78 | + }; |
43 | 79 |
|
44 | 80 | let(tessdataFolderName, ^id{ |
45 | 81 | return @"tessdata"; |
46 | 82 | }); |
47 | 83 |
|
48 | | - let(bundle, ^id{ |
49 | | - return [NSBundle bundleForClass:G8Tesseract.class]; |
50 | | - }); |
51 | | - |
52 | | - it(@"no tessdata folder in the Documents yet", ^{ |
| 84 | + it(@"no tessdata folder in the Caches yet", ^{ |
53 | 85 |
|
54 | | - // proof Documents folder is empty |
55 | | - BOOL folderExists = [fileManager fileExistsAtPath:[cashesPath stringByAppendingPathComponent:tessdataFolderName]]; |
| 86 | + // proof Caches folder is empty |
| 87 | + BOOL folderExists = [fileManager fileExistsAtPath:cachesTessDataPath]; |
56 | 88 | [[theValue(folderExists) should] equal:theValue(NO)]; |
57 | 89 |
|
58 | 90 | G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
|
62 | 94 | engineMode:G8OCREngineModeTesseractOnly]; |
63 | 95 | [[tesseract shouldNot] beNil]; |
64 | 96 |
|
65 | | - NSError *error = nil; |
66 | | - NSString *path = [[NSBundle bundleForClass:[G8Tesseract class]].bundlePath stringByAppendingPathComponent:@"tessdata"]; |
67 | | - NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:path error:&error]; |
68 | | - [[contentsOfTessdataFromTheBundle should] haveCountOfAtLeast:1]; |
69 | | - if (error != nil) { |
70 | | - NSLog(@"Error getting the content of the Tessdata folder from the app bundle: %@", error); |
71 | | - } |
72 | | - |
73 | | - NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath:[tesseract.absoluteDataPath stringByAppendingPathComponent:@"tessdata"] error:&error]; |
74 | | - [[contentsOfTheTessdataPathFolder should] haveCountOfAtLeast:1]; |
75 | | - if (error != nil) { |
76 | | - NSLog(@"Error getting the content of the Tessdata folder from the Documents folder: %@", error); |
77 | | - } |
| 97 | + [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
78 | 98 |
|
79 | | - [[contentsOfTessdataFromTheBundle should] containObjectsInArray:contentsOfTheTessdataPathFolder]; |
| 99 | + [[theValue(doFoldersContainTheSameElements()) should] equal:theValue(YES)]; |
80 | 100 | }); |
81 | 101 |
|
82 | | - it(@"tessdata are already in the Documents", ^{ |
| 102 | + it(@"tessdata are already in the Caches", ^{ |
83 | 103 | // helper |
84 | | - BOOL (^moveTessdataToDocumentsDirectoryIfNecessary)(NSString *dataPath) = ^(NSString *dataPath){ |
| 104 | + BOOL (^moveTessdataToCachesDirectoryIfNecessary)(NSString *dataPath) = ^(NSString *dataPath){ |
85 | 105 |
|
86 | 106 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
87 | 107 |
|
88 | 108 | // Useful paths |
89 | | - NSString *tessdataFolderName = @"tessdata"; |
90 | | - NSString *tessdataDestinationPath = [[NSBundle bundleForClass:G8Tesseract.class].resourcePath |
91 | | - stringByAppendingPathComponent:tessdataFolderName]; |
92 | | - NSString *destinationPath = [[[NSBundle bundleForClass:G8Tesseract.class].resourcePath |
93 | | - stringByAppendingPathComponent:dataPath] |
94 | | - stringByAppendingPathComponent:tessdataFolderName]; |
| 109 | + NSString *tessdataSourcePath = [resourcePath stringByAppendingPathComponent:tessdataFolderName]; |
| 110 | + NSString *destinationPath = [[cashesPath stringByAppendingPathComponent:dataPath] stringByAppendingPathComponent:tessdataFolderName]; |
95 | 111 |
|
96 | 112 | if ([fileManager fileExistsAtPath:destinationPath] == NO) { |
97 | 113 | NSError *error = nil; |
|
103 | 119 | } |
104 | 120 |
|
105 | 121 | NSError *error = nil; |
106 | | - NSArray *files = [fileManager contentsOfDirectoryAtPath:tessdataDestinationPath error:&error]; |
| 122 | + NSArray *files = [fileManager contentsOfDirectoryAtPath:tessdataSourcePath error:&error]; |
107 | 123 | if (error != nil) { |
108 | 124 | NSLog(@"Test: ERROR! %@", error.description); |
109 | 125 | return NO; |
110 | 126 | } |
111 | 127 | for (NSString *filename in files) { |
112 | 128 |
|
113 | 129 | NSString *destinationFileName = [destinationPath stringByAppendingPathComponent:filename]; |
114 | | - if ([fileManager fileExistsAtPath:destinationFileName]) { |
| 130 | + if (![fileManager fileExistsAtPath:destinationFileName]) { |
115 | 131 |
|
116 | | - NSString *filePath = [tessdataDestinationPath stringByAppendingPathComponent:filename]; |
| 132 | + NSString *sourceFilePath = [tessdataSourcePath stringByAppendingPathComponent:filename]; |
117 | 133 | //NSLog(@"found %@", filePath); |
118 | 134 | //NSLog(@"symlink in %@", destinationFileName); |
119 | 135 |
|
120 | 136 | BOOL res = [fileManager createSymbolicLinkAtPath:destinationFileName |
121 | | - withDestinationPath:filePath |
| 137 | + withDestinationPath:sourceFilePath |
122 | 138 | error:&error]; |
123 | 139 | if (res == NO) { |
124 | 140 | NSLog(@"Test: The result of createSymbolicLinkAtPath == NO"); |
125 | 141 | return NO; |
126 | 142 | } |
127 | 143 | if (error != nil) { |
128 | | - NSLog(@"Test: Error creating symlink %@: %@", filePath, error); |
| 144 | + NSLog(@"Test: Error creating symlink %@: %@", sourceFilePath, error); |
129 | 145 | return NO; |
130 | 146 | } |
131 | 147 | } |
|
134 | 150 | return YES; |
135 | 151 | }; |
136 | 152 |
|
137 | | - // copy files to the Documents dir first |
138 | | - BOOL res = moveTessdataToDocumentsDirectoryIfNecessary(tessdataPath); |
| 153 | + // copy files to the Caches dir first |
| 154 | + BOOL res = moveTessdataToCachesDirectoryIfNecessary(tessdataPath); |
139 | 155 | [[theValue(res) should] equal:theValue(YES)]; |
| 156 | + |
| 157 | + [[theValue(doFoldersContainTheSameElements()) should] equal:theValue(YES)]; |
140 | 158 |
|
141 | 159 | G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
142 | 160 | configDictionary:nil |
143 | 161 | configFileNames:nil |
144 | 162 | cachesRelatedDataPath:tessdataPath |
145 | 163 | engineMode:G8OCREngineModeTesseractOnly]; |
146 | 164 | [[tesseract shouldNot] beNil]; |
| 165 | + [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
147 | 166 | }); |
148 | 167 |
|
149 | 168 | afterEach(^{ |
150 | | - NSLog(@"Removing previous tessdata folder from Documents"); |
| 169 | + NSLog(@"Removing previous tessdata folder from Caches folder"); |
151 | 170 | NSError *error = nil; |
152 | | - NSString *path = [cashesPath stringByAppendingPathComponent:tessdataPath]; |
153 | | - BOOL fileIsRemoved = [fileManager removeItemAtPath:path error:&error]; |
| 171 | + BOOL fileIsRemoved = [fileManager removeItemAtPath:cachesTessDataPath error:&error]; |
154 | 172 | [[theValue(fileIsRemoved) should] equal:theValue(YES)]; |
155 | 173 | if (error != nil) { |
156 | | - NSLog(@"Error deleting tessdata folder from the Documents: %@", error); |
| 174 | + NSLog(@"Error deleting tessdata folder from the Caches folder: %@", error); |
157 | 175 | } |
158 | 176 |
|
159 | 177 | // check tessdata folder was deleted |
160 | | - NSArray *documentsContent = [fileManager contentsOfDirectoryAtPath:cashesPath error:&error]; |
161 | | - [[documentsContent shouldNot] contain:tessdataPath]; |
| 178 | + NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cashesPath error:&error]; |
| 179 | + [[cachesContent shouldNot] contain:tessdataPath]; |
162 | 180 | if (error != nil) { |
163 | | - NSLog(@"Error getting the contents of the Documents folder: %@", error); |
| 181 | + NSLog(@"Error getting the contents of the Caches folder: %@", error); |
164 | 182 | } |
165 | 183 | }); |
166 | 184 | }); |
|
0 commit comments