/core/externals/google-toolbox-for-mac/Foundation/GTMNSFileManager+PathTest.m

http://macfuse.googlecode.com/ · Objective C · 263 lines · 182 code · 37 blank · 44 comment · 12 complexity · aed1a6b4134efb09926d930a8b127dc9 MD5 · raw file

  1. //
  2. // GTMNSFileManager+PathTest.m
  3. //
  4. // Copyright 2006-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMNSFileManager+Path.h"
  20. #import "GTMNSFileHandle+UniqueName.h"
  21. @interface GTMNSFileManager_PathTest : GTMTestCase {
  22. NSString *baseDir_;
  23. }
  24. @end
  25. @implementation GTMNSFileManager_PathTest
  26. - (void)setUp {
  27. // make a directory to scribble in
  28. NSFileManager *fm = [NSFileManager defaultManager];
  29. baseDir_
  30. = [[fm gtm_createTemporaryDirectoryBasedOn:@"GTMNSFileManager_PathTestXXXXXX"]
  31. retain];
  32. }
  33. - (void)tearDown {
  34. if (baseDir_) {
  35. // clean up our directory
  36. NSFileManager *fm = [NSFileManager defaultManager];
  37. #if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
  38. NSError *error = nil;
  39. [fm removeItemAtPath:baseDir_ error:&error];
  40. STAssertNil(error,
  41. @"Unable to delete %@: %@", baseDir_, error);
  42. #else
  43. [fm removeFileAtPath:baseDir_ handler:nil];
  44. #endif
  45. [baseDir_ release];
  46. baseDir_ = nil;
  47. }
  48. }
  49. #if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  50. - (void)testCreateFullPathToDirectoryAttributes {
  51. STAssertNotNil(baseDir_, @"setUp failed");
  52. NSString *testPath =
  53. [baseDir_ stringByAppendingPathComponent:@"/foo/bar/baz"];
  54. STAssertNotNil(testPath, nil);
  55. NSFileManager *fm = [NSFileManager defaultManager];
  56. STAssertFalse([fm fileExistsAtPath:testPath],
  57. @"You must delete '%@' before running this test", testPath);
  58. STAssertFalse([fm gtm_createFullPathToDirectory:nil attributes:nil],
  59. @"didn't fail on nil input");
  60. STAssertTrue([fm gtm_createFullPathToDirectory:testPath attributes:nil],
  61. @"Failed to create nested testPath");
  62. STAssertTrue([fm gtm_createFullPathToDirectory:testPath attributes:nil],
  63. @"Failed to succeed on second create of testPath");
  64. NSString *pathToFail = [@"/etc" stringByAppendingPathComponent:testPath];
  65. STAssertFalse([fm gtm_createFullPathToDirectory:pathToFail attributes:nil],
  66. @"We were allowed to create a dir in '/etc'?!");
  67. STAssertFalse([fm gtm_createFullPathToDirectory:nil attributes:nil],
  68. @"Should have failed when passed (nil)");
  69. }
  70. #endif // GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  71. - (void)testfilePathsWithExtensionsInDirectory {
  72. STAssertNotNil(baseDir_, @"setUp failed");
  73. NSFileManager *fm = [NSFileManager defaultManager];
  74. NSString *bogusPath = @"/some/place/that/does/not/exist";
  75. // --------------------------------------------------------------------------
  76. // test fail cases first
  77. // single
  78. STAssertNil([fm gtm_filePathsWithExtension:nil inDirectory:nil],
  79. @"shouldn't have gotten anything for nil dir");
  80. STAssertNil([fm gtm_filePathsWithExtension:@"txt" inDirectory:nil],
  81. @"shouldn't have gotten anything for nil dir");
  82. STAssertNil([fm gtm_filePathsWithExtension:@"txt" inDirectory:bogusPath],
  83. @"shouldn't have gotten anything for a bogus dir");
  84. // array
  85. STAssertNil([fm gtm_filePathsWithExtensions:nil inDirectory:nil],
  86. @"shouldn't have gotten anything for nil dir");
  87. STAssertNil([fm gtm_filePathsWithExtensions:[NSArray array] inDirectory:nil],
  88. @"shouldn't have gotten anything for nil dir");
  89. STAssertNil([fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
  90. inDirectory:nil],
  91. @"shouldn't have gotten anything for nil dir");
  92. STAssertNil([fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
  93. inDirectory:bogusPath],
  94. @"shouldn't have gotten anything for a bogus dir");
  95. // --------------------------------------------------------------------------
  96. // create some test data
  97. NSString *testDirs[] = {
  98. @"", @"/foo", // mave a subdir to make sure we don't match w/in it
  99. };
  100. NSString *testFiles[] = {
  101. @"a.txt", @"b.txt", @"c.rtf", @"d.m",
  102. };
  103. for (size_t i = 0; i < sizeof(testDirs) / sizeof(NSString*); i++) {
  104. NSString *testDir = nil;
  105. if ([testDirs[i] length]) {
  106. testDir = [baseDir_ stringByAppendingPathComponent:testDirs[i]];
  107. #if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
  108. NSError *error = nil;
  109. STAssertTrue([fm createDirectoryAtPath:testDir
  110. withIntermediateDirectories:YES
  111. attributes:nil
  112. error:&error],
  113. @"Can't create %@ (%@)", testDir, error);
  114. #else
  115. STAssertTrue([fm createDirectoryAtPath:testDir attributes:nil], nil);
  116. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  117. } else {
  118. testDir = baseDir_;
  119. }
  120. for (size_t j = 0; j < sizeof(testFiles) / sizeof(NSString*); j++) {
  121. NSString *testFile = [testDir stringByAppendingPathComponent:testFiles[j]];
  122. NSError *err = nil;
  123. STAssertTrue([@"test" writeToFile:testFile
  124. atomically:YES
  125. encoding:NSUTF8StringEncoding
  126. error:&err], @"Error: %@", err);
  127. }
  128. }
  129. // build set of the top level items
  130. NSMutableArray *allFiles = [NSMutableArray array];
  131. for (size_t i = 0; i < sizeof(testDirs) / sizeof(NSString*); i++) {
  132. if ([testDirs[i] length]) {
  133. NSString *testDir = [baseDir_ stringByAppendingPathComponent:testDirs[i]];
  134. [allFiles addObject:testDir];
  135. }
  136. }
  137. for (size_t j = 0; j < sizeof(testFiles) / sizeof(NSString*); j++) {
  138. NSString *testFile = [baseDir_ stringByAppendingPathComponent:testFiles[j]];
  139. [allFiles addObject:testFile];
  140. }
  141. NSArray *matches = nil;
  142. NSArray *expectedMatches = nil;
  143. NSArray *extensions = nil;
  144. // NOTE: we do all compares w/ sets so order doesn't matter
  145. // --------------------------------------------------------------------------
  146. // test match all
  147. // single
  148. matches = [fm gtm_filePathsWithExtension:nil inDirectory:baseDir_];
  149. STAssertEqualObjects([NSSet setWithArray:matches],
  150. [NSSet setWithArray:allFiles],
  151. @"didn't get all files for nil extension");
  152. matches = [fm gtm_filePathsWithExtension:@"" inDirectory:baseDir_];
  153. STAssertEqualObjects([NSSet setWithArray:matches],
  154. [NSSet setWithArray:allFiles],
  155. @"didn't get all files for nil extension");
  156. // array
  157. matches = [fm gtm_filePathsWithExtensions:nil inDirectory:baseDir_];
  158. STAssertEqualObjects([NSSet setWithArray:matches],
  159. [NSSet setWithArray:allFiles],
  160. @"didn't get all files for nil extension");
  161. matches = [fm gtm_filePathsWithExtensions:[NSArray array]
  162. inDirectory:baseDir_];
  163. STAssertEqualObjects([NSSet setWithArray:matches],
  164. [NSSet setWithArray:allFiles],
  165. @"didn't get all files for nil extension");
  166. // --------------------------------------------------------------------------
  167. // test match something
  168. // single
  169. extensions = [NSArray arrayWithObject:@"txt"];
  170. matches = [fm gtm_filePathsWithExtension:@"txt" inDirectory:baseDir_];
  171. expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  172. STAssertEqualObjects([NSSet setWithArray:matches],
  173. [NSSet setWithArray:expectedMatches],
  174. @"didn't get expected files");
  175. // array
  176. matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
  177. expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  178. STAssertEqualObjects([NSSet setWithArray:matches],
  179. [NSSet setWithArray:expectedMatches],
  180. @"didn't get expected files");
  181. extensions = [NSArray arrayWithObjects:@"txt", @"rtf", @"xyz", nil];
  182. matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
  183. expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  184. STAssertEqualObjects([NSSet setWithArray:matches],
  185. [NSSet setWithArray:expectedMatches],
  186. @"didn't get expected files");
  187. // --------------------------------------------------------------------------
  188. // test match nothing
  189. // single
  190. extensions = [NSArray arrayWithObject:@"xyz"];
  191. matches = [fm gtm_filePathsWithExtension:@"xyz" inDirectory:baseDir_];
  192. expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  193. STAssertEqualObjects([NSSet setWithArray:matches],
  194. [NSSet setWithArray:expectedMatches],
  195. @"didn't get expected files");
  196. // array
  197. matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
  198. expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  199. STAssertEqualObjects([NSSet setWithArray:matches],
  200. [NSSet setWithArray:expectedMatches],
  201. @"didn't get expected files");
  202. // --------------------------------------------------------------------------
  203. // test match an empty dir
  204. // create the empty dir
  205. NSString *emptyDir = [baseDir_ stringByAppendingPathComponent:@"emptyDir"];
  206. #if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
  207. NSError *error = nil;
  208. STAssertTrue([fm createDirectoryAtPath:emptyDir
  209. withIntermediateDirectories:YES
  210. attributes:nil
  211. error:&error],
  212. @"Can't create %@ (%@)", emptyDir, error);
  213. #else
  214. STAssertTrue([fm createDirectoryAtPath:emptyDir attributes:nil], nil);
  215. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  216. // single
  217. matches = [fm gtm_filePathsWithExtension:@"txt" inDirectory:emptyDir];
  218. STAssertEqualObjects([NSSet setWithArray:matches], [NSSet set],
  219. @"expected empty dir");
  220. // array
  221. matches = [fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
  222. inDirectory:emptyDir];
  223. STAssertEqualObjects([NSSet setWithArray:matches], [NSSet set],
  224. @"expected empty dir");
  225. }
  226. @end