Skip to content

Commit 546c5ba

Browse files
committed
wip
1 parent f472192 commit 546c5ba

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

packages/shared/src/helpers/progress-report.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,17 @@
1717
import { info } from './logging.js';
1818

1919
/**
20-
* Logs the current file being analyzed every N seconds
21-
*
22-
* @param interval the interval between two logs, in milliseconds
20+
* Logs the current file being analyzed every 10 seconds
2321
*/
2422

2523
export class ProgressReport {
26-
private currentFileNumber = 0;
24+
private counter = 0;
2725
private currentFilename = '';
26+
private readonly interval = 10_000;
2827
private intervalId: NodeJS.Timeout | undefined = undefined;
2928

30-
constructor(
31-
private readonly count: number,
32-
private readonly interval: number = 10_000,
33-
) {
34-
info(`${this.count} source ${this.pluralizeFile(this.count)} to be analyzed.`);
29+
constructor(private readonly total: number) {
30+
info(`${this.total} source ${this.pluralizeFile(this.total)} to be analyzed`);
3531
}
3632

3733
pluralizeFile(count: number) {
@@ -41,13 +37,13 @@ export class ProgressReport {
4137
start() {
4238
this.intervalId = setInterval(() => {
4339
info(
44-
`${this.currentFileNumber}/${this.count} ${this.pluralizeFile(this.currentFileNumber)} analyzed, current file: ${this.currentFilename}`,
40+
`${this.counter}/${this.total} ${this.pluralizeFile(this.counter)} analyzed, current file: ${this.currentFilename}`,
4541
);
4642
}, this.interval);
4743
}
4844

4945
nextFile(currentFilename: string) {
50-
this.currentFileNumber++;
46+
this.counter++;
5147
this.currentFilename = currentFilename;
5248
if (!this.intervalId) {
5349
this.start();
@@ -57,11 +53,7 @@ export class ProgressReport {
5753
stop() {
5854
clearInterval(this.intervalId);
5955
info(
60-
`${this.count}/${this.count} source ${this.pluralizeFile(this.count)} ${this.count === 1 ? 'has' : 'have'} been analyzed.`,
56+
`${this.total}/${this.total} source ${this.pluralizeFile(this.total)} ${this.total === 1 ? 'has' : 'have'} been analyzed`,
6157
);
6258
}
63-
64-
cancel() {
65-
clearInterval(this.intervalId);
66-
}
6759
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SonarQube JavaScript Plugin
3+
* Copyright (C) 2011-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
import { describe, it, type Mock } from 'node:test';
18+
import { ProgressReport } from '../../src/helpers/progress-report.js';
19+
import { expect } from 'expect';
20+
21+
describe('progress-report', () => {
22+
it('should report progress', async ({ mock }) => {
23+
mock.timers.enable({ apis: ['setInterval'] });
24+
mock.method(console, 'log');
25+
const progressReport = new ProgressReport(5);
26+
const consoleLogMock = (console.log as Mock<typeof console.log>).mock;
27+
expect(consoleLogMock.calls[0].arguments[0]).toEqual(`5 source files to be analyzed`);
28+
expect(consoleLogMock.callCount()).toEqual(1);
29+
// we pass next file
30+
progressReport.nextFile('file1.ts');
31+
// passing next file should not trigger a console.log
32+
expect(consoleLogMock.callCount()).toEqual(1);
33+
34+
// after 10 seconds have passed, there is a new log
35+
mock.timers.tick(10000);
36+
expect(consoleLogMock.calls[1].arguments[0]).toEqual(
37+
`1/5 file analyzed, current file: file1.ts`,
38+
);
39+
expect(consoleLogMock.callCount()).toEqual(2);
40+
41+
// another 10 seconds, we should log next file
42+
mock.timers.tick(10000);
43+
expect(consoleLogMock.calls[2].arguments[0]).toEqual(
44+
`1/5 file analyzed, current file: file1.ts`,
45+
);
46+
expect(consoleLogMock.callCount()).toEqual(3);
47+
48+
progressReport.stop();
49+
// another 10 seconds, nothing else has been logged because we have stopped the reporter
50+
mock.timers.tick(10000);
51+
expect(consoleLogMock.calls[3].arguments[0]).toEqual(`5/5 source files have been analyzed`);
52+
expect(consoleLogMock.callCount()).toEqual(4);
53+
});
54+
});

0 commit comments

Comments
 (0)