aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSFileManager+PathTest.m
blob: a0f61fb9fcd4737467e66b056479ec9d2094213d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
//  GTMNSFileManager+PathTest.m
//
//  Copyright 2006-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import "GTMNSFileManager+Path.h"
#import "GTMNSFileHandle+UniqueName.h"

@interface GTMNSFileManager_PathTest : GTMTestCase {
  NSString *baseDir_;
}
@end

@implementation GTMNSFileManager_PathTest

- (void)setUp {
  // make a directory to scribble in
  NSFileManager *fm = [NSFileManager defaultManager];
  baseDir_
    = [[fm gtm_createTemporaryDirectoryBasedOn:@"GTMNSFileManager_PathTestXXXXXX"]
       retain];
}

- (void)tearDown {
  if (baseDir_) {
    // clean up our directory
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    [fm removeItemAtPath:baseDir_ error:&error];
    XCTAssertNil(error, @"Unable to delete %@: %@", baseDir_, error);

    [baseDir_ release];
    baseDir_ = nil;
  }
}

- (void)testfilePathsWithExtensionsInDirectory {
  XCTAssertNotNil(baseDir_, @"setUp failed");

  NSFileManager *fm = [NSFileManager defaultManager];
  NSString *bogusPath = @"/some/place/that/does/not/exist";

  // --------------------------------------------------------------------------
  // test fail cases first

  // single
  XCTAssertNil([fm gtm_filePathsWithExtension:nil inDirectory:nil],
               @"shouldn't have gotten anything for nil dir");
  XCTAssertNil([fm gtm_filePathsWithExtension:@"txt" inDirectory:nil],
               @"shouldn't have gotten anything for nil dir");
  XCTAssertNil([fm gtm_filePathsWithExtension:@"txt" inDirectory:bogusPath],
               @"shouldn't have gotten anything for a bogus dir");
  // array
  XCTAssertNil([fm gtm_filePathsWithExtensions:nil inDirectory:nil],
               @"shouldn't have gotten anything for nil dir");
  XCTAssertNil([fm gtm_filePathsWithExtensions:[NSArray array] inDirectory:nil],
               @"shouldn't have gotten anything for nil dir");
  XCTAssertNil([fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
                                   inDirectory:nil],
               @"shouldn't have gotten anything for nil dir");
  XCTAssertNil([fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
                                   inDirectory:bogusPath],
               @"shouldn't have gotten anything for a bogus dir");

  // --------------------------------------------------------------------------
  // create some test data

  NSString *testDirs[] = {
    @"", @"/foo",  // mave a subdir to make sure we don't match w/in it
  };
  NSString *testFiles[] = {
    @"a.txt", @"b.txt", @"c.rtf", @"d.m",
  };

  for (size_t i = 0; i < sizeof(testDirs) / sizeof(NSString*); i++) {
    NSString *testDir = nil;
    if ([testDirs[i] length]) {
      testDir = [baseDir_ stringByAppendingPathComponent:testDirs[i]];
      NSError *error = nil;
      XCTAssertTrue([fm createDirectoryAtPath:testDir
                  withIntermediateDirectories:YES
                                   attributes:nil
                                        error:&error],
                    @"Can't create %@ (%@)", testDir, error);
    } else {
      testDir = baseDir_;
    }
    for (size_t j = 0; j < sizeof(testFiles) / sizeof(NSString*); j++) {
      NSString *testFile = [testDir stringByAppendingPathComponent:testFiles[j]];
      NSError *err = nil;
      XCTAssertTrue([@"test" writeToFile:testFile
                              atomically:YES
                                encoding:NSUTF8StringEncoding
                                   error:&err], @"Error: %@", err);
    }
  }

  // build set of the top level items
  NSMutableArray *allFiles = [NSMutableArray array];
  for (size_t i = 0; i < sizeof(testDirs) / sizeof(NSString*); i++) {
    if ([testDirs[i] length]) {
      NSString *testDir = [baseDir_ stringByAppendingPathComponent:testDirs[i]];
      [allFiles addObject:testDir];
    }
  }
  for (size_t j = 0; j < sizeof(testFiles) / sizeof(NSString*); j++) {
    NSString *testFile = [baseDir_ stringByAppendingPathComponent:testFiles[j]];
    [allFiles addObject:testFile];
  }

  NSArray *matches = nil;
  NSArray *expectedMatches = nil;
  NSArray *extensions = nil;

  // NOTE: we do all compares w/ sets so order doesn't matter

  // --------------------------------------------------------------------------
  // test match all

  // single
  matches = [fm gtm_filePathsWithExtension:nil inDirectory:baseDir_];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:allFiles],
                        @"didn't get all files for nil extension");
  matches = [fm gtm_filePathsWithExtension:@"" inDirectory:baseDir_];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:allFiles],
                        @"didn't get all files for nil extension");
  // array
  matches = [fm gtm_filePathsWithExtensions:nil inDirectory:baseDir_];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:allFiles],
                        @"didn't get all files for nil extension");
  matches = [fm gtm_filePathsWithExtensions:[NSArray array]
                                inDirectory:baseDir_];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:allFiles],
                        @"didn't get all files for nil extension");

  // --------------------------------------------------------------------------
  // test match something

  // single
  extensions = [NSArray arrayWithObject:@"txt"];
  matches = [fm gtm_filePathsWithExtension:@"txt" inDirectory:baseDir_];
  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:expectedMatches],
                        @"didn't get expected files");
  // array
  matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:expectedMatches],
                        @"didn't get expected files");
  extensions = [NSArray arrayWithObjects:@"txt", @"rtf", @"xyz", nil];
  matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:expectedMatches],
                        @"didn't get expected files");

  // --------------------------------------------------------------------------
  // test match nothing

  // single
  extensions = [NSArray arrayWithObject:@"xyz"];
  matches = [fm gtm_filePathsWithExtension:@"xyz" inDirectory:baseDir_];
  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:expectedMatches],
                        @"didn't get expected files");
  // array
  matches = [fm gtm_filePathsWithExtensions:extensions inDirectory:baseDir_];
  expectedMatches = [allFiles pathsMatchingExtensions:extensions];
  XCTAssertEqualObjects([NSSet setWithArray:matches],
                        [NSSet setWithArray:expectedMatches],
                        @"didn't get expected files");

  // --------------------------------------------------------------------------
  // test match an empty dir

  // create the empty dir
  NSString *emptyDir = [baseDir_ stringByAppendingPathComponent:@"emptyDir"];
  NSError *error = nil;
  XCTAssertTrue([fm createDirectoryAtPath:emptyDir
              withIntermediateDirectories:YES
                               attributes:nil
                                    error:&error],
                @"Can't create %@ (%@)", emptyDir, error);

  // single
  matches = [fm gtm_filePathsWithExtension:@"txt" inDirectory:emptyDir];
  XCTAssertEqualObjects([NSSet setWithArray:matches], [NSSet set],
                        @"expected empty dir");
  // array
  matches = [fm gtm_filePathsWithExtensions:[NSArray arrayWithObject:@"txt"]
                                inDirectory:emptyDir];
  XCTAssertEqualObjects([NSSet setWithArray:matches], [NSSet set],
                        @"expected empty dir");
}

@end