Skip to content

Commit ff1d62c

Browse files
committed
Improve test mode detection and build script organization
1 parent 7b60839 commit ff1d62c

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"lint-ci": "pnpm run check:lint",
5151
"type-ci": "pnpm run check:tsc",
5252
"coverage": "run-s coverage:*",
53-
"coverage:test": "run-s test:prepare test:unit:coverage",
53+
"coverage:test": "run-s pretest:unit test:unit:coverage",
5454
"coverage:percent": "node scripts/get-coverage-percentage.mjs",
5555
"coverage:type": "type-coverage",
5656
"coverage:type:verbose": "type-coverage --detail",
@@ -76,18 +76,18 @@
7676
"lint-staged": "lint-staged",
7777
"precommit": "lint-staged",
7878
"prepare": "husky",
79+
"pretest:unit": "dotenvx -q run -f .env.test -- pnpm run build",
7980
"bs": "pnpm run build:dist:src; pnpm exec socket --",
8081
"s": "pnpm exec socket --",
8182
"test": "run-s check test:*",
82-
"test:prepare": "dotenvx -q run -f .env.test -- pnpm run build && del-cli 'test/**/node_modules'",
8383
"test:unit": "dotenvx -q run -f .env.test -- vitest run",
8484
"test:unit:update": "dotenvx -q run -f .env.test -- vitest run --update",
8585
"test:unit:coverage": "dotenvx -q run -f .env.test -- vitest run --coverage",
8686
"test:validate": "node scripts/validate-tests.mjs",
8787
"test:wrapper": "node scripts/test-wrapper.mjs",
8888
"test-ci": "run-s test:*",
8989
"test-pre-commit": "dotenvx -q run -f .env.precommit -- pnpm test",
90-
"testu": "dotenvx -q run -f .env.test -- run-s test:prepare; pnpm run test:unit:update --",
90+
"testu": "dotenvx -q run -f .env.test -- run-s pretest:unit; pnpm run test:unit:update --",
9191
"testuf": "dotenvx -q run -f .env.test -- pnpm run test:unit:update --",
9292
"update": "run-p --aggregate-output update:**",
9393
"update:deps": "node scripts/taze.mjs",

