|
| 1 | +import { exec as _exec } from 'child_process'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { promisify } from 'util'; |
| 6 | +import { zipDirectory } from '../../src/util/archive'; |
| 7 | + |
| 8 | +const exec = promisify(_exec); |
| 9 | + |
| 10 | +describe('zipDirectory', () => { |
| 11 | + let tempDir: string; |
| 12 | + let outputZipPath: string; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + // Create a temporary directory |
| 16 | + tempDir = await fs.promises.mkdtemp( |
| 17 | + path.join(os.tmpdir(), 'zipDirectory-test-'), |
| 18 | + ); |
| 19 | + outputZipPath = path.join(os.tmpdir(), 'output.zip'); |
| 20 | + |
| 21 | + // Clean up any existing test files |
| 22 | + try { |
| 23 | + await fs.promises.unlink(outputZipPath); |
| 24 | + } catch (e) { |
| 25 | + // Ignore errors if file doesn't exist |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(async () => { |
| 30 | + // Clean up |
| 31 | + try { |
| 32 | + await fs.promises.rm(tempDir, { recursive: true }); |
| 33 | + await fs.promises.unlink(outputZipPath); |
| 34 | + } catch (e) { |
| 35 | + // Ignore errors during cleanup |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + test('creates a zip file with expected files', async () => { |
| 40 | + // Setup |
| 41 | + const testFileContent = 'test content'; |
| 42 | + const testFilePath = path.join(tempDir, 'testfile.txt'); |
| 43 | + await fs.promises.writeFile(testFilePath, testFileContent); |
| 44 | + |
| 45 | + // Create a nested directory |
| 46 | + const nestedDir = path.join(tempDir, 'nested'); |
| 47 | + await fs.promises.mkdir(nestedDir, { recursive: true }); |
| 48 | + const nestedFilePath = path.join(nestedDir, 'nestedfile.txt'); |
| 49 | + await fs.promises.writeFile(nestedFilePath, 'nested content'); |
| 50 | + |
| 51 | + // Act |
| 52 | + await zipDirectory(tempDir, outputZipPath); |
| 53 | + |
| 54 | + // Assert |
| 55 | + const stats = await fs.promises.stat(outputZipPath); |
| 56 | + expect(stats.isFile()).toBe(true); |
| 57 | + |
| 58 | + // Verify content using unzip |
| 59 | + const { stdout: fileList } = await exec(`unzip -l ${outputZipPath}`); |
| 60 | + |
| 61 | + // Check file list contains the expected files |
| 62 | + expect(fileList).toContain('testfile.txt'); |
| 63 | + expect(fileList).toContain('nested/nestedfile.txt'); |
| 64 | + |
| 65 | + // Create a temporary directory to extract files |
| 66 | + const extractDir = await fs.promises.mkdtemp( |
| 67 | + path.join(os.tmpdir(), 'extract-test-'), |
| 68 | + ); |
| 69 | + |
| 70 | + try { |
| 71 | + // Extract files |
| 72 | + await exec(`unzip ${outputZipPath} -d ${extractDir}`); |
| 73 | + |
| 74 | + // Verify content of files |
| 75 | + const mainFileContent = await fs.promises.readFile( |
| 76 | + path.join(extractDir, 'testfile.txt'), |
| 77 | + { encoding: 'utf-8' }, |
| 78 | + ); |
| 79 | + expect(mainFileContent).toBe(testFileContent); |
| 80 | + |
| 81 | + const nestedFileContent = await fs.promises.readFile( |
| 82 | + path.join(extractDir, 'nested/nestedfile.txt'), |
| 83 | + { encoding: 'utf-8' }, |
| 84 | + ); |
| 85 | + expect(nestedFileContent).toBe('nested content'); |
| 86 | + } finally { |
| 87 | + // Clean up the extract directory |
| 88 | + await fs.promises.rm(extractDir, { recursive: true }); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + test('preserves file permissions', async () => { |
| 93 | + // Setup - create a file with specific permissions |
| 94 | + const testFilePath = path.join(tempDir, 'executable.sh'); |
| 95 | + await fs.promises.writeFile(testFilePath, '#!/bin/sh\necho "Hello"'); |
| 96 | + |
| 97 | + // Set executable permissions (0755) |
| 98 | + await fs.promises.chmod(testFilePath, 0o755); |
| 99 | + |
| 100 | + // Act |
| 101 | + await zipDirectory(tempDir, outputZipPath); |
| 102 | + |
| 103 | + // Assert - extract the file and check permissions |
| 104 | + // Create a temporary directory to extract files |
| 105 | + const extractDir = await fs.promises.mkdtemp( |
| 106 | + path.join(os.tmpdir(), 'extract-permissions-test-'), |
| 107 | + ); |
| 108 | + |
| 109 | + try { |
| 110 | + // Extract files |
| 111 | + await exec(`unzip ${outputZipPath} -d ${extractDir}`); |
| 112 | + |
| 113 | + // Check the extracted file permissions |
| 114 | + const stats = await fs.promises.stat(path.join(extractDir, 'executable.sh')); |
| 115 | + |
| 116 | + // Check executable bit is set (0o111 = ---x--x--x) |
| 117 | + // eslint-disable-next-line no-bitwise |
| 118 | + const isExecutable = !!(stats.mode & 0o111); // Check if any executable bit is set |
| 119 | + expect(isExecutable).toBeTruthy(); |
| 120 | + } finally { |
| 121 | + // Clean up the extract directory |
| 122 | + await fs.promises.rm(extractDir, { recursive: true }); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + test('follows symlinks as expected', async () => { |
| 127 | + // Skip test on Windows as symlinks might not be properly supported |
| 128 | + if (os.platform() === 'win32') { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + // Setup - create a file and a symlink to it |
| 133 | + const targetFile = path.join(tempDir, 'target.txt'); |
| 134 | + await fs.promises.writeFile(targetFile, 'target content'); |
| 135 | + |
| 136 | + const symlinkPath = path.join(tempDir, 'link.txt'); |
| 137 | + await fs.promises.symlink(targetFile, symlinkPath); |
| 138 | + |
| 139 | + // Act |
| 140 | + await zipDirectory(tempDir, outputZipPath); |
| 141 | + |
| 142 | + // Assert - extract the file and check content |
| 143 | + // Create a temporary directory to extract files |
| 144 | + const extractDir = await fs.promises.mkdtemp( |
| 145 | + path.join(os.tmpdir(), 'extract-symlink-test-'), |
| 146 | + ); |
| 147 | + |
| 148 | + try { |
| 149 | + // Extract files |
| 150 | + await exec(`unzip ${outputZipPath} -d ${extractDir}`); |
| 151 | + |
| 152 | + // Check that the link.txt file exists in the zip |
| 153 | + const linkExists = await fs.promises.stat(path.join(extractDir, 'link.txt')) |
| 154 | + .then(() => true) |
| 155 | + .catch(() => false); |
| 156 | + expect(linkExists).toBeTruthy(); |
| 157 | + |
| 158 | + // Check that the content is the same as the target file |
| 159 | + const linkedContent = await fs.promises.readFile( |
| 160 | + path.join(extractDir, 'link.txt'), |
| 161 | + { encoding: 'utf-8' }, |
| 162 | + ); |
| 163 | + expect(linkedContent).toBe('target content'); |
| 164 | + } finally { |
| 165 | + // Clean up the extract directory |
| 166 | + await fs.promises.rm(extractDir, { recursive: true }); |
| 167 | + } |
| 168 | + }); |
| 169 | +}); |
0 commit comments