Skip to content

Commit 96bb458

Browse files
author
Kirill Makankov
committed
1. moveTessdataToCachesDirectoryIfNecessary tests.
2. moveTessdataToCachesDirectoryIfNecessary logic has been changed to allow it to be tested. No checking for error anymore. Instead check the function results. 3. Some tests of tests are change to NSAsserts
1 parent 7ef8021 commit 96bb458

File tree

2 files changed

+82
-47
lines changed

2 files changed

+82
-47
lines changed

TesseractOCR/G8Tesseract.mm

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ - (id)initWithLanguage:(NSString *)language
113113

114114
_absoluteDataPath = [cachesPath stringByAppendingPathComponent:_absoluteDataPath].copy;
115115

116-
[self moveTessdataToCachesDirectoryIfNecessary];
116+
BOOL succes = [self moveTessdataToCachesDirectoryIfNecessary];
117+
if (succes == NO) {
118+
return nil;
119+
}
117120
}
118121
else {
119122
// config Tesseract to search trainedData in tessdata folder of the application bundle];
@@ -207,8 +210,8 @@ - (BOOL)moveTessdataToCachesDirectoryIfNecessary
207210

208211
if ([fileManager fileExistsAtPath:destinationPath] == NO) {
209212
NSError *error = nil;
210-
[fileManager createDirectoryAtPath:destinationPath withIntermediateDirectories:YES attributes:nil error:&error];
211-
if (error != nil) {
213+
BOOL res = [fileManager createDirectoryAtPath:destinationPath withIntermediateDirectories:YES attributes:nil error:&error];
214+
if (res == NO) {
212215
NSLog(@"Error creating folder %@: %@", destinationPath, error);
213216
return NO;
214217
}
@@ -217,30 +220,31 @@ - (BOOL)moveTessdataToCachesDirectoryIfNecessary
217220
BOOL result = YES;
218221
NSError *error = nil;
219222
NSArray *files = [fileManager contentsOfDirectoryAtPath:tessdataPath error:&error];
220-
if (error != nil) {
223+
if (files == nil) {
221224
NSLog(@"ERROR! %@", error.description);
222225
result = NO;
223-
}
224-
for (NSString *filename in files) {
225-
226-
NSString *destinationFileName = [destinationPath stringByAppendingPathComponent:filename];
227-
if (![fileManager fileExistsAtPath:destinationFileName]) {
226+
} else {
227+
for (NSString *filename in files) {
228228

229-
NSString *filePath = [tessdataPath stringByAppendingPathComponent:filename];
230-
//NSLog(@"found %@", filePath);
231-
//NSLog(@"symlink in %@", destinationFileName);
232-
233-
// delete broken symlinks first
234-
[fileManager removeItemAtPath:destinationFileName error:&error];
235-
236-
// than recreate it
237-
error = nil; // don't care about previous error, that can happens if we tried to remove an symlink, which doesn't exist
238-
[fileManager createSymbolicLinkAtPath:destinationFileName
239-
withDestinationPath:filePath
240-
error:&error];
241-
if (error != nil) {
242-
NSLog(@"Error creating symlink %@: %@", destinationPath, error);
243-
result = NO;
229+
NSString *destinationFileName = [destinationPath stringByAppendingPathComponent:filename];
230+
if (![fileManager fileExistsAtPath:destinationFileName]) {
231+
232+
NSString *filePath = [tessdataPath stringByAppendingPathComponent:filename];
233+
//NSLog(@"found %@", filePath);
234+
//NSLog(@"symlink in %@", destinationFileName);
235+
236+
// delete broken symlinks first
237+
[fileManager removeItemAtPath:destinationFileName error:&error];
238+
239+
// than recreate it
240+
error = nil; // don't care about previous error, that can happens if we tried to remove a symlink, which doesn't exist
241+
BOOL res = [fileManager createSymbolicLinkAtPath:destinationFileName
242+
withDestinationPath:filePath
243+
error:&error];
244+
if (res == NO) {
245+
NSLog(@"Error creating symlink %@: %@", destinationPath, error);
246+
result = NO;
247+
}
244248
}
245249
}
246250
}

TestsProject/TestsProjectTests/InitializationTests.m

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,66 @@ - (BOOL)resetEngine;
147147
checkVariablesAreSetForTesseract(tesseract);
148148
});
149149
});
150-
150+
151151
NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
152152
NSString *cachesPath = cachesPaths.firstObject;
153153
NSString *tessdataPath = @"foo/bar";
154154
NSString *cachesTessDataPath = [cachesPath stringByAppendingPathComponent:tessdataPath];
155+
156+
void(^cleanTessdataFolder)() = ^{
157+
//NSLog(@"Removing previous tessdata folder from Caches folder");
158+
NSError *error = nil;
159+
BOOL fileIsRemoved = [fileManager removeItemAtPath:cachesTessDataPath error:&error];
160+
if (error != nil) {
161+
NSLog(@"Error deleting tessdata folder from the Caches folder: %@", error);
162+
}
163+
NSAssert(fileIsRemoved == YES, @"Error cleaning tessdata from the Caches folder");
164+
165+
// check tessdata folder was deleted
166+
NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cachesPath error:&error];
167+
if (error != nil) {
168+
NSLog(@"Error getting the contents of the Caches folder: %@", error);
169+
}
170+
NSAssert([cachesContent containsObject:tessdataPath] == NO, @"Assert! Tessdata path was not removed from the Caches folder");
171+
};
172+
173+
context(@"moveTessdataToCachesDirectoryIfNecessary", ^{
174+
175+
void (^checkInitializationWithFailedSelectorReturnValueAndCount)(SEL selector, id returnValue, int count) = ^(SEL selector, id returnValue, int count){
176+
G8Tesseract *wrongTesseract = [G8Tesseract alloc];
177+
[[wrongTesseract shouldNot] beNil];
178+
[[[NSFileManager defaultManager] should] receive:selector andReturn:returnValue withCount:count];
179+
wrongTesseract = [wrongTesseract initWithLanguage:kG8Languages configDictionary:nil configFileNames:nil cachesRelatedDataPath:tessdataPath engineMode:G8OCREngineModeTesseractOnly];
180+
[[wrongTesseract should] beNil];
181+
};
182+
183+
it(@"Should return nil if createDirectoryAtPath fails", ^{
184+
checkInitializationWithFailedSelectorReturnValueAndCount(@selector(createDirectoryAtPath:withIntermediateDirectories:attributes:error:), theValue(NO), 1);
185+
});
186+
187+
it(@"Should return nil if createSymbolicLinkAtPath fails", ^{
188+
NSError *error = nil;
189+
NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:tessdataFolderPathFromTheBundle error:&error];
190+
NSAssert (error == nil, @"Error getting the content of the Tessdata folder from the app bundle: %@", error);
155191

192+
checkInitializationWithFailedSelectorReturnValueAndCount(@selector(createSymbolicLinkAtPath:withDestinationPath:error:), theValue(NO), contentsOfTessdataFromTheBundle.count);
193+
cleanTessdataFolder();
194+
});
195+
196+
it(@"Should return nil if contentsOfDirectoryAtPath fails", ^{
197+
checkInitializationWithFailedSelectorReturnValueAndCount(@selector(contentsOfDirectoryAtPath:error:), nil, 2);
198+
cleanTessdataFolder();
199+
});
200+
});
201+
156202
context(@"not nil cachesRelatedDataPath", ^{
157203

158204
// helper
159205
BOOL (^doFoldersContainTheSameElements)(void) = ^(void){
160206
NSError *error = nil;
161207
NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:tessdataFolderPathFromTheBundle error:&error];
162-
[[contentsOfTessdataFromTheBundle should] haveCountOfAtLeast:1];
163-
if (error != nil) {
164-
NSLog(@"Error getting the content of the Tessdata folder from the app bundle: %@", error);
165-
}
208+
NSAssert(contentsOfTessdataFromTheBundle.count >= 1, @"Error! Tessdata folder is empty");
209+
NSAssert(error == nil, @"Error getting the content of the Tessdata folder from the app bundle: %@", error);
166210

167211
NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath:[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] error:&error];
168212
[[contentsOfTheTessdataPathFolder should] haveCountOfAtLeast:1];
@@ -181,7 +225,7 @@ - (BOOL)resetEngine;
181225
it(@"Should simple init, download rus language files and reinitialize tess with them", ^{
182226
// proof Caches folder is empty
183227
BOOL folderExists = [fileManager fileExistsAtPath:cachesTessDataPath];
184-
[[theValue(folderExists) should] beNo];
228+
NSAssert(folderExists == NO, @"Error! Tessdata folder is already here: %@", cachesTessDataPath);
185229

186230
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
187231
configDictionary:nil
@@ -229,7 +273,7 @@ - (BOOL)resetEngine;
229273
});
230274

231275
it(@"Should initialize with config dictionary", ^{
232-
276+
233277
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
234278
configDictionary:@{
235279
kG8ParamTessdataManagerDebugLevel : @"1",
@@ -268,8 +312,8 @@ - (BOOL)resetEngine;
268312

269313
it(@"Should initialize with 2 config files", ^{
270314

271-
[[[fileManager attributesOfItemAtPath:debugConfigsFilePathFromTheCaches error:nil] should] beNil];
272-
[[[fileManager attributesOfItemAtPath:recognitionConfigsFilePathFromTheCaches error:nil] should] beNil];
315+
NSAssert([fileManager attributesOfItemAtPath:debugConfigsFilePathFromTheCaches error:nil] == nil, @"Error! %@ is already here!", debugConfigsFilePathFromTheCaches);
316+
NSAssert([fileManager attributesOfItemAtPath:recognitionConfigsFilePathFromTheCaches error:nil] == nil, @"Error! %@ cannot is already here!", recognitionConfigsFilePathFromTheCaches);
273317

274318
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
275319
configDictionary:nil
@@ -413,20 +457,7 @@ - (BOOL)resetEngine;
413457
});
414458

415459
afterEach(^{
416-
//NSLog(@"Removing previous tessdata folder from Caches folder");
417-
NSError *error = nil;
418-
BOOL fileIsRemoved = [fileManager removeItemAtPath:cachesTessDataPath error:&error];
419-
if (error != nil) {
420-
NSLog(@"Error deleting tessdata folder from the Caches folder: %@", error);
421-
}
422-
NSAssert(fileIsRemoved == YES, @"Error cleaning tessdata from the Caches folder");
423-
424-
// check tessdata folder was deleted
425-
NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cachesPath error:&error];
426-
if (error != nil) {
427-
NSLog(@"Error getting the contents of the Caches folder: %@", error);
428-
}
429-
NSAssert([cachesContent containsObject:tessdataPath] == NO, @"Assert! Tessdata path was not removed from the Caches folder");
460+
cleanTessdataFolder();
430461
});
431462
});
432463
});

0 commit comments

Comments
 (0)