-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy patharchiver.integration.test.ts
More file actions
163 lines (135 loc) · 5.51 KB
/
archiver.integration.test.ts
File metadata and controls
163 lines (135 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {zip, brotliCompress} from './archiver.js'
import {fileExists, inTemporaryDirectory, mkdir, touchFile} from './fs.js'
import {joinPath, dirname} from './path.js'
import {exec} from './system.js'
import {describe, expect, test} from 'vitest'
import StreamZip from 'node-stream-zip'
import brotli from 'brotli'
import fs from 'fs'
describe('zip', () => {
test('zips a directory', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const zipPath = joinPath(tmpDir, 'output.zip')
const outputDirectoryName = 'output'
const outputDirectoryPath = joinPath(tmpDir, outputDirectoryName)
const structure = ['extensions/first/main.js', 'test.json']
await createFiles(structure, outputDirectoryPath)
// When
await zip({
inputDirectory: outputDirectoryPath,
outputZipPath: zipPath,
})
// Then
const archiveEntries = await readArchiveFiles(zipPath)
const expectedEntries = ['extensions/', 'extensions/first/', 'extensions/first/main.js', 'test.json']
expect(expectedEntries.sort()).toEqual(archiveEntries.sort())
})
})
test('only zips files that match input pattern', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const zipPath = joinPath(tmpDir, 'output.zip')
const outputDirectoryName = 'output'
const outputDirectoryPath = joinPath(tmpDir, outputDirectoryName)
const structure = ['extensions/first/main.js', 'test.json']
await createFiles(structure, outputDirectoryPath)
// When
await zip({
inputDirectory: outputDirectoryPath,
outputZipPath: zipPath,
matchFilePattern: '**/extensions/**',
})
// Then
const archiveEntries = await readArchiveFiles(zipPath)
expect(archiveEntries).toContain('extensions/')
expect(archiveEntries).toContain('extensions/first/')
const expectedEntries = ['extensions/', 'extensions/first/', 'extensions/first/main.js']
expect(expectedEntries.sort()).toEqual(archiveEntries.sort())
})
})
})
describe('brotliCompress', () => {
test('compresses a directory with brotli', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const brotliPath = joinPath(tmpDir, 'output.br')
const outputDirectoryName = 'output'
const outputDirectoryPath = joinPath(tmpDir, outputDirectoryName)
const testContent = 'test content'
// Create test file
await mkdir(outputDirectoryPath)
await touchFile(joinPath(outputDirectoryPath, 'test.txt'))
fs.writeFileSync(joinPath(outputDirectoryPath, 'test.txt'), testContent)
// When
await brotliCompress({
inputDirectory: outputDirectoryPath,
outputPath: brotliPath,
})
// Then
// Verify file exists and is compressed
const exists = await fileExists(brotliPath)
expect(exists).toBeTruthy()
const compressedContent = fs.readFileSync(brotliPath)
expect(compressedContent.length).toBeGreaterThan(0)
// Verify it's a valid brotli file by checking the header bytes
// Brotli files start with the bytes 0x1B...
expect(compressedContent[0]).toBe(0x1b)
// Decompress using brotli library
const decompressed = brotli.decompress(compressedContent)
expect(decompressed).toBeTruthy()
})
})
test('only compresses files that match input pattern', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const brotliPath = joinPath(tmpDir, 'output.br')
const outputDirectoryName = 'output'
const outputDirectoryPath = joinPath(tmpDir, outputDirectoryName)
const structure = ['extensions/first/main.js', 'test.json']
await createFiles(structure, outputDirectoryPath)
// When
await brotliCompress({
inputDirectory: outputDirectoryPath,
outputPath: brotliPath,
matchFilePattern: '**/extensions/**',
})
// Then
const exists = await fileExists(brotliPath)
expect(exists).toBeTruthy()
// Create a temporary directory to extract the tar archive
const extractPath = joinPath(tmpDir, 'extract')
await mkdir(extractPath)
// Save compressed content to a file
const compressedContent = fs.readFileSync(brotliPath)
const decompressed = brotli.decompress(compressedContent)
const tmpTarPath = joinPath(tmpDir, 'output.tar')
fs.writeFileSync(tmpTarPath, decompressed)
// Extract the tar
const tarArgs = ['-xf', tmpTarPath, '-C', extractPath]
await exec('tar', tarArgs)
// Verify only the extension file exists
const extractedFiles = fs.readdirSync(joinPath(extractPath, 'extensions/first'))
expect(extractedFiles).toContain('main.js')
// Verify the root file does not exist
expect(fs.existsSync(joinPath(extractPath, 'test.json'))).toBeFalsy()
})
})
})
async function createFiles(structure: string[], directory: string) {
for (const fileRelativePath of structure) {
const filePath = joinPath(directory, fileRelativePath)
// eslint-disable-next-line no-await-in-loop
await mkdir(dirname(filePath))
// eslint-disable-next-line no-await-in-loop
await touchFile(filePath)
}
}
async function readArchiveFiles(zipPath: string) {
await expect(fileExists(zipPath)).resolves.toBeTruthy()
// eslint-disable-next-line @babel/new-cap
const archive = new StreamZip.async({file: zipPath})
const archiveEntries = Object.keys(await archive.entries())
await archive.close()
return archiveEntries
}