|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basics |
| 14 | +import TSCBasic |
| 15 | +import TSCclibc // for SPM_posix_spawn_file_actions_addchdir_np_supported |
| 16 | +import TSCTestSupport |
| 17 | +import XCTest |
| 18 | + |
| 19 | +final class TarArchiverTests: XCTestCase { |
| 20 | + func testSuccess() throws { |
| 21 | + try testWithTemporaryDirectory { tmpdir in |
| 22 | + let archiver = TarArchiver(fileSystem: localFileSystem) |
| 23 | + let inputArchivePath = AbsolutePath(path: #file).parentDirectory |
| 24 | + .appending(components: "Inputs", "archive.tar.gz") |
| 25 | + try archiver.extract(from: inputArchivePath, to: tmpdir) |
| 26 | + let content = tmpdir.appending("file") |
| 27 | + XCTAssert(localFileSystem.exists(content)) |
| 28 | + XCTAssertEqual((try? localFileSystem.readFileContents(content))?.cString, "Hello World!") |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + func testArchiveDoesntExist() { |
| 33 | + let fileSystem = InMemoryFileSystem() |
| 34 | + let archiver = TarArchiver(fileSystem: fileSystem) |
| 35 | + let archive = AbsolutePath("/archive.tar.gz") |
| 36 | + XCTAssertThrowsError(try archiver.extract(from: archive, to: "/")) { error in |
| 37 | + XCTAssertEqual(error as? FileSystemError, FileSystemError(.noEntry, archive)) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + func testDestinationDoesntExist() throws { |
| 42 | + let fileSystem = InMemoryFileSystem(emptyFiles: "/archive.tar.gz") |
| 43 | + let archiver = TarArchiver(fileSystem: fileSystem) |
| 44 | + let destination = AbsolutePath("/destination") |
| 45 | + XCTAssertThrowsError(try archiver.extract(from: "/archive.tar.gz", to: destination)) { error in |
| 46 | + XCTAssertEqual(error as? FileSystemError, FileSystemError(.notDirectory, destination)) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + func testDestinationIsFile() throws { |
| 51 | + let fileSystem = InMemoryFileSystem(emptyFiles: "/archive.tar.gz", "/destination") |
| 52 | + let archiver = TarArchiver(fileSystem: fileSystem) |
| 53 | + let destination = AbsolutePath("/destination") |
| 54 | + XCTAssertThrowsError(try archiver.extract(from: "/archive.tar.gz", to: destination)) { error in |
| 55 | + XCTAssertEqual(error as? FileSystemError, FileSystemError(.notDirectory, destination)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + func testInvalidArchive() throws { |
| 60 | + try testWithTemporaryDirectory { tmpdir in |
| 61 | + let archiver = TarArchiver(fileSystem: localFileSystem) |
| 62 | + let inputArchivePath = AbsolutePath(path: #file).parentDirectory |
| 63 | + .appending(components: "Inputs", "invalid_archive.tar.gz") |
| 64 | + XCTAssertThrowsError(try archiver.extract(from: inputArchivePath, to: tmpdir)) { error in |
| 65 | + #if os(Linux) |
| 66 | + XCTAssertMatch((error as? StringError)?.description, .contains("not in gzip format")) |
| 67 | + #else |
| 68 | + XCTAssertMatch((error as? StringError)?.description, .contains("Unrecognized archive format")) |
| 69 | + #endif |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + func testValidation() throws { |
| 75 | + // valid |
| 76 | + try testWithTemporaryDirectory { _ in |
| 77 | + let archiver = TarArchiver(fileSystem: localFileSystem) |
| 78 | + let path = AbsolutePath(path: #file).parentDirectory |
| 79 | + .appending(components: "Inputs", "archive.tar.gz") |
| 80 | + XCTAssertTrue(try archiver.validate(path: path)) |
| 81 | + } |
| 82 | + // invalid |
| 83 | + try testWithTemporaryDirectory { _ in |
| 84 | + let archiver = TarArchiver(fileSystem: localFileSystem) |
| 85 | + let path = AbsolutePath(path: #file).parentDirectory |
| 86 | + .appending(components: "Inputs", "invalid_archive.tar.gz") |
| 87 | + XCTAssertFalse(try archiver.validate(path: path)) |
| 88 | + } |
| 89 | + // error |
| 90 | + try testWithTemporaryDirectory { _ in |
| 91 | + let archiver = TarArchiver(fileSystem: localFileSystem) |
| 92 | + let path = AbsolutePath.root.appending("does_not_exist.tar.gz") |
| 93 | + XCTAssertThrowsError(try archiver.validate(path: path)) { error in |
| 94 | + XCTAssertEqual(error as? FileSystemError, FileSystemError(.noEntry, path)) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + func testCompress() throws { |
| 100 | + #if os(Linux) |
| 101 | + guard SPM_posix_spawn_file_actions_addchdir_np_supported() else { |
| 102 | + throw XCTSkip("working directory not supported on this platform") |
| 103 | + } |
| 104 | + #endif |
| 105 | + |
| 106 | + try testWithTemporaryDirectory { tmpdir in |
| 107 | + let archiver = TarArchiver(fileSystem: localFileSystem) |
| 108 | + |
| 109 | + let rootDir = tmpdir.appending(component: UUID().uuidString) |
| 110 | + try localFileSystem.createDirectory(rootDir) |
| 111 | + try localFileSystem.writeFileContents(rootDir.appending("file1.txt"), string: "Hello World!") |
| 112 | + |
| 113 | + let dir1 = rootDir.appending("dir1") |
| 114 | + try localFileSystem.createDirectory(dir1) |
| 115 | + try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!") |
| 116 | + |
| 117 | + let dir2 = dir1.appending("dir2") |
| 118 | + try localFileSystem.createDirectory(dir2) |
| 119 | + try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!") |
| 120 | + try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!") |
| 121 | + |
| 122 | + let archivePath = tmpdir.appending(component: UUID().uuidString + ".tar.gz") |
| 123 | + try archiver.compress(directory: rootDir, to: archivePath) |
| 124 | + XCTAssertFileExists(archivePath) |
| 125 | + |
| 126 | + let extractRootDir = tmpdir.appending(component: UUID().uuidString) |
| 127 | + try localFileSystem.createDirectory(extractRootDir) |
| 128 | + try archiver.extract(from: archivePath, to: extractRootDir) |
| 129 | + try localFileSystem.stripFirstLevel(of: extractRootDir) |
| 130 | + |
| 131 | + XCTAssertFileExists(extractRootDir.appending("file1.txt")) |
| 132 | + XCTAssertEqual( |
| 133 | + try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")), |
| 134 | + "Hello World!" |
| 135 | + ) |
| 136 | + |
| 137 | + let extractedDir1 = extractRootDir.appending("dir1") |
| 138 | + XCTAssertDirectoryExists(extractedDir1) |
| 139 | + XCTAssertFileExists(extractedDir1.appending("file2.txt")) |
| 140 | + XCTAssertEqual( |
| 141 | + try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")), |
| 142 | + "Hello World 2!" |
| 143 | + ) |
| 144 | + |
| 145 | + let extractedDir2 = extractedDir1.appending("dir2") |
| 146 | + XCTAssertDirectoryExists(extractedDir2) |
| 147 | + XCTAssertFileExists(extractedDir2.appending("file3.txt")) |
| 148 | + XCTAssertEqual( |
| 149 | + try? localFileSystem.readFileContents(extractedDir2.appending("file3.txt")), |
| 150 | + "Hello World 3!" |
| 151 | + ) |
| 152 | + XCTAssertFileExists(extractedDir2.appending("file4.txt")) |
| 153 | + XCTAssertEqual( |
| 154 | + try? localFileSystem.readFileContents(extractedDir2.appending("file4.txt")), |
| 155 | + "Hello World 4!" |
| 156 | + ) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments