ZipTests.swift 10.9 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 {
24
            let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")!
25
            let destinationURL = try Zip.quickUnzipFile(filePath)
26 27
            let fileManager = FileManager.default
            XCTAssertTrue(fileManager.fileExists(atPath: destinationURL.path!))
28 29 30
        }
        catch {
            XCTFail()
Roy Marmelstein's avatar
Roy Marmelstein committed
31
        }
Roy Marmelstein's avatar
Roy Marmelstein committed
32 33 34 35
    }
    
    func testQuickUnzipNonExistingPath() {
        do {
36
            let filePathURL = Bundle(for: ZipTests.self).resourcePath
Roy Marmelstein's avatar
Roy Marmelstein committed
37
            let filePath = NSURL(string:"\(filePathURL!)/bb9.zip")
38 39 40
            let destinationURL = try Zip.quickUnzipFile(filePath! as URL)
            let fileManager = FileManager.default
            XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!))
Roy Marmelstein's avatar
Roy Marmelstein committed
41 42 43 44 45
        }
        catch {
            XCTAssert(true)
        }
    }
Justin Anderson's avatar
Justin Anderson committed
46
    
Roy Marmelstein's avatar
Roy Marmelstein committed
47 48
    func testQuickUnzipNonZipPath() {
        do {
49
            let filePath = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")!
50
            let destinationURL = try Zip.quickUnzipFile(filePath)
51 52
            let fileManager = FileManager.default
            XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!))
Roy Marmelstein's avatar
Roy Marmelstein committed
53 54 55 56
        }
        catch {
            XCTAssert(true)
        }
Roy Marmelstein's avatar
Roy Marmelstein committed
57 58
    }
    
Roy Marmelstein's avatar
Roy Marmelstein committed
59 60
    func testQuickUnzipProgress() {
        do {
61
            let filePath = Bundle(for: 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 75 76
            let destinationURL = try Zip.quickUnzipFile(filePath as URL)
            let fileManager = FileManager.default
            XCTAssertFalse(fileManager.fileExists(atPath:destinationURL.path!))
77 78 79 80 81
        }
        catch {
            XCTAssert(true)
        }
    }
Roy Marmelstein's avatar
Roy Marmelstein committed
82 83 84
    
    func testUnzip() {
        do {
85 86
            let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")!
            let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL
Justin Anderson's avatar
Justin Anderson committed
87
            
88
            try Zip.unzipFile(filePath, destination: documentsFolder as URL, 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
            
92 93
            let fileManager = FileManager.default
            XCTAssertTrue(fileManager.fileExists(atPath:documentsFolder.path!))
Roy Marmelstein's avatar
Roy Marmelstein committed
94 95 96 97 98
        }
        catch {
            XCTFail()
        }
    }
99
    
Justin Anderson's avatar
Justin Anderson committed
100 101
    func testImplicitProgressUnzip() {
        do {
102
            let progress = Progress()
Justin Anderson's avatar
Justin Anderson committed
103 104
            progress.totalUnitCount = 1
            
105 106
            let filePath = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")!
            let documentsFolder = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)[0] as NSURL
Justin Anderson's avatar
Justin Anderson committed
107
            
108 109
            progress.becomeCurrent(withPendingUnitCount: 1)
            try Zip.unzipFile(filePath, destination: documentsFolder as URL, overwrite: true, password: "password", progress: nil)
Justin Anderson's avatar
Justin Anderson committed
110 111 112 113 114 115 116 117 118 119 120 121
            progress.resignCurrent()
            
            XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount)
        }
        catch {
            XCTFail()
        }
        
    }
    
    func testImplicitProgressZip() {
        do {
122
            let progress = Progress()
Justin Anderson's avatar
Justin Anderson committed
123 124
            progress.totalUnitCount = 1
            
125 126 127 128
            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")
Justin Anderson's avatar
Justin Anderson committed
129
            
130 131
            progress.becomeCurrent(withPendingUnitCount: 1)
            try Zip.zipFiles([imageURL1, imageURL2], zipFilePath: zipFilePath!, password: nil, progress: nil)
Justin Anderson's avatar
Justin Anderson committed
132 133 134 135 136 137 138 139 140 141
            progress.resignCurrent()
            
            XCTAssertTrue(progress.totalUnitCount == progress.completedUnitCount)
        }
        catch {
            XCTFail()
        }
    }
    
    