src/constants.mts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export type ENV = Remap<
188188
SOCKET_CLI_API_PROXY: string
189189
SOCKET_CLI_API_TIMEOUT: number
190190
SOCKET_CLI_API_TOKEN: string
191+
SOCKET_CLI_BUN_PATH: string
191192
SOCKET_CLI_CDXGEN_LOCAL_PATH: string
192193
SOCKET_CLI_COANA_LOCAL_PATH: string
193194
SOCKET_CLI_CONFIG: string
@@ -196,9 +197,19 @@ export type ENV = Remap<
196197
SOCKET_CLI_GITHUB_TOKEN: string
197198
SOCKET_CLI_NO_API_TOKEN: boolean
198199
SOCKET_CLI_NPM_PATH: string
200+
SOCKET_CLI_NPX_PATH: string
199201
SOCKET_CLI_ORG_SLUG: string
202+
SOCKET_CLI_PNPM_PATH: string
203+
SOCKET_CLI_PNPM_V8_PATH: string
204+
SOCKET_CLI_PNPM_V9_PATH: string
205+
SOCKET_CLI_PNPM_V10_PATH: string
206+
SOCKET_CLI_PYTHON_PATH: string
200207
SOCKET_CLI_SFW_LOCAL_PATH: string
208+
SOCKET_CLI_VLT_PATH: string
201209
SOCKET_CLI_VIEW_ALL_RISKS: boolean
210+
SOCKET_CLI_YARN_BERRY_PATH: string
211+
SOCKET_CLI_YARN_CLASSIC_PATH: string
212+
SOCKET_CLI_YARN_PATH: string
202213
SOCKET_CLI_SEA_NODE_VERSION: string
203214
TERM: string
204215
XDG_DATA_HOME: string
@@ -536,12 +547,15 @@ const LAZY_ENV = () => {
536547
const envAsNumber = envHelpers.envAsNumber
537548
const envAsString = envHelpers.envAsString
538549
const GITHUB_TOKEN = envAsString(env['GITHUB_TOKEN'])
550+
const INLINED_SOCKET_CLI_NAME = envAsString(
551+
process.env['INLINED_SOCKET_CLI_NAME'],
552+
)
539553
const INLINED_SOCKET_CLI_PUBLISHED_BUILD = envAsBoolean(
540554
process.env['INLINED_SOCKET_CLI_PUBLISHED_BUILD'],
541555
)
542556
// We inline some environment values so that they CANNOT be influenced by user
543557
// provided environment variables.
544-
return Object.freeze({
558+
const ENV = Object.freeze({
545559
__proto__: null,
546560
// Lazily access registryConstants.ENV.
547561
...regConsts.ENV,
@@ -609,9 +623,7 @@ const LAZY_ENV = () => {
609623
),
610624
// Comp-time inlined Socket package name.
611625
// The '@rollup/plugin-replace' will replace "process.env['INLINED_SOCKET_CLI_NAME']".
612-
INLINED_SOCKET_CLI_NAME: envAsString(
613-
process.env['INLINED_SOCKET_CLI_NAME'],
614-
),
626+
INLINED_SOCKET_CLI_NAME,
615627
// Comp-time inlined flag to determine if this is a published build.
616628
// The '@rollup/plugin-replace' will replace "process.env['INLINED_SOCKET_CLI_PUBLISHED_BUILD']".
617629
INLINED_SOCKET_CLI_PUBLISHED_BUILD,
@@ -783,6 +795,38 @@ const LAZY_ENV = () => {
783795
? false
784796
: envAsBoolean(process.env['VITEST']),
785797
})
798+
799+
// Guard: Detect build/test mode mismatch.
800+
// If the build was NOT made for testing (inlined VITEST is false) but we're
801+
// running in test mode (process.env.VITEST is set), warn about the mismatch.
802+
const runtimeVitestValue = envAsBoolean(env['VITEST'])
803+
if (
804+
INLINED_SOCKET_CLI_NAME === 'socket' &&
805+
!INLINED_SOCKET_CLI_PUBLISHED_BUILD &&
806+
runtimeVitestValue
807+
) {
808+
// Check if running as SEA binary (inline to avoid require issues after bundling).
809+
let isSea = false
810+
try {
811+
const seaModule = require('node:sea')
812+
isSea = seaModule.isSea()
813+
} catch {
814+
// Node.js < 24 or SEA not available
815+
isSea = false
816+
}
817+
818+
if (!isSea) {
819+
const { logger } = require('@socketsecurity/registry/lib/logger')
820+
logger.warn(
821+
'Build/test mode mismatch! Built without VITEST=1 but running in test mode.',
822+
)
823+
logger.warn(
824+
'This causes snapshot failures. Rebuild with: pnpm run pretest:unit',
825+
)
826+
}
827+
}
828+
829+
return ENV
786830
}
787831

788832
const lazyBashRcPath = () => path.join(constants.homePath, '.bashrc')

src/utils/sdk.mts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ export function getDefaultProxyUrl(): string | undefined {
6363
// This Socket API token should be stored globally for the duration of the CLI execution.
6464
let _defaultToken: string | undefined
6565
export function getDefaultApiToken(): string | undefined {
66+
// In test mode: Ignore .env tokens and config file tokens to ensure
67+
// consistent snapshots. Tests must explicitly pass tokens via --config flag.
68+
// This prevents .env files from affecting test snapshots.
69+
// Note: Use process.env directly (not constants.ENV) to check at runtime,
70+
// since constants.ENV['VITEST'] is inlined at build time.
71+
if (process.env['VITEST'] === '1') {
72+
return undefined
73+
}
74+
75+
// When SOCKET_CLI_NO_API_TOKEN=1: Ignore environment variable tokens and only
76+
// check config file. This forces the token to be explicitly set via config.
77+
// Otherwise: Check environment variables first, then config file.
6678
const key = constants.ENV['SOCKET_CLI_NO_API_TOKEN']
6779
? getConfigValueOrUndef(CONFIG_KEY_API_TOKEN) || _defaultToken
6880
: constants.ENV['SOCKET_CLI_API_TOKEN'] ||

0 commit comments

Comments
 (0)