Skip to content

Commit 7c3b502

Browse files
authored
refactor(cli/unstable): use Date.now() internally and use FakeTime for testing (#6686)
1 parent 55d0efb commit 7c3b502

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

cli/unstable_progress_bar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,15 @@ export class ProgressBar {
255255
.pipeTo(writable, { preventClose: this.#keepOpen })
256256
.catch(() => clearInterval(this.#id));
257257
this.#writer = stream.writable.getWriter();
258-
this.#startTime = performance.now();
258+
this.#startTime = Date.now();
259259
this.#previousTime = 0;
260260
this.#previousValue = this.value;
261261

262262
this.#id = setInterval(() => this.#print(), 1000);
263263
this.#print();
264264
}
265265
#createFormatterObject() {
266-
const time = performance.now() - this.#startTime;
266+
const time = Date.now() - this.#startTime;
267267

268268
const size = this.value / this.max * this.#barLength | 0;
269269
const fillChars = this.#fillChar.repeat(size);

cli/unstable_progress_bar_test.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,57 @@
22

33
import { assertEquals } from "@std/assert";
44
import { ProgressBar } from "./unstable_progress_bar.ts";
5-
6-
async function* getData(
7-
loops: number,
8-
bufferSize: number,
9-
): AsyncGenerator<Uint8Array> {
10-
for (let i = 0; i < loops; ++i) {
11-
yield new Uint8Array(bufferSize);
12-
await new Promise((a) => setTimeout(a, Math.random() * 100));
13-
}
14-
}
5+
import { FakeTime } from "@std/testing/time";
156

167
const decoder = new TextDecoder();
178

189
Deno.test("ProgressBar() outputs default result", async () => {
10+
using fakeTime = new FakeTime();
11+
1912
const { readable, writable } = new TransformStream();
2013
const bar = new ProgressBar({ writable, max: 10 * 1000 });
14+
for (let index = 0; index < 10; index++) {
15+
bar.value += 1000;
16+
fakeTime.tick(1000);
17+
}
18+
bar.stop().then(() => writable.close());
19+
20+
const expected = [
21+
"\r\x1b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB]",
22+
"\r\x1b[K[00:01] [#####---------------------------------------------] [0.98/9.77 KiB]",
23+
"\r\x1b[K[00:02] [##########----------------------------------------] [1.95/9.77 KiB]",
24+
"\r\x1b[K[00:03] [###############-----------------------------------] [2.93/9.77 KiB]",
25+
"\r\x1b[K[00:04] [####################------------------------------] [3.91/9.77 KiB]",
26+
"\r\x1b[K[00:05] [#########################-------------------------] [4.88/9.77 KiB]",
27+
"\r\x1b[K[00:06] [##############################--------------------] [5.86/9.77 KiB]",
28+
"\r\x1b[K[00:07] [###################################---------------] [6.84/9.77 KiB]",
29+
"\r\x1b[K[00:08] [########################################----------] [7.81/9.77 KiB]",
30+
"\r\x1b[K[00:09] [#############################################-----] [8.79/9.77 KiB]",
31+
"\r\x1b[K[00:10] [##################################################] [9.77/9.77 KiB]",
32+
"\r\x1b[K[00:10] [##################################################] [9.77/9.77 KiB]",
33+
"\n",
34+
];
2135

22-
for await (const a of getData(10, 1000)) bar.value += a.length;
36+
const actual: string[] = [];
37+
for await (const buffer of readable) {
38+
actual.push(decoder.decode(buffer));
39+
}
40+
assertEquals(actual, expected);
41+
});
42+
43+
Deno.test("ProgressBar() prints every second", async () => {
44+
using fakeTime = new FakeTime();
45+
const { readable, writable } = new TransformStream();
46+
const bar = new ProgressBar({ writable, max: 10 * 1000 });
47+
fakeTime.tick(3000);
2348
bar.stop().then(() => writable.close());
2449

2550
const expected = [
2651
"\r\x1b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB]",
27-
"\r\x1b[K[00:00] [##################################################] [9.77/9.77 KiB]",
52+
"\r\x1b[K[00:01] [--------------------------------------------------] [0.00/9.77 KiB]",
53+
"\r\x1b[K[00:02] [--------------------------------------------------] [0.00/9.77 KiB]",
54+
"\r\x1b[K[00:03] [--------------------------------------------------] [0.00/9.77 KiB]",
55+
"\r\x1b[K[00:03] [--------------------------------------------------] [0.00/9.77 KiB]",
2856
"\n",
2957
];
3058

@@ -36,20 +64,17 @@ Deno.test("ProgressBar() outputs default result", async () => {
3664
});
3765

3866
Deno.test("ProgressBar() can handle a readable.cancel() correctly", async () => {
67+
using _fakeTime = new FakeTime();
3968
const { readable, writable } = new TransformStream();
4069
const bar = new ProgressBar({ writable, max: 10 * 1000 });
41-
42-
for await (const a of getData(10, 1000)) bar.value += a.length;
4370
bar.stop();
44-
4571
await readable.cancel();
4672
});
4773

4874
Deno.test("ProgressBar() can remove itself when finished", async () => {
75+
using _fakeTime = new FakeTime();
4976
const { readable, writable } = new TransformStream();
5077
const bar = new ProgressBar({ writable, max: 10 * 1000, clear: true });
51-
52-
for await (const a of getData(10, 1000)) bar.value += a.length;
5378
bar.stop().then(() => writable.close());
5479

5580
const expected = [
@@ -65,14 +90,17 @@ Deno.test("ProgressBar() can remove itself when finished", async () => {
6590
});
6691

6792
Deno.test("ProgressBar() passes correct values to formatter", async () => {
93+
using _fakeTime = new FakeTime();
6894
const { readable, writable } = new TransformStream();
6995
let lastTime: undefined | number = undefined;
7096
let lastValue: undefined | number = undefined;
97+
let called = false;
7198
const bar = new ProgressBar({
7299
writable,
73100
max: 10 * 1000,
74101
keepOpen: false,
75102
fmt(x) {
103+
called = true;
76104
if (lastTime != undefined) assertEquals(x.previousTime, lastTime);
77105
if (lastValue != undefined) assertEquals(x.previousValue, lastValue);
78106
lastTime = x.time;
@@ -81,9 +109,9 @@ Deno.test("ProgressBar() passes correct values to formatter", async () => {
81109
},
82110
});
83111

84-
for await (const a of getData(10, 1000)) bar.value += a.length;
112+
bar.value += 1000;
85113
bar.stop();
86-
114+
assertEquals(called, true);
87115
await new Response(readable).bytes();
88116
});
89117

0 commit comments

Comments
 (0)