142 143
    func testQuickZip() {
        do {
144 145
            let imageURL1 = Bundle(for: ZipTests.self).urlForResource("3crBXeO", withExtension: "gif")!
            let imageURL2 = Bundle(for: ZipTests.self).urlForResource("kYkLkPf", withExtension: "gif")!
146
            let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive")
147 148
            let fileManager = FileManager.default
            XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!))
149 150 151 152 153
        }
        catch {
            XCTFail()
        }
    }
Roy Marmelstein's avatar
Roy Marmelstein committed
154
    
Roy Marmelstein's avatar
Roy Marmelstein committed
155 156
    func testQuickZipFolder() {
        do {
157 158 159 160 161 162 163 164
            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)
Roy Marmelstein's avatar
Roy Marmelstein committed
165
            }
166 167 168
            try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false, attributes: nil)
            try fileManager.copyItem(at: imageURL1, to: targetImageURL1)
            try fileManager.copyItem(at: imageURL2, to: targetImageURL2)
Roy Marmelstein's avatar
Roy Marmelstein committed
169
            let destinationURL = try Zip.quickZipFiles([folderURL], fileName: "directory")
170
            XCTAssertTrue(fileManager.fileExists(atPath:destinationURL.path!))
Roy Marmelstein's avatar
Roy Marmelstein committed
171 172 173 174 175
        }
        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
    func testZip() {
        do {
180 181 182 183 184
            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
Roy Marmelstein's avatar
Roy Marmelstein committed
185 186
                print(progress)
            })
187 188
            let fileManager = FileManager.default
            XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!))
Roy Marmelstein's avatar
Roy Marmelstein committed
189 190 191 192 193
        }
        catch {
            XCTFail()
        }
    }
194
    
195 196
    func testZipUnzipPassword() {
        do {
197 198 199 200 201
            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
202 203
                print(progress)
            })
204 205 206 207
            let fileManager = FileManager.default
            XCTAssertTrue(fileManager.fileExists(atPath:(zipFilePath?.path!)!))
            guard let fileExtension = zipFilePath?.pathExtension, let fileName = zipFilePath?.lastPathComponent else {
                throw ZipError.unzipFail
208
            }
209 210 211 212 213
            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!)!))
214 215 216 217 218 219 220
        }
        catch {
            XCTFail()
        }
    }

    
221 222
    func testQuickUnzipSubDir() {
        do {
223
            let bookURL = Bundle(for: ZipTests.self).urlForResource("bb8", withExtension: "zip")!
224
            let unzipDestination = try Zip.quickUnzipFile(bookURL)
225 226 227
            let fileManager = FileManager.default
            let subDir = try unzipDestination.appendingPathComponent("subDir")
            let imageURL = try subDir.appendingPathComponent("r2W9yu9").appendingPathExtension("gif")
228
            
229 230 231
            XCTAssertTrue(fileManager.fileExists(atPath:unzipDestination.path!))
            XCTAssertTrue(fileManager.fileExists(atPath:subDir.path!))
            XCTAssertTrue(fileManager.fileExists(atPath:imageURL.path!))
232 233 234 235
        } catch {
            XCTFail()
        }
    }
Roy Marmelstein's avatar
Roy Marmelstein committed
236

237
    func testFileExtensionIsNotInvalidForValidUrl() {
238
        let fileUrl = NSURL(string: "file.cbz")
József Vesza's avatar
József Vesza committed
239
        let result = Zip.fileExtensionIsInvalid(fileUrl?.pathExtension)
240
        XCTAssertFalse(result)
József Vesza's avatar
József Vesza committed
241 242 243
    }
    
    func testFileExtensionIsInvalidForInvalidUrl() {
244
        let fileUrl = NSURL(string: "file.xyz")
József Vesza's avatar
József Vesza committed
245
        let result = Zip.fileExtensionIsInvalid(fileUrl?.pathExtension)
246
        XCTAssertTrue(result)
József Vesza's avatar
József Vesza committed
247
    }
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
    
    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
277
}