Skip to content

Commit 4802b56

Browse files
committed
test: add tests for new zipStream fs
1 parent 72846c0 commit 4802b56

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

packages/core/src/shared/utilities/zipStream.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,16 @@ export class ZipStream {
110110
return this._zipWriter.add(path, new TextReader(data))
111111
}
112112

113-
// TODO: add tests for this.
114113
public writeData(data: Uint8Array, path: string) {
115114
return this._zipWriter.add(path, new Uint8ArrayReader(data))
116115
}
117116

118117
/**
119118
* Add the content for file to zip at path.
120-
* @param file file to read
121-
* @param path path to write data to in zip.
119+
* @param sourceFilePath file to read
120+
* @param targetFilePath path to write data to in zip.
122121
*/
123-
public writeFile(file: string, path: string) {
122+
public writeFile(sourceFilePath: string, targetFilePath: string) {
124123
// We use _numberOfFilesToStream to make sure we don't finalize too soon
125124
// (before the progress event has been fired for the last file)
126125
// The problem is that we can't rely on progress.entries.total,
@@ -131,15 +130,15 @@ export class ZipStream {
131130
// We only start zipping another file if we're under our limit
132131
// of concurrent file streams
133132
if (this._filesBeingZipped < this._maxNumberOfFileStreams) {
134-
void readFileAsString(file).then((content) => {
135-
return this._zipWriter.add(path, new TextReader(content), {
133+
void readFileAsString(sourceFilePath).then((content) => {
134+
return this._zipWriter.add(targetFilePath, new TextReader(content), {
136135
onend: this.boundFileCompletionCallback,
137136
onstart: this.boundFileStartCallback,
138137
})
139138
})
140139
} else {
141140
// Queue it for later (see "write" event)
142-
this._filesToZip.push([file, path])
141+
this._filesToZip.push([sourceFilePath, targetFilePath])
143142
}
144143
}
145144

@@ -166,7 +165,6 @@ export class ZipStream {
166165
}
167166
}
168167

169-
// TODO: add tests for this.
170168
public async finalizeToFile(targetPath: string) {
171169
const result = await this.finalize()
172170
const contents = result.streamBuffer.getContents() || Buffer.from('')

packages/core/src/test/shared/utilities/zipStream.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('zipStream', function () {
2121
await fs.delete(tmpDir, { recursive: true })
2222
})
2323

24-
it('Should create a zip stream from text content', async function () {
24+
it('should create a zip stream from text content', async function () {
2525
const zipStream = new ZipStream({ hashAlgorithm: 'md5' })
2626
await zipStream.writeString('foo bar', 'file.txt')
2727
const result = await zipStream.finalize()
@@ -39,7 +39,29 @@ describe('zipStream', function () {
3939
assert.strictEqual(result.sizeInBytes, (await fs.stat(zipPath)).size)
4040
})
4141

42-
it('Should create a zip stream from file', async function () {
42+
it('should create a zip stream from binary content', async function () {
43+
const zipStream = new ZipStream({ hashAlgorithm: 'md5' })
44+
await zipStream.writeData(Buffer.from('foo bar'), 'file.txt')
45+
const result = await zipStream.finalize()
46+
47+
const zipBuffer = result.streamBuffer.getContents()
48+
assert.ok(zipBuffer)
49+
50+
const zipPath = path.join(tmpDir, 'test.zip')
51+
await fs.writeFile(zipPath, zipBuffer)
52+
const expectedMd5 = crypto
53+
.createHash('md5')
54+
.update(await fs.readFileBytes(zipPath))
55+
.digest('base64')
56+
assert.strictEqual(result.hash, expectedMd5)
57+
assert.strictEqual(result.sizeInBytes, (await fs.stat(zipPath)).size)
58+
59+
const zipContents = await ZipStream.unzip(zipBuffer)
60+
assert.strictEqual(zipContents.length, 1)
61+
assert.strictEqual(zipContents[0].filename, 'file.txt')
62+
})
63+
64+
it('should create a zip stream from file', async function () {
4365
const testFilePath = path.join(tmpDir, 'test.txt')
4466
await fs.writeFile(testFilePath, 'foo bar')
4567

@@ -71,4 +93,14 @@ describe('zipStream', function () {
7193
const zipEntries = await ZipStream.unzip(zipBuffer)
7294
assert.strictEqual(zipEntries[0].filename, 'file.txt')
7395
})
96+
97+
it('should write contents to file', async function () {
98+
const zipStream = new ZipStream()
99+
await zipStream.writeString('foo bar', 'file.txt')
100+
await zipStream.writeData(Buffer.from('foo bar'), 'file2.txt')
101+
const zipPath = path.join(tmpDir, 'test.zip')
102+
const result = await zipStream.finalizeToFile(path.join(tmpDir, 'test.zip'))
103+
104+
assert.strictEqual(result.sizeInBytes, (await fs.stat(zipPath)).size)
105+
})
74106
})

0 commit comments

Comments
 (0)