From 13d4019df9c2d74c4582e0f2c7ea122f4a912806 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Thu, 17 Apr 2025 14:49:24 +0700 Subject: [PATCH] fix: git info check should not throw when debugging --- src/helpers/index.ts | 1 + src/helpers/is-debugging.ts | 12 +++++++++ .../__tests__/server-version.test.ts | 26 +++++++++++++++++++ src/server-version/index.ts | 23 +++++++++++++--- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/helpers/is-debugging.ts diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 4fa4ee7..7210afb 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -1,3 +1,4 @@ export * from './iterators'; export * from './time'; export * from './values'; +export * from './is-debugging'; diff --git a/src/helpers/is-debugging.ts b/src/helpers/is-debugging.ts new file mode 100644 index 0000000..642c27e --- /dev/null +++ b/src/helpers/is-debugging.ts @@ -0,0 +1,12 @@ +/* + * Reliable way to check if we are debugging. Supports ts-node and other tools unlike some other + * approaches that check argv or env vars. It also lazy-loads the `node:inspector` module to avoid + * unnecessary overhead in production environments where this function might not be called. + */ +export function isDebugging() { + type NodeInspectorType = typeof import('node:inspector'); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const inspector = require('node:inspector') as NodeInspectorType; + const url = inspector.url(); + return url !== undefined; +} diff --git a/src/server-version/__tests__/server-version.test.ts b/src/server-version/__tests__/server-version.test.ts index eb5387d..61ddb85 100644 --- a/src/server-version/__tests__/server-version.test.ts +++ b/src/server-version/__tests__/server-version.test.ts @@ -2,10 +2,36 @@ import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import { spawnSync, execSync } from 'child_process'; +import { getServerVersion } from '../index'; +import { isDebugging } from '../../helpers'; const scriptFilePath = path.resolve('bin/api-toolkit-git-info.js'); const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), '.tmp')); +describe('getServerVersion does not throw when debugging', () => { + let debuggingEnabled: boolean; + let originalEnv: string | undefined; + + beforeAll(() => { + debuggingEnabled = isDebugging(); + originalEnv = process.env.NODE_ENV; + process.env.NODE_ENV = 'prod'; + }); + + afterAll(() => { + process.env.NODE_ENV = originalEnv; + }); + + test('getServerVersion does not throw when debugging', () => { + if (!debuggingEnabled) { + console.log(`Skipping test because debugging is not enabled.`); + return; + } + const version = getServerVersion(); + expect(version.branch).toBe('debugging'); + }); +}); + describe('git info script', () => { test('error when git repo data not available', () => { const result = spawnSync(`node "${scriptFilePath}"`, { diff --git a/src/server-version/index.ts b/src/server-version/index.ts index f22393b..b6bd448 100644 --- a/src/server-version/index.ts +++ b/src/server-version/index.ts @@ -1,4 +1,5 @@ -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; +import { isDebugging } from '../helpers/is-debugging'; interface ServerVersion { branch: string; @@ -6,7 +7,7 @@ interface ServerVersion { tag: string; } -function getServerVersion(): ServerVersion { +export function getServerVersion(): ServerVersion { if (process.env.NODE_ENV === 'test') { return { branch: 'test', @@ -14,8 +15,22 @@ function getServerVersion(): ServerVersion { tag: 'v0.0.1', }; } - const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n'); - return { branch, commit, tag }; + + try { + const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n'); + return { branch, commit, tag }; + } catch (error: unknown) { + // If .git-info file does not exist and we are debugging, return a default version + const fileNotExists = (error as NodeJS.ErrnoException).code === 'ENOENT'; + if (fileNotExists && isDebugging()) { + return { + branch: 'debugging', + commit: '123456', + tag: 'v0.0.1', + }; + } + throw error; + } } export const SERVER_VERSION = getServerVersion();