Skip to content

Commit 899ab76

Browse files
committed
feat(utils): throw if invalid group/spinner combinations used in logger
1 parent 50cd17d commit 899ab76

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

packages/utils/src/lib/logger.int.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,4 +781,59 @@ ${ansis.red.bold('Cancelled by SIGINT')}
781781
);
782782
});
783783
});
784+
785+
describe('invalid usage', () => {
786+
it('should throw if nesting group in another group', async () => {
787+
const logger = new Logger();
788+
789+
await expect(
790+
logger.group('Outer group', async () => {
791+
await logger.group('Inner group', async () => 'Inner group complete');
792+
return 'Outer group complete';
793+
}),
794+
).rejects.toThrow(
795+
'Internal Logger error - nested groups are not supported',
796+
);
797+
});
798+
799+
it('should throw if nesting groups across logger instances', async () => {
800+
await expect(
801+
new Logger().group('Outer group', async () => {
802+
await new Logger().group(
803+
'Inner group',
804+
async () => 'Inner group complete',
805+
);
806+
return 'Outer group complete';
807+
}),
808+
).rejects.toThrow(
809+
'Internal Logger error - nested groups are not supported',
810+
);
811+
});
812+
813+
it('should throw if creating group while spinner is running', async () => {
814+
const logger = new Logger();
815+
816+
await expect(
817+
logger.task('Some async process', async () => {
818+
await logger.group('Some group', async () => 'Group completed');
819+
return 'Async process completed';
820+
}),
821+
).rejects.toThrow(
822+
'Internal Logger error - creating group in active spinner is not supported',
823+
);
824+
});
825+
826+
it('should throw if starting new spinner while another is still active', async () => {
827+
const logger = new Logger();
828+
829+
await expect(
830+
Promise.all([
831+
logger.task('Task 1', async () => 'DONE'),
832+
logger.task('Task 2', async () => 'DONE'),
833+
]),
834+
).rejects.toThrow(
835+
'Internal Logger error - concurrent spinners are not supported',
836+
);
837+
});
838+
});
784839
});

packages/utils/src/lib/logger.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ export class Logger {
8888
}
8989

9090
async group(title: string, worker: () => Promise<string>): Promise<void> {
91+
if (this.#groupColor) {
92+
throw new Error(
93+
'Internal Logger error - nested groups are not supported',
94+
);
95+
}
96+
if (this.#activeSpinner) {
97+
throw new Error(
98+
'Internal Logger error - creating group in active spinner is not supported',
99+
);
100+
}
101+
91102
if (!this.#endsWithBlankLine) {
92103
this.newline();
93104
}
@@ -210,6 +221,12 @@ export class Logger {
210221
failure: (error: unknown) => string;
211222
},
212223
): Promise<void> {
224+
if (this.#activeSpinner) {
225+
throw new Error(
226+
'Internal Logger error - concurrent spinners are not supported',
227+
);
228+
}
229+
213230
process.removeListener('SIGINT', this.#sigintListener);
214231
process.addListener('SIGINT', this.#sigintListener);
215232

0 commit comments

Comments
 (0)