PageRenderTime 100ms CodeModel.GetById 44ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19#import "GTMSenTestCase.h"
 20#import "GTMNSFileManager+Path.h"
 21#import "GTMNSFileHandle+UniqueName.h"
 22
 23@interface GTMNSFileManager_PathTest : GTMTestCase {
 24  NSString *baseDir_;
 25}
 26@end
 27
 28@implementation GTMNSFileManager_PathTest
 29
 30- (void)setUp {
 31  // make a directory to scribble in
 32  NSFileManager *fm = [NSFileManager defaultManager];
 33  baseDir_
 34    = [[fm gtm_createTemporaryDirectoryBasedOn:@"GTMNSFileManager_PathTestXXXXXX"]
 35       retain];
 36}
 37
 38- (void)tearDown {
 39  if (baseDir_) {
 40    // clean up our directory
 41    NSFileManager *fm = [NSFileManager defaultManager];
 42#if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
 43    NSError *error = nil;
 44    [fm removeItemAtPath:baseDir_ error:&error];
 45    STAssertNil(error,
 46                @"Unable to delete %@: %@", baseDir_, error);
 47#else
 48    [fm removeFileAtPath:baseDir_ handler:nil];
 49#endif
 50
 51    [baseDir_ release];
 52    baseDir_ = nil;
 53  }
 54}
 55
 56#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 57
 58- (void)testCreateFullPathToDirectoryAttributes {
 59  STAssertNotNil(baseDir_, @"setUp failed");
 60
 61  NSString *testPath =
 62    [baseDir_ stringByAppendingPathComponent:@"/foo/bar/baz"];
 63  STAssertNotNil(testPath, nil);
 64  NSFileManager *fm = [NSFileManager defaultManager];
 65
 66  STAssertFalse([fm fileExistsAtPath:testPath],
 67                @"You must delete '%@' before running this test", testPath);
 68
 69  STAssertFalse([fm gtm_createFullPathToDirectory:nil attributes:nil],
 70                @"didn't fail on nil input");
 71
 72  STAssertTrue([fm gtm_createFullPathToDirectory:testPath attributes:nil],
 73               @"Failed to create nested testPath");
 74  STAssertTrue([fm gtm_createFullPathToDirectory:testPath attributes:nil],
 75               @"Failed to succeed on second create of testPath");
 76
 77  NSString *pathToFail = [@"/etc" stringByAppendingPathComponent:testPath];
 78  STAssertFalse([fm gtm_createFullPathToDirectory:pathToFail attributes:nil],
 79                @"We were allowed to create a dir in '/etc'?!");
 80
 81  STAssertFalse([fm gtm_createFullPathToDirectory:nil attributes:nil],
 82                @"Should have failed when passed (nil)");
 83}
 84
 85#endif  // GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 86
 87- (void)testfilePathsWithExtensionsInDirectory {
 88  STAssertNotNil(baseDir_, @"setUp failed");
 89
 90  NSFileManager *fm = [NSFileManager defaultManager];
 91  NSString *bogusPath = @"/some/place/that/does/not/exist";
 92
 93  // --------------------------------------------------------------------------
 94  // test fail cases first
 95
 96  // single
 97  STAssertNil([fm gtm_filePathsWithExtension:nil inDirectory:nil],
 98              @"shouldn't have gotten anything for nil dir");
 99  STAssertNil([fm gtm_filePathsWithExtension:@"txt" inDirectory:nil],
100              @"shouldn't have gotten anything for nil dir");
101  STAssertNil([fm gtm_filePathsWithExtension:@"txt" inDirectory:bogusPath],
102              @"shouldn't have gotten anything for a bogus dir");
103  // array
104  STAssertNil([fm gtm_filePathsWithExtensions:nil inDirectory:nil],
105              @"shouldn't have gotten anything for nil dir");
106  STAssertNil([fm gtm_filePathsWithExtensions:[NSArray array] inDirectory:nil],
107              @"shouldn't have gotten anything for nil dir");
108  STAssertNil([fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
109                                  inDirectory:nil],
110              @"shouldn't have gotten anything for nil dir");
111  STAssertNil([fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
112                                  inDirectory:bogusPath],
113              @"shouldn't have gotten anything for a bogus dir");
114
115  // --------------------------------------------------------------------------
116  // create some test data
117
118  NSString *testDirs[] = {
119    @"", @"/foo",  // mave a subdir to make sure we don't match w/in it
120  };
121  NSString *testFiles[] = {
122    @"a.txt", @"b.txt", @"c.rtf", @"d.m",
123  };
124
125  for (size_t i = 0; i < sizeof(testDirs) / sizeof(NSString*); i++) {
126    NSString *testDir = nil;
127    if ([testDirs[i] length]) {
128      testDir = [baseDir_ stringByAppendingPathComponent:testDirs[i]];
129#if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
130      NSError *error = nil;
131      STAssertTrue([fm createDirectoryAtPath:testDir
132                 withIntermediateDirectories:YES
133                                  attributes:nil
134                                       error:&error],
135                   @"Can't create %@ (%@)", testDir, error);
136#else
137      STAssertTrue([fm createDirectoryAtPath:testDir attributes:nil], nil);
138#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
139    } else {
140      testDir = baseDir_;
141    }
142    for (size_t j = 0; j < sizeof(testFiles) / sizeof(NSString*); j++) {
143      NSString *testFile = [testDir stringByAppendingPathComponent:testFiles[j]];
144      NSError *err = nil;
145      STAssertTrue([@"test" writeToFile:testFile
146                             atomically:YES
147                               encoding:NSUTF8StringEncoding
148                                  error:&err], @"Error: %@", err);
149    }
150  }
151
152  // build set of the top level items
153  NSMutableArray *allFiles = [NSMutableArray array];
154  for (size_t i = 0; i < sizeof(testDirs) / sizeof(NSString*); i++) {
155    if ([testDirs[i] length]) {
156      NSString *testDir = [baseDir_ stringByAppendingPathComponent:testDirs[i]];
157      [allFiles addObject:testDir];
158    }
159  }
160  for (size_t j = 0; j < sizeof(testFiles) / sizeof(NSString*); j++) {
161    NSString *testFile = [baseDir_ stringByAppendingPathComponent:testFiles[j]];
162    [allFiles addObject:testFile];
163  }
164
165  NSArray *matches = nil;
166  NSArray *expectedMatches = nil;
167  NSArray *extensions = nil;
168
169  // NOTE: we do all compares w/ sets so order doesn't matter
170
171  // --------------------------------------------------------------------------
172  // test match all
173
174  // single
175  matches = [fm gtm_filePathsWithExtension:nil inDirectory:baseDir_];
176  STAssertEqualObjects([NSSet setWithArray:matches],
177                       [NSSet setWithArray:allFiles],
178                       @"didn't get all files for nil extension");
179  matches = [fm gtm_filePathsWithExtension:@"" inDirectory:baseDir_];
180  STAssertEqualObjects([NSSet setWithArray:matches],
181                       [NSSet setWithArray:allFiles],
182                       @"didn't get all files for nil extension");
183  // array
184  matches = [fm gtm_filePathsWithExtensions:nil inDirectory:baseDir_];
185  STAssertEqualObjects([NSSet setWithArray:matches],
186                       [NSSet setWithArray:allFiles],
187                       @"didn't get all files for nil extension");
188  matches = [fm gtm_filePathsWithExtensions:[NSArray array]
189                                inDirectory:baseDir_];
190  STAssertEqualObjects([NSSet setWithArray:matches],
191                       [NSSet setWithArray:allFiles],
192                       @"didn't get all files for nil extension");
193
194  // --------------------------------------------------------------------------
195  // test match something
196
197  // single
198  extensions = [NSArray arrayWithObject:@"txt"];
199  matches = [fm gtm_filePathsWithExtension:@"txt" inDirectory:baseDir_];
200  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
201  STAssertEqualObjects([NSSet setWithArray:matches],
202                       [NSSet setWithArray:expectedMatches],
203                       @"didn't get expected files");
204  // array
205  matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
206  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
207  STAssertEqualObjects([NSSet setWithArray:matches],
208                       [NSSet setWithArray:expectedMatches],
209                       @"didn't get expected files");
210  extensions = [NSArray arrayWithObjects:@"txt", @"rtf", @"xyz", nil];
211  matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
212  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
213  STAssertEqualObjects([NSSet setWithArray:matches],
214                       [NSSet setWithArray:expectedMatches],
215                       @"didn't get expected files");
216
217  // --------------------------------------------------------------------------
218  // test match nothing
219
220  // single
221  extensions = [NSArray arrayWithObject:@"xyz"];
222  matches = [fm gtm_filePathsWithExtension:@"xyz" inDirectory:baseDir_];
223  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
224  STAssertEqualObjects([NSSet setWithArray:matches],
225                       [NSSet setWithArray:expectedMatches],
226                       @"didn't get expected files");
227  // array
228  matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
229  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
230  STAssertEqualObjects([NSSet setWithArray:matches],
231                       [NSSet setWithArray:expectedMatches],
232                       @"didn't get expected files");
233
234  // --------------------------------------------------------------------------
235  // test match an empty dir
236
237  // create the empty dir
238  NSString *emptyDir = [baseDir_ stringByAppendingPathComponent:@"emptyDir"];
239#if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
240  NSError *error = nil;
241  STAssertTrue([fm createDirectoryAtPath:emptyDir
242             withIntermediateDirectories:YES
243                              attributes:nil
244                                   error:&error],
245               @"Can't create %@ (%@)", emptyDir, error);
246#else
247  STAssertTrue([fm createDirectoryAtPath:emptyDir attributes:nil], nil);
248#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
249
250
251  // single
252  matches = [fm gtm_filePathsWithExtension:@"txt" inDirectory:emptyDir];
253  STAssertEqualObjects([NSSet setWithArray:matches], [NSSet set],
254                       @"expected empty dir");
255  // array
256  matches = [fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
257                                inDirectory:emptyDir];
258  STAssertEqualObjects([NSSet setWithArray:matches], [NSSet set],
259                       @"expected empty dir");
260
261}
262
263@end