2011-02-16 08:21:17 +00:00
# import "Testing.h"
# import "ObjectTesting.h"
# import < Foundation / NSAutoreleasePool . h >
2018-01-02 12:48:18 +00:00
# import < Foundation / NSError . h >
2011-02-16 08:21:17 +00:00
# import < Foundation / NSFileManager . h >
# import < Foundation / NSProcessInfo . h >
# import < Foundation / NSPathUtilities . h >
2012-07-08 15:50:58 +00:00
# import < Foundation / NSError . h >
2013-03-05 11:51:14 +00:00
# import < Foundation / NSURL . h >
2011-02-16 08:21:17 +00:00
2018-01-02 12:48:18 +00:00
@ interface MyHandler : NSObject
{
@ public
NSString * path ;
NSError * error ;
}
- ( BOOL ) fileManager : ( NSFileManager * ) m shouldProceedAfterError : ( NSError * ) e ;
- ( void ) fileManager : ( NSFileManager * ) m willProcessPath : ( NSString * ) p ;
- ( void ) reset ;
@ end
@ implementation MyHandler
- ( void ) dealloc
{
[ self reset ] ;
[ super dealloc ] ;
}
- ( BOOL ) fileManager : ( NSFileManager * ) m shouldProceedAfterError : ( NSError * ) e
{
ASSIGN ( error , e ) ;
return NO ;
}
- ( void ) fileManager : ( NSFileManager * ) m willProcessPath : ( NSString * ) p
{
ASSIGN ( path , p ) ;
}
- ( void ) reset
{
DESTROY ( path ) ;
DESTROY ( error ) ;
}
@ end
2011-02-16 08:21:17 +00:00
int main ( )
{
NSAutoreleasePool * arp = [ NSAutoreleasePool new ] ;
NSFileManager * mgr = [ NSFileManager defaultManager ] ;
NSString * dir = @ "NSFileManagerTestDir" ;
2018-01-02 12:48:18 +00:00
MyHandler * handler = [ MyHandler new ] ;
2012-07-08 15:50:58 +00:00
NSDictionary * attr ;
2011-10-02 11:17:49 +00:00
NSString * dirInDir ;
2011-02-16 08:21:17 +00:00
NSString * str1 , * str2 ;
2015-08-24 09:25:24 +00:00
NSString * tmp ;
2012-03-03 09:19:41 +00:00
NSError * err ;
2012-07-08 15:50:58 +00:00
NSDictionary * errInfo ;
BOOL exists ;
BOOL isDir ;
2011-10-02 11:17:49 +00:00
2013-03-05 14:06:25 +00:00
dirInDir = [ dir stringByAppendingPathComponent : @ "WithinDirectory" ] ;
2011-10-02 11:17:49 +00:00
2011-02-16 08:21:17 +00:00
PASS ( mgr ! = nil && [ mgr isKindOfClass : [ NSFileManager class ] ] ,
"NSFileManager understands +defaultManager" ) ;
2012-07-08 15:50:58 +00:00
/ * remove test directory if it exists * /
exists = [ mgr fileExistsAtPath : dir isDirectory : & isDir ] ;
if ( exists )
{
2018-01-02 12:48:18 +00:00
[ mgr removeFileAtPath : dir handler : handler ] ;
2012-07-08 15:50:58 +00:00
}
2011-02-16 08:21:17 +00:00
PASS ( [ mgr fileAttributesAtPath : dir traverseLink : NO ] = = nil ,
"NSFileManager returns nil for attributes of non-existent file" ) ;
2012-07-08 15:50:58 +00:00
PASS ( [ mgr createDirectoryAtPath : dir attributes : nil ] ,
"NSFileManager can create a directory" ) ;
PASS ( [ mgr fileExistsAtPath : dir isDirectory : & isDir ] &&
isDir = = YES ,
"exists and is a directory" ) ;
2017-06-18 08:13:57 +00:00
PASS ( NO = = [ mgr createDirectoryAtPath : dir attributes : nil ] ,
"-createDirectoryAtPath:attributes: fails for existing directory" ) ;
PASS ( NO = = [ mgr createDirectoryAtPath : dir withIntermediateDirectories : NO
attributes : nil error : 0 ] ,
"-createDirectoryAtPath:withIntermediateDirectories:attributes:error:"
" fails for existing directory if flag is NO" ) ;
PASS ( YES = = [ mgr createDirectoryAtPath : dir withIntermediateDirectories : YES
attributes : nil error : 0 ] ,
"-createDirectoryAtPath:withIntermediateDirectories:attributes:error:"
" succeeds for existing directory if flag is YES" ) ;
2012-07-08 15:50:58 +00:00
PASS ( [ mgr fileAttributesAtPath : dir traverseLink : NO ] ! = nil ,
"NSFileManager returns non-nil for attributes of existing file" ) ;
attr = [ mgr fileAttributesAtPath : dir traverseLink : NO ] ;
PASS ( attr ! = nil ,
"NSFileManager returns non-nil for attributes of existing file" ) ;
PASS ( [ NSUserName ( ) isEqual : [ attr fileOwnerAccountName ] ] ,
"newly created file is owned by current user" ) ;
2017-06-18 08:13:57 +00:00
// NSLog ( @ "'%@', '%@'" , NSUserName ( ) , [ attr fileOwnerAccountName ] ) ;
2013-03-05 12:14:36 +00:00
err = ( id ) ( void * ) 42 ;
2012-07-08 15:50:58 +00:00
attr = [ mgr attributesOfItemAtPath : dir error : & err ] ;
2013-03-05 12:14:36 +00:00
PASS ( attr ! = nil && err = = ( id ) ( void * ) 42 ,
2012-07-08 15:50:58 +00:00
"[NSFileManager attributesOfItemAtPath:error:] returns non-nil for "
2013-03-05 12:14:36 +00:00
"attributes and leaves error unchanged in the case of existing file" ) ;
2012-07-08 15:50:58 +00:00
attr = [ mgr attributesOfItemAtPath :
[ dir stringByAppendingPathComponent :
@ "thispathMUSTNOTexistatyoursystem" ] error : & err ] ;
PASS ( attr = = nil && err ! = nil ,
"[NSFileManager attributesOfItemAtPath:error:] returns nil for "
"attributes and non-nil for error in the case of non-existent file" ) ;
2011-02-16 08:21:17 +00:00
PASS ( [ mgr changeCurrentDirectoryPath : dir ] ,
"NSFileManager can change directories" ) ;
{
NSString * dir1 = [ mgr currentDirectoryPath ] ;
PASS ( dir1 ! = nil && [ [ dir1 lastPathComponent ] isEqualToString : dir ] ,
"NSFileManager can get current dir" ) ;
}
str1 = @ "A string" ;
PASS ( [ mgr createFileAtPath : @ "NSFMFile"
contents : [ str1 dataUsingEncoding : 1 ]
attributes : nil ] ,
"NSFileManager creates a file" ) ;
PASS ( [ mgr fileExistsAtPath : @ "NSFMFile" ] , "-fileExistsAtPath: agrees" ) ;
2011-10-02 11:17:49 +00:00
{
NSArray * a ;
a = [ mgr contentsOfDirectoryAtPath : @ "." error : 0 ] ;
PASS ( 1 = = [ a count ] && [ [ a lastObject ] isEqual : @ "NSFMFile" ] ,
"-contentsOfDirectoryAtPath: agrees" ) ;
}
2011-02-16 08:21:17 +00:00
{
NSData * dat1 = [ mgr contentsAtPath : @ "NSFMFile" ] ;
str2 = [ [ NSString alloc ] initWithData : dat1 encoding : 1 ] ;
PASS ( [ str1 isEqualToString : str2 ] , "NSFileManager file contents match" ) ;
}
2018-01-02 12:48:18 +00:00
[ handler reset ] ;
2011-02-16 08:21:17 +00:00
PASS ( [ mgr copyPath : @ "NSFMFile"
toPath : @ "NSFMCopy"
2018-01-02 12:48:18 +00:00
handler : handler ] ,
2011-02-16 08:21:17 +00:00
"NSFileManager copies a file" ) ;
PASS ( [ mgr fileExistsAtPath : @ "NSFMCopy" ] , "-fileExistsAtPath: agrees" ) ;
{
NSData * dat1 = [ mgr contentsAtPath : @ "NSFMCopy" ] ;
str2 = [ [ NSString alloc ] initWithData : dat1 encoding : 1 ] ;
PASS ( [ str1 isEqual : str2 ] , "NSFileManager copied file contents match" ) ;
}
2020-07-29 13:58:21 +00:00
NSDictionary * oa = [ mgr fileAttributesAtPath : @ "NSFMFile" traverseLink : NO ] ;
NSDictionary * na = [ mgr fileAttributesAtPath : @ "NSFMCopy" traverseLink : NO ] ;
2020-07-30 15:39:47 +00:00
PASS ( EQ ( [ [ oa fileCreationDate ] timeIntervalSinceReferenceDate ] , [ [ na fileCreationDate ] timeIntervalSinceReferenceDate ] ) , "copy creation date equals original" )
PASS ( EQ ( [ [ oa fileModificationDate ] timeIntervalSinceReferenceDate ] , [ [ na fileModificationDate ] timeIntervalSinceReferenceDate ] ) , "copy modification date equals original" )
2020-07-29 13:58:21 +00:00
2011-02-16 08:21:17 +00:00
PASS ( [ mgr movePath : @ "NSFMFile"
toPath : @ "NSFMMove"
2018-01-02 12:48:18 +00:00
handler : handler ] ,
2011-02-16 08:21:17 +00:00
"NSFileManager moves a file" ) ;
PASS ( [ mgr fileExistsAtPath : @ "NSFMMove" ] ,
"NSFileManager move destination exists" ) ;
PASS ( ! [ mgr fileExistsAtPath : @ "NSFMFile" ] ,
"NSFileManager move source doesn't exist" ) ;
{
NSData * dat1 = [ mgr contentsAtPath : @ "NSFMMove" ] ;
str2 = [ [ NSString alloc ] initWithData : dat1 encoding : 1 ] ;
2018-01-02 12:48:18 +00:00
PASS ( [ str1 isEqualToString : str2 ] , "NSFileManager moved file contents match" )
2011-02-16 08:21:17 +00:00
}
2018-01-02 12:48:18 +00:00
PASS ( ! [ mgr copyPath : @ "NSFMFile"
toPath : @ "NSFMDestination"
handler : handler ] ,
"NSFileManager does not copy nonexistent file" )
2011-02-16 08:21:17 +00:00
if ( [ [ NSProcessInfo processInfo ] operatingSystem ]
! = NSWindowsNTOperatingSystem )
{
PASS ( [ mgr createSymbolicLinkAtPath : @ "NSFMLink" pathContent : @ "NSFMMove" ] ,
"NSFileManager creates a symbolic link" ) ;
PASS ( [ mgr fileExistsAtPath : @ "NSFMLink" ] , "link exists" ) ;
2018-01-02 12:48:18 +00:00
PASS ( [ mgr removeFileAtPath : @ "NSFMLink" handler : handler ] ,
2011-02-16 08:21:17 +00:00
"NSFileManager removes a symbolic link" ) ;
PASS ( ! [ mgr fileExistsAtPath : @ "NSFMLink" ] ,
"NSFileManager removed link doesn't exist" ) ;
PASS ( [ mgr fileExistsAtPath : @ "NSFMMove" ] ,
"NSFileManager removed link's target still exists" ) ;
}
2018-01-02 12:48:18 +00:00
PASS ( [ mgr removeFileAtPath : @ "NSFMMove" handler : handler ] ,
2011-02-16 08:21:17 +00:00
"NSFileManager removes a file" ) ;
PASS ( ! [ mgr fileExistsAtPath : @ "NSFMMove" ] ,
"NSFileManager removed file doesn't exist" ) ;
PASS ( [ mgr isReadableFileAtPath : @ "NSFMCopy" ] ,
"NSFileManager isReadableFileAtPath: works" ) ;
PASS ( [ mgr isWritableFileAtPath : @ "NSFMCopy" ] ,
"NSFileManager isWritableFileAtPath: works" ) ;
PASS ( [ mgr isDeletableFileAtPath : @ "NSFMCopy" ] ,
"NSFileManager isDeletableFileAtPath: works" ) ;
PASS ( ! [ mgr isExecutableFileAtPath : @ "NSFMCopy" ] ,
"NSFileManager isExecutableFileAtPath: works" ) ;
2018-01-02 12:48:18 +00:00
PASS_EXCEPTION ( [ mgr removeFileAtPath : @ "." handler : handler ] ; ,
2011-02-24 16:26:01 +00:00
NSInvalidArgumentException ,
2011-02-16 08:21:17 +00:00
"NSFileManager -removeFileAtPath: @\" . \ " throws exception" ) ;
PASS ( [ mgr createDirectoryAtPath : @ "subdir" attributes : nil ] ,
"NSFileManager can create a subdirectory" ) ;
PASS ( [ mgr changeCurrentDirectoryPath : @ "subdir" ] ,
"NSFileManager can move into subdir" ) ;
2011-04-07 05:09:08 +00:00
2013-03-05 13:42:37 +00:00
[ mgr createDirectoryAtPath : @ "sub1" attributes : nil ] ;
[ mgr createFileAtPath : @ "sub1/x"
contents : [ @ "hello" dataUsingEncoding : NSASCIIStringEncoding ]
attributes : nil ] ,
[ mgr createDirectoryAtPath : @ "sub2" attributes : nil ] ;
[ mgr createFileAtPath : @ "sub2/x"
contents : [ @ "hello" dataUsingEncoding : NSASCIIStringEncoding ]
attributes : nil ] ;
PASS ( YES = = [ mgr contentsEqualAtPath : @ "sub1/x" andPath : @ "sub2/x" ] ,
"directories containing identical files are equal" ) ;
2018-01-02 12:48:18 +00:00
[ mgr removeFileAtPath : @ "sub2/x" handler : handler ] ,
2013-03-05 13:42:37 +00:00
[ mgr createFileAtPath : @ "sub2/x"
contents : [ @ "goodbye" dataUsingEncoding : NSASCIIStringEncoding ]
attributes : nil ] ;
PASS ( NO = = [ mgr contentsEqualAtPath : @ "sub1/x" andPath : @ "sub2/x" ] ,
"directories containing files with different content are not equal" ) ;
2018-01-02 12:48:18 +00:00
PASS ( YES = = [ mgr removeFileAtPath : @ "sub1" handler : handler ] ,
2015-08-24 09:25:24 +00:00
"sub1 removed" ) ;
2018-01-02 12:48:18 +00:00
PASS ( YES = = [ mgr removeFileAtPath : @ "sub2" handler : handler ] ,
2015-08-24 09:25:24 +00:00
"sub2 removed" ) ;
2013-03-05 13:42:37 +00:00
2013-03-05 12:14:36 +00:00
err = nil ;
2012-07-08 15:50:58 +00:00
PASS ( [ mgr createDirectoryAtPath : dirInDir
withIntermediateDirectories : NO
attributes : nil
2013-03-05 12:14:36 +00:00
error : & err ] = = NO ,
2012-07-08 15:50:58 +00:00
"NSFileManager refuses to create intermediate directories" ) ;
2013-03-05 12:14:36 +00:00
PASS ( err ! = nil , "error value is set" ) ;
PASS_EQUAL ( [ err domain ] , NSCocoaErrorDomain , "cocoa error domain" ) ;
PASS ( ( errInfo = [ err userInfo ] ) ! = nil , "error user info is set" ) ;
PASS ( [ errInfo objectForKey : NSFilePathErrorKey ] ! = nil ,
"error info has a path" ) ;
2011-04-07 05:09:08 +00:00
2013-03-05 12:14:36 +00:00
err = nil ;
2012-07-08 15:50:58 +00:00
PASS ( [ mgr createDirectoryAtPath : dirInDir
withIntermediateDirectories : YES
attributes : nil
error : & err ] && err = = nil ,
"NSFileManager can create intermediate directories" ) ;
PASS ( [ mgr fileExistsAtPath : dirInDir isDirectory : & isDir ] && isDir = = YES ,
"NSFileManager create directory and intermediate directory" ) ;
2013-03-05 11:51:14 +00:00
2015-08-24 09:25:24 +00:00
tmp = [ mgr currentDirectoryPath ] ;
exists = [ mgr fileExistsAtPath : tmp isDirectory : & isDir ] ;
PASS ( YES = = exists && YES = = isDir , "current directory exists" ) ;
tmp = [ tmp stringByDeletingLastPathComponent ] ;
exists = [ mgr fileExistsAtPath : tmp isDirectory : & isDir ] ;
PASS ( YES = = exists && YES = = isDir , "parent directory exists" ) ;
tmp = [ tmp stringByDeletingLastPathComponent ] ;
exists = [ mgr fileExistsAtPath : tmp isDirectory : & isDir ] ;
PASS ( YES = = exists && YES = = isDir , "parent of parent directory exists" ) ;
[ mgr changeCurrentDirectoryPath : tmp ] ;
2013-03-05 11:51:14 +00:00
exists = [ mgr fileExistsAtPath : dir isDirectory : & isDir ] ;
2015-08-24 09:25:24 +00:00
PASS ( YES = = exists && YES = = isDir , "directory exists" ) ;
2013-03-05 14:06:25 +00:00
if ( exists && isDir )
2013-03-05 11:51:14 +00:00
{
2015-09-22 09:46:10 +00:00
dir = [ dir stringByStandardizingPath ] ;
2018-01-02 12:48:18 +00:00
PASS ( [ mgr removeFileAtPath : dir handler : handler ] , "removed directory" ) ;
2015-08-24 09:25:24 +00:00
PASS ( ! [ mgr fileExistsAtPath : dir ] , "directory no longer exists" ) ;
GSPrintf ( stdout , @ "%@\n" , dir ) ;
GSPrintf ( stderr , @ "%@\n" , dir ) ;
2013-03-05 13:42:37 +00:00
}
2013-03-05 11:51:14 +00:00
2013-03-05 12:14:36 +00:00
err = nil ;
2013-03-05 11:51:14 +00:00
PASS ( [ mgr createDirectoryAtURL : [ NSURL fileURLWithPath : dirInDir ]
withIntermediateDirectories : NO
attributes : nil
error : & err ] = = NO
&& err ! = nil && [ [ err domain ] isEqual : NSCocoaErrorDomain ]
&& ( errInfo = [ err userInfo ] ) ! = nil
2013-03-05 12:14:36 +00:00
&& [ errInfo objectForKey : NSFilePathErrorKey ] ! = nil ,
2013-03-05 11:51:14 +00:00
"NSFileManager refuses to create intermediate directories on URL" ) ;
2013-03-05 12:14:36 +00:00
err = nil ;
2013-03-05 11:51:14 +00:00
PASS ( [ mgr createDirectoryAtURL : [ NSURL fileURLWithPath : dirInDir ]
withIntermediateDirectories : YES
attributes : nil
error : & err ] && err = = nil ,
"NSFileManager can create intermediate directories on URL" ) ;
PASS ( [ mgr fileExistsAtPath : dirInDir isDirectory : & isDir ] && isDir = = YES ,
"NSFileManager create directory and intermediate directory on URL" ) ;
2013-03-08 14:07:34 +00:00
[ mgr createDirectoryAtPath : @ "sub1"
withIntermediateDirectories : YES
attributes : nil
error : & err ] ;
[ mgr createDirectoryAtPath : @ "sub2"
withIntermediateDirectories : YES
attributes : nil
error : & err ] ;
[ mgr copyItemAtURL : [ NSURL fileURLWithPath : @ "sub1" ]
toURL : [ NSURL fileURLWithPath : @ "sub2/sub1" ]
error : & err ] ;
PASS ( [ mgr fileExistsAtPath : @ "sub2/sub1" isDirectory : & isDir ]
&& isDir = = YES , "NSFileManager copy item at URL" ) ;
[ mgr copyItemAtPath : @ "sub2" toPath : @ "sub1/sub2" error : & err ] ;
PASS ( [ mgr fileExistsAtPath : @ "sub1/sub2/sub1" isDirectory : & isDir ]
&& isDir = = YES , "NSFileManager copy item at Path" ) ;
[ mgr moveItemAtURL : [ NSURL fileURLWithPath : @ "sub2/sub1" ]
toURL : [ NSURL fileURLWithPath : @ "sub1/moved" ]
error : & err ] ;
PASS ( [ mgr fileExistsAtPath : @ "sub1/moved" isDirectory : & isDir ]
&& isDir = = YES , "NSFileManager move item at URL" ) ;
[ mgr moveItemAtPath : @ "sub1/sub2" toPath : @ "sub2/moved" error : & err ] ;
PASS ( [ mgr fileExistsAtPath : @ "sub2/moved" isDirectory : & isDir ]
&& isDir = = YES , "NSFileManager move item at Path" ) ;
[ mgr removeItemAtURL : [ NSURL fileURLWithPath : @ "sub1" ]
error : & err ] ;
PASS ( [ mgr fileExistsAtPath : @ "sub1" isDirectory : & isDir ] = = NO ,
"NSFileManager remove item at URL" ) ;
[ mgr removeItemAtPath : @ "sub2" error : & err ] ;
PASS ( [ mgr fileExistsAtPath : @ "sub2" isDirectory : & isDir ] = = NO ,
"NSFileManager remove item at Path" ) ;
2011-02-16 08:21:17 +00:00
2018-01-02 12:48:18 +00:00
PASS_EXCEPTION ( [ mgr removeFileAtPath : @ "." handler : handler ] ; ,
2013-03-08 14:07:34 +00:00
NSInvalidArgumentException ,
"NSFileManager -removeFileAtPath: @\" . \ " throws exception" ) ;
2011-02-16 08:21:17 +00:00
2018-01-02 12:48:18 +00:00
PASS_EXCEPTION ( [ mgr removeFileAtPath : @ ".." handler : handler ] ; ,
2013-03-08 14:07:34 +00:00
NSInvalidArgumentException ,
"NSFileManager -removeFileAtPath: @\" . . \ " throws exception" ) ;
2011-02-16 08:21:17 +00:00
/ * clean up * /
2012-07-08 15:50:58 +00:00
[ mgr changeCurrentDirectoryPath : [ [ [ mgr currentDirectoryPath ] stringByDeletingLastPathComponent ] stringByDeletingLastPathComponent ] ] ;
exists = [ mgr fileExistsAtPath : dir isDirectory : & isDir ] ;
2013-03-05 12:14:36 +00:00
if ( exists && isDir )
2012-07-08 15:50:58 +00:00
{
2018-01-02 12:48:18 +00:00
PASS ( [ mgr removeFileAtPath : dir handler : handler ] ,
2012-07-08 15:50:58 +00:00
"NSFileManager removes a directory" ) ;
PASS ( ! [ mgr fileExistsAtPath : dir ] , "directory no longer exists" ) ;
}
2011-02-16 08:21:17 +00:00
[ arp release ] ; arp = nil ;
return 0 ;
}