ZipTests.swift 11.2 KB
Newer Older
Roy Marmelstein's avatar
Roy Marmelstein committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
//  ZipTests.swift
//  ZipTests
//
//  Created by Roy Marmelstein on 13/12/2015.
//  Copyright © 2015 Roy Marmelstein. All rights reserved.
//

import XCTest
@testable import Zip

class ZipTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
22 23
    func testQuickUnzip() {
        do {
Roy Marmelstein's avatar
Roy Marmelstein committed
24
            let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
25
            let destinationURL = try Zip.quickUnzipFile(filePath)
26 27 28 29 30
            let fileManager = NSFileManager.defaultManager()
            XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
        }
        catch {
            XCTFail()
Roy Marmelstein's avatar
Roy Marmelstein committed
31
        }
Roy Marmelstein's avatar
Roy Marmelstein committed
32 33 34 35 36
    }
    
    func testQuickUnzipNonExistingPath() {
        do {
            let filePathURL = NSBundle(forClass: ZipTests.self).resourcePath
Roy Marmelstein's avatar
Roy Marmelstein committed
37
            let filePath = NSURL(string:"\(filePathURL!)/bb9.zip")
38
            let destinationURL = try Zip.quickUnzipFile(filePath!)
Roy Marmelstein's avatar
Roy Marmelstein committed
39 40 41 42 43 44 45
            let fileManager = NSFileManager.defaultManager()
            XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
        }
        catch {
            XCTAssert(true)
        }
    }
Justin Anderson's avatar
Justin Anderson committed
46
    
Roy Marmelstein's avatar
Roy Marmelstein committed
47 48
    func testQuickUnzipNonZipPath() {
        do {
Roy Marmelstein's avatar
Roy Marmelstein committed
49
            let filePath = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
50
            let destinationURL = try Zip.quickUnzipFile(filePath)
Roy Marmelstein's avatar
Roy Marmelstein committed
51 52 53 54 55 56
            let fileManager = NSFileManager.defaultManager()
            XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
        }
        catch {
            XCTAssert(true)
        }
Roy Marmelstein's avatar
Roy Marmelstein committed
57 58
    }
    
Roy Marmelstein's avatar
Roy Marmelstein committed
59 60
    func testQuickUnzipProgress() {
        do {
Roy Marmelstein's avatar
Roy Marmelstein committed
61
            let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
62
            try Zip.quickUnzipFile(filePath, progress: { (progress) -> () in
Roy Marmelstein's avatar
Roy Marmelstein committed
63 64 65 66 67 68 69
                XCTAssert(true)
            })
        }
        catch {
            XCTFail()
        }
    }
70
    
Roy Marmelstein's avatar
Roy Marmelstein committed
71
    func testQuickUnzipOnlineURL() {
72
        do {
Roy Marmelstein's avatar
Roy Marmelstein committed
73
            let filePath = NSURL(string: "http://www.google.com/google.zip")!
74
            let destinationURL = try Zip.quickUnzipFile(filePath)
75 76 77 78 79 80 81
            let fileManager = NSFileManager.defaultManager()
            XCTAssertFalse(fileManager.fileExistsAtPath(destinationURL.path!))
        }
        catch {
            XCTAssert(true)
        }
    }
Roy Marmelstein's avatar
Roy Marmelstein committed
82 83 84 85 86
    
    func testUnzip() {
        do {
            let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
            let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
Justin Anderson's avatar
Justin Anderson committed
87
            
88
            try Zip.unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: { (progress) -> () in
Roy Marmelstein's avatar
Roy Marmelstein committed
89 90
                print(progress)
            })
Justin Anderson's avatar
Justin Anderson committed
91
            
Roy Marmelstein's avatar
Roy Marmelstein committed
92 93 94 95 96 97 98
            let fileManager = NSFileManager.defaultManager()
            XCTAssertTrue(fileManager.fileExistsAtPath(documentsFolder.path!))
        }
        catch {
            XCTFail()
        }
    }
99
    
Justin Anderson's avatar
Justin Anderson committed
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
    func testImplicitProgressUnzip() {
        do {
            let progress = NSProgress()
            progress.totalUnitCount = 1
            
            let filePath = NSBundle(forClass: ZipTests.self).URLForResource("bb8", withExtension: "zip")!
            let documentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
            
            progress.becomeCurrentWithPendingUnitCount(1)
            try Zip.unzipFile(filePath, destination: documentsFolder, overwrite: true, password: "password", progress: nil)
            progress.resignCurrent()
            
            XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount)
        }
        catch {
            XCTFail()
        }
        
    }
    
    func testImplicitProgressZip() {
        do {
            let progress = NSProgress()
            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")
            
            progress.becomeCurrentWithPendingUnitCount(1)
            try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: nil)
            progress.resignCurrent()
            
            XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount)
        }
        catch {
            XCTFail()
        }
    }
    
    
