Skip to content

Commit 2267e9d

Browse files
committed
Add tests for unzip progress reporting
1 parent 6f85894 commit 2267e9d

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

extensions/ql-vscode/src/common/unzip.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ export async function unzipToDirectory(
205205
});
206206
};
207207

208+
reportProgress();
209+
208210
await taskRunner(
209211
entries.map((entry) => async () => {
210212
let entryBytesExtracted = 0;

extensions/ql-vscode/test/unit-tests/common/unzip.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,75 @@ describe.each([
164164
expect(await pathExists(join(tmpDir.path, "empty-directory"))).toBe(true);
165165
expect(await readdir(join(tmpDir.path, "empty-directory"))).toEqual([]);
166166
});
167+
168+
describe("with reported progress", () => {
169+
const progressCallback = jest.fn();
170+
171+
beforeEach(async () => {
172+
progressCallback.mockReset();
173+
174+
await unzipToDirectory(zipPath, tmpDir.path, progressCallback);
175+
});
176+
177+
it("has at least as many progress callbacks as files", () => {
178+
expect(progressCallback.mock.calls.length).toBeGreaterThanOrEqual(11);
179+
});
180+
181+
it("has an incrementing files extracted value", () => {
182+
let previousValue = 0;
183+
for (const call of progressCallback.mock.calls.values()) {
184+
const [{ filesExtracted }] = call;
185+
expect(filesExtracted).toBeGreaterThanOrEqual(previousValue);
186+
previousValue = filesExtracted;
187+
}
188+
});
189+
190+
it("has an incrementing bytes extracted value", () => {
191+
let previousValue = 0;
192+
for (const call of progressCallback.mock.calls.values()) {
193+
const [{ bytesExtracted }] = call;
194+
expect(bytesExtracted).toBeGreaterThanOrEqual(previousValue);
195+
previousValue = bytesExtracted;
196+
}
197+
});
198+
199+
it("always increments either bytes or files extracted", () => {
200+
let previousBytesExtracted = 0;
201+
let previousFilesExtracted = 0;
202+
203+
for (const [index, call] of progressCallback.mock.calls.entries()) {
204+
if (index === 0) {
205+
// The first call is always 0, 0
206+
continue;
207+
}
208+
209+
const [{ bytesExtracted, filesExtracted }] = call;
210+
expect(bytesExtracted + filesExtracted).toBeGreaterThan(
211+
previousBytesExtracted + previousFilesExtracted,
212+
);
213+
previousBytesExtracted = bytesExtracted;
214+
previousFilesExtracted = filesExtracted;
215+
}
216+
});
217+
218+
it("has a first call with the correct values", () => {
219+
expect(progressCallback).toHaveBeenNthCalledWith(1, {
220+
bytesExtracted: 0,
221+
totalBytes: 87,
222+
filesExtracted: 0,
223+
totalFiles: 11,
224+
});
225+
});
226+
227+
it("has a last call with the correct values", () => {
228+
expect(progressCallback).toHaveBeenLastCalledWith({
229+
bytesExtracted: 87,
230+
totalBytes: 87,
231+
filesExtracted: 11,
232+
totalFiles: 11,
233+
});
234+
});
235+
});
167236
});
168237

169238
async function expectFile(

0 commit comments

Comments
 (0)