From 433e476be044917987acde11674830a3a34521e5 Mon Sep 17 00:00:00 2001 From: utkarsh patrikar Date: Thu, 14 May 2026 16:53:19 +0530 Subject: [PATCH] fix(workflow): restrict publish to main and improve error handling in tests --- .github/workflows/publish.yml | 1 + src/__tests__/health.test.ts | 1 + src/__tests__/logs.test.ts | 1 + src/__tests__/show.test.ts | 16 ++++++++++++++++ src/commands/root.ts | 4 +++- 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2ce5871..b7dcf22 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -15,6 +15,7 @@ on: jobs: publish: + if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest permissions: contents: write diff --git a/src/__tests__/health.test.ts b/src/__tests__/health.test.ts index 78ae0f4..ba0b47a 100644 --- a/src/__tests__/health.test.ts +++ b/src/__tests__/health.test.ts @@ -13,6 +13,7 @@ vi.mock('../ui/spinner', () => ({ createSpinner: vi.fn(() => ({ start: vi.fn().mockReturnThis(), stop: vi.fn().mockReturnThis(), + fail: vi.fn().mockReturnThis(), })), })); diff --git a/src/__tests__/logs.test.ts b/src/__tests__/logs.test.ts index e17a044..923773b 100644 --- a/src/__tests__/logs.test.ts +++ b/src/__tests__/logs.test.ts @@ -13,6 +13,7 @@ vi.mock('../ui/spinner', () => ({ createSpinner: vi.fn(() => ({ start: vi.fn().mockReturnThis(), stop: vi.fn().mockReturnThis(), + fail: vi.fn().mockReturnThis(), })), })); diff --git a/src/__tests__/show.test.ts b/src/__tests__/show.test.ts index f84a55f..5e8467b 100644 --- a/src/__tests__/show.test.ts +++ b/src/__tests__/show.test.ts @@ -3,6 +3,7 @@ import { getRunningContainers } from '../docker/containers'; import { getRunningPods } from '../kubernetes/pods'; import { logger } from '../utils/logger'; import { showRunners, showPods } from '../commands/show'; +import { createSpinner } from '../ui/spinner'; import * as tableUtils from '../ui/table'; // Mock dependencies @@ -47,6 +48,13 @@ describe('show command runners', () => { // Verify: Warning was logged for Kubernetes, but no crash expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Kubernetes is unreachable')); + + // Verify spinner lifecycle + expect(createSpinner).toHaveBeenCalled(); + const spinnerInstance = vi.mocked(createSpinner).mock.results[0].value; + expect(spinnerInstance.start).toHaveBeenCalled(); + expect(spinnerInstance.warn).toHaveBeenCalledWith(expect.stringContaining('Some runners could not be fetched')); + // Verify: Table still rendered with Docker data expect(tableUtils.renderTable).toHaveBeenCalled(); }); @@ -60,6 +68,10 @@ describe('show command runners', () => { expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Docker is unreachable')); expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Kubernetes is unreachable')); expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('No running containers or pods found')); + + // Verify spinner lifecycle + const spinnerInstance = vi.mocked(createSpinner).mock.results[0].value; + expect(spinnerInstance.warn).toHaveBeenCalledWith(expect.stringContaining('Some runners could not be fetched')); }); it('should handle Kubernetes connection failure gracefully in showPods', async () => { @@ -67,5 +79,9 @@ describe('show command runners', () => { // Should not throw await expect(showPods()).resolves.not.toThrow(); + + // Verify spinner lifecycle + const spinnerInstance = vi.mocked(createSpinner).mock.results[0].value; + expect(spinnerInstance.fail).toHaveBeenCalledWith(expect.stringContaining('Failed to fetch Kubernetes pods')); }); }); diff --git a/src/commands/root.ts b/src/commands/root.ts index 21c9099..4f7a12a 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -29,6 +29,7 @@ const run = async () => { showWelcomeBanner('1.1.0'); const spinner = createSpinner('Checking connections...').start(); + let hadError = false; try { const [dockerStatus, k8sStatus, minikubeStatus] = await Promise.all([ checkDockerConnection(), @@ -65,10 +66,11 @@ const run = async () => { console.log(chalk.bold('Commands:\n')); console.log(` kdm show runners\n kdm health all\n kdm watch\n kdm logs \n`); } catch (error) { + hadError = true; spinner.fail(`Connection check failed: ${(error as Error).message}`); } finally { program.outputHelp(); - process.exit(0); + process.exit(hadError ? 1 : 0); } }