142 143 144 145
    func testQuickZip() {
        do {
            let imageURL1 = NSBundle(forClass: ZipTests.self).URLForResource("3crBXeO", withExtension: "gif")!
            let imageURL2 = NSBundle(forClass: ZipTests.self).URLForResource("kYkLkPf", withExtension: "gif")!
146
            let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive")
147 148 149 150 151 152 153
            let fileManager = NSFileManager.defaultManager()
            XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
        }
        catch {
            XCTFail()
        }
    }
Roy Marmelstein's avatar
Roy Marmelstein committed
154
    
Roy Marmelstein's avatar
Roy Marmelstein committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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)
            }
            try fileManager.createDirectoryAtURL(folderURL, withIntermediateDirectories: false, attributes: nil)
            try fileManager.copyItemAtURL(imageURL1, toURL: targetImageURL1)
            try fileManager.copyItemAtURL(imageURL2, toURL: targetImageURL2)
            let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory")
            XCTAssertTrue(fileManager.fileExistsAtPath(destinationURL.path!))
        }
        catch {
            XCTFail()
        }
    }
Justin Anderson's avatar
Justin Anderson committed
176
    
Roy Marmelstein's avatar
Roy Marmelstein committed
177
    
Roy Marmelstein's avatar
Roy Marmelstein committed
178 179 180 181 182 183
    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")
184
            try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath, password: nil, progress: { (progress) -> () in
Roy Marmelstein's avatar
Roy Marmelstein committed
185 186 187 188 189 190 191 192 193
                print(progress)
            })
            let fileManager = NSFileManager.defaultManager()
            XCTAssertTrue(fileManager.fileExistsAtPath(zipFilePath.path!))
        }
        catch {
            XCTFail()
        }
    }
194
    
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    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
                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 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!))
        }
        catch {
            XCTFail()
        }
    }

    
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    func testQuickUnzipSubDir() {
        do {
            let bookURL = NSBundle(forClass: 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")
            
            XCTAssertTrue(fileManager.fileExistsAtPath(unzipDestination.path!))
            XCTAssertTrue(fileManager.fileExistsAtPath(subDir.path!))
            XCTAssertTrue(fileManager.fileExistsAtPath(imageURL.path!))
        } catch {
            XCTFail()
        }
    }
Roy Marmelstein's avatar
Roy Marmelstein committed
237

238
    func testFileExtensionIsNotInvalidForValidUrl() {
239
        let fileUrl = NSURL(string: "file.cbz")
József Vesza's avatar
József Vesza committed
240
        let result = Zip.fileExtensionIsInvalid(fileUrl?.pathExtension)
241
        XCTAssertFalse(result)
József Vesza's avatar
József Vesza committed
242 243 244
    }
    
    func testFileExtensionIsInvalidForInvalidUrl() {
245
        let fileUrl = NSURL(string: "file.xyz")
József Vesza's avatar
József Vesza committed
246
        let result = Zip.fileExtensionIsInvalid(fileUrl?.pathExtension)
247
        XCTAssertTrue(result)
József Vesza's avatar
József Vesza committed
248
    }
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    
    func testAddedCustomFileExtensionIsValid() {
        let fileExtension = "cstm"
        Zip.addCustomFileExtension(fileExtension)
        let result = Zip.isValidFileExtension(fileExtension)
        XCTAssertTrue(result)
        Zip.removeCustomFileExtension(fileExtension)
    }
    
    func testRemovedCustomFileExtensionIsInvalid() {
        let fileExtension = "cstm"
        Zip.addCustomFileExtension(fileExtension)
        Zip.removeCustomFileExtension(fileExtension)
        let result = Zip.isValidFileExtension(fileExtension)
        XCTAssertFalse(result)
    }
    
    func testDefaultFileExtensionsIsValid() {
        XCTAssertTrue(Zip.isValidFileExtension("zip"))
        XCTAssertTrue(Zip.isValidFileExtension("cbz"))
    }
    
    func testDefaultFileExtensionsIsNotRemoved() {
        Zip.removeCustomFileExtension("zip")
        Zip.removeCustomFileExtension("cbz")
        XCTAssertTrue(Zip.isValidFileExtension("zip"))
        XCTAssertTrue(Zip.isValidFileExtension("cbz"))
    }
    
Roy Marmelstein's avatar
Roy Marmelstein committed
278
}