From ad0ea07252f58734a29887a641584c16c9938857 Mon Sep 17 00:00:00 2001 From: Daniel Jalkut Date: Sun, 11 Nov 2018 07:10:12 -0500 Subject: [PATCH] Fix unit tests (#1311) * Quiet warnings from Swift compiler: "Using '!' here is deprecated and will be removed in a future release" * Ignore diagnostics that yield 'messaging unqualified id' warnings when calling XCTAssertNotEqualObjects. I have filed a bug with Apple, Radar #45968630, requesting that they quiet the warning internally to the macro implementation. * When setting the access time on a directory and its contents, set the access time on the directory last so that it will be guaranteed to match the expected value. There is a side-effect of calling getFileSystemRepresentation that it sets the access time of its container folder, presumably because of an implementatiton detail of how Foundation builds up the path. Note there is another problem here that the order of items from NSDirectoryEnumerator is undefined, so some of the interior items of a directory will undoubtedly end up getting slightly varying access times set upon them. However, for the purposes of Sparkle's use of this method to "keep things alive" in /tmp, I don't think there is an issue. This fix addresses a failing unit test testUpdateFileAccessTime, which was failing because of very slight differences in access time created by the problem described. --- Sparkle/SUFileManager.m | 22 ++++++++++++++++------ Tests/SUAppcastTest.swift | 4 ++-- Tests/SUBinaryDeltaTest.m | 6 ++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index 92f5f158..5bafe942 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -423,6 +423,12 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { - (BOOL)_updateItemAtURL:(NSURL *)targetURL withAccessTime:(struct timeval)accessTime error:(NSError * __autoreleasing *)error { char path[PATH_MAX] = {0}; + + // NOTE: At least on Mojave 10.14.1, running on an APFS filesystem, the act of asking + // for a path's file system representation causes the access time of the containing folder + // to be updated. Callers should take care when attempting to set a recursive directory's + // access time to ensure that the inner-most items get set first, so that the implicitly + // updated access times are replaced after this side-effect occurs. if (![targetURL.path getFileSystemRepresentation:path maxLength:sizeof(path)]) { if (error != NULL) { *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"File to update modification & access time (%@) cannot be represented as a valid file name.", targetURL.path.lastPathComponent] }]; @@ -486,16 +492,12 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return NO; } - // Only recurse if it's actually a directory. Don't recurse into a - // root-level symbolic link. NSString *rootURLPath = targetURL.path; NSDictionary *rootAttributes = [_fileManager attributesOfItemAtPath:rootURLPath error:nil]; NSString *rootType = [rootAttributes objectForKey:NSFileType]; - - if (![self _updateItemAtURL:targetURL withAccessTime:currentTime error:error]) { - return NO; - } + // Only recurse if it's actually a directory. Don't recurse into a + // root-level symbolic link. if ([rootType isEqualToString:NSFileTypeDirectory]) { // The NSDirectoryEnumerator will avoid recursing into any contained // symbolic links, so no further type checks are needed. @@ -507,6 +509,14 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { } } } + + // Set the access time on the container last because the process of setting the access + // time on children actually causes the access time of the container directory to be + // updated. + if (![self _updateItemAtURL:targetURL withAccessTime:currentTime error:error]) { + return NO; + } + return YES; } diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 20f25b2c..c22a7821 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -17,7 +17,7 @@ class SUAppcastTest: XCTestCase { let testData = NSData(contentsOfFile: testFile)! do { - let items = try appcast.parseAppcastItems(fromXMLData: testData as Data!) as! [SUAppcastItem]; + let items = try appcast.parseAppcastItems(fromXMLData: testData as Data) as! [SUAppcastItem]; XCTAssertEqual(4, items.count); @@ -72,7 +72,7 @@ class SUAppcastTest: XCTestCase { let testData = NSData(contentsOfFile: testFile)! do { - let items = try appcast.parseAppcastItems(fromXMLData: testData as Data!) as! [SUAppcastItem]; + let items = try appcast.parseAppcastItems(fromXMLData: testData as Data) as! [SUAppcastItem]; XCTAssertEqual(2, items.count); diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index 544be1ff..4a03575f 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -27,7 +27,10 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec NSString *tmp1 = temporaryDirectory(@"Sparklęエンジン"); NSString *tmp2 = temporaryDirectory(@"Sparklęエンジン"); NSLog(@"Temporary directories: %@, %@", tmp1, tmp2); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-messaging-id" XCTAssertNotEqualObjects(tmp1, tmp2); +#pragma clang diagnostic pop XCTAssert(YES, @"Pass"); } @@ -36,7 +39,10 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec NSString *tmp1 = temporaryFilename(@"Sparklęエンジン"); NSString *tmp2 = temporaryFilename(@"Sparklęエンジン"); NSLog(@"Temporary files: %@, %@", tmp1, tmp2); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-messaging-id" XCTAssertNotEqualObjects(tmp1, tmp2); +#pragma clang diagnostic pop XCTAssert(YES, @"Pass"); }