From 361c51ba4c87290c4160ede6381596938a57ef0c Mon Sep 17 00:00:00 2001 From: Mostafa Berg Date: Thu, 28 Jul 2016 10:52:08 +0200 Subject: [PATCH] +Updated tests to user Swift 3.0, all tests passing --- ZipTests/ZipTests.swift | 151 ++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 76 deletions(-) diff --git a/ZipTests/ZipTests.swift b/ZipTests/ZipTests.swift index 153d01d..bad3179 100644 --- a/ZipTests/ZipTests.swift +++ b/ZipTests/ZipTests.swift @@ -21,10 +21,10 @@ class ZipTests: XCTestCase { func testQuickUnzip() { do { - let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! + let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! let destinationURL = try Zip.quickUnzipFile(filePath) - let fileManager = NSFileManager.defaultManager() - XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) + let fileManager = FileManager.default + XCTAssertTrue(fileManager.fileExists(atPath: destinationURL.path!)) } catch { XCTFail() @@ -33,11 +33,11 @@ class ZipTests: XCTestCase { func testQuickUnzipNonExistingPath() { do { - let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath + let filePathURL = Bundle(for: ZipTests.self).resourcePath let filePath = NSURL(string:"\(filePathURL!)/bb9.zip") - let destinationURL = try Zip.quickUnzipFile(filePath!) - let fileManager = NSFileManager.defaultManager() - XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) + let destinationURL = try Zip.quickUnzipFile(filePath! as URL) + let fileManager = FileManager.default + XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) } catch { XCTAssert(true) @@ -46,10 +46,10 @@ class ZipTests: XCTestCase { func testQuickUnzipNonZipPath() { do { - let filePath = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! + let filePath = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! let destinationURL = try Zip.quickUnzipFile(filePath) - let fileManager = NSFileManager.defaultManager() - XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) + let fileManager = FileManager.default + XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) } catch { XCTAssert(true) @@ -58,7 +58,7 @@ class ZipTests: XCTestCase { func testQuickUnzipProgress() { do { - let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! + let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in XCTAssert(true) }) @@ -71,9 +71,9 @@ class ZipTests: XCTestCase { func testQuickUnzipOnlineURL() { do { let filePath = NSURL(string: "http://www.google.com/google.zip")! - let destinationURL = try Zip.quickUnzipFile(filePath) - let fileManager = NSFileManager.defaultManager() - XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!)) + let destinationURL = try Zip.quickUnzipFile(filePath as URL) + let fileManager = FileManager.default + XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!)) } catch { XCTAssert(true) @@ -82,15 +82,15 @@ class ZipTests: XCTestCase { func testUnzip() { do { - let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! - let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL + let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! + let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL - try Zip.unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: { (progress) -> () in + try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: { (progress) -> () in print(progress) }) - let fileManager = NSFileManager.defaultManager() - XCTAssertTrue(fileManager.fileExistsAtPath(documentsFolder.path!)) + let fileManager = FileManager.default + XCTAssertTrue(fileManager.fileExists(atPath:documentsFolder.path!)) } catch { XCTFail() @@ -99,14 +99,14 @@ class ZipTests: XCTestCase { func testImplicitProgressUnzip() { do { - let progress = NSProgress() + let progress = Progress() progress.totalUnitCount = 1 - let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! - let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL + let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! + let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL - progress.becomeCurrentWithPendingUnitCount(1) - try Zip.unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: nil) + progress.becomeCurrent(withPendingUnitCount: 1) + try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: nil) progress.resignCurrent() XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount) @@ -119,16 +119,16 @@ class ZipTests: XCTestCase { func testImplicitProgressZip() { do { - let progress = NSProgress() + let progress = Progress() progress.totalUnitCount = 1 - let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! - let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip") + let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! + let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") - progress.becomeCurrentWithPendingUnitCount(1) - try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: nil) + progress.becomeCurrent(withPendingUnitCount: 1) + try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: nil, progress: nil) progress.resignCurrent() XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount) @@ -141,11 +141,11 @@ class ZipTests: XCTestCase { func testQuickZip() { do { - let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! + let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive") - let fileManager = NSFileManager.defaultManager() - XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) + let fileManager = FileManager.default + XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!)) } catch { XCTFail() @@ -154,20 +154,20 @@ class ZipTests: XCTestCase { func testQuickZipFolder() { do { - let fileManager = NSFileManager.defaultManager() - let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! - let folderURL = NSBundle(forClass: ZipTests.self).bundleURL.URLByAppendingPathComponent("Directory") - let targetImageURL1 = folderURL.URLByAppendingPathComponent("3crBXeO.gif") - let targetImageURL2 = folderURL.URLByAppendingPathComponent("kYkLkPf.gif") - if fileManager.fileExistsAtPath(folderURL.path!) { - try fileManager.removeItemAtURL(folderURL) + let fileManager = FileManager.default + let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! + let folderURL = try Bundle(for: ZipTests.self).bundleURL.appendingPathComponent("Directory") + let targetImageURL1 = try folderURL.appendingPathComponent("3crBXeO.gif") + let targetImageURL2 = try folderURL.appendingPathComponent("kYkLkPf.gif") + if fileManager.fileExists(atPath:folderURL.path!) { + try fileManager.removeItem(at: folderURL) } - try fileManager.createDirectoryAtURL(folderURL, withIntermediateDirectories: false, attributes: nil) - try fileManager.copyItemAtURL(imageURL1, toURL: targetImageURL1) - try fileManager.copyItemAtURL(imageURL2, toURL: targetImageURL2) + try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false, attributes: nil) + try fileManager.copyItem(at: imageURL1, to: targetImageURL1) + try fileManager.copyItem(at: imageURL2, to: targetImageURL2) let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory") - XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!)) } catch { XCTFail() @@ -177,15 +177,15 @@ class ZipTests: XCTestCase { func testZip() { do { - let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! - let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip") - try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: { (progress) -> () in + let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! + let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") + try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: nil, progress: { (progress) -> () in print(progress) }) - let fileManager = NSFileManager.defaultManager() - XCTAssertTrue(fileManager.fileExistsAtPath(zipFilePath.path!)) + let fileManager = FileManager.default + XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!)) } catch { XCTFail() @@ -194,23 +194,23 @@ class ZipTests: XCTestCase { func testZipUnzipPassword() { do { - let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")! - let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")! - let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip") - try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in + let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")! + let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")! + let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let zipFilePath = documentsFolder.appendingPathComponent("archive.zip") + try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: "password", progress: { (progress) -> () in print(progress) }) - let fileManager = NSFileManager.defaultManager() - XCTAssertTrue(fileManager.fileExistsAtPath(zipFilePath.path!)) - guard let fileExtension = zipFilePath.pathExtension, let fileName = zipFilePath.lastPathComponent else { - throw ZipError.UnzipFail + let fileManager = FileManager.default + XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!)) + guard let fileExtension = zipFilePath?.pathExtension, let fileName = zipFilePath?.lastPathComponent else { + throw ZipError.unzipFail } - let directoryName = fileName.stringByReplacingOccurrencesOfString(".\(fileExtension)", withString: "") - let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - let destinationUrl = documentsUrl.URLByAppendingPathComponent(directoryName, isDirectory: true) - try Zip.unzipFile(zipFilePath, destination: destinationUrl, overwrite: true, password: "password", progress: nil) - XCTAssertTrue(fileManager.fileExistsAtPath(destinationUrl.path!)) + let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") + let documentsUrl = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL + let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true) + try Zip.unzipFile(zipFilePath!, destination: destinationUrl!, overwrite: true, password: "password", progress: nil) + XCTAssertTrue(fileManager.fileExists(atPath:(destinationUrl?.path!)!)) } catch { XCTFail() @@ -220,16 +220,15 @@ class ZipTests: XCTestCase { func testQuickUnzipSubDir() { do { - let bookURL = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")! + let bookURL = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")! let unzipDestination = try Zip.quickUnzipFile(bookURL) - let fileManager = NSFileManager.defaultManager() - - let subDir = unzipDestination.URLByAppendingPathComponent("subDir") - let imageURL = subDir.URLByAppendingPathComponent("r2W9yu9").URLByAppendingPathExtension("gif") + let fileManager = FileManager.default + let subDir = try unzipDestination.appendingPathComponent("subDir") + let imageURL = try subDir.appendingPathComponent("r2W9yu9").appendingPathExtension("gif") - XCTAssertTrue(fileManager.fileExistsAtPath(unzipDestination.path!)) - XCTAssertTrue(fileManager.fileExistsAtPath(subDir.path!)) - XCTAssertTrue(fileManager.fileExistsAtPath(imageURL.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:unzipDestination.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:subDir.path!)) + XCTAssertTrue(fileManager.fileExists(atPath:imageURL.path!)) } catch { XCTFail() } -- 2.26.2