Files
renovate/lib/util/exec/hermit.ts
T
Jamie Tanna ff6761ed23 fix(config/global): return default values for GlobalConfig.get (#42113)
* fix(config/global): return default values for `GlobalConfig.get`

As noted in #39949, it can be awkward working with in `GlobalConfig.get`, where
we would previously need to specify the default value when calling `.get`,
which could lead to drift with the defaults for a given config option.

To reduce this risk, we can rely on a given `default` for a configuration
option, providing that as a default.

Because this now definitely returns a value that matches the config option, it
now allows us to remove `!` assertions.

* fixup! fix(config/global): return default values for `GlobalConfig.get`

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

* fixup! fix(config/global): return default values for `GlobalConfig.get`

* test(presets): remove impossible test

As it's now not possible to set a value as `undefined` (as
`GlobalConfig` will then use the default), this test no longer validates
anything.

* test(workers/repository): add mock

Now `platform` defaults to `github`, we need to mock out the call to
fetch config.

Co-authored-by: Claude Sonnet 4.6 <jamie.tanna+claude-code@mend.io>

* test: coverage

* test: coverage

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <jamie.tanna+claude-code@mend.io>
2026-05-15 17:10:38 +00:00

52 lines
1.6 KiB
TypeScript

import upath from 'upath';
import { GlobalConfig } from '../../config/global.ts';
import { logger } from '../../logger/index.ts';
import { findUpLocal } from '../fs/index.ts';
import { newlineRegex } from '../regex.ts';
import { coerceString } from '../string.ts';
import { rawExec } from './common.ts';
import type { RawExecOptions } from './types.ts';
export function isHermit(): boolean {
return GlobalConfig.get('binarySource') === 'hermit';
}
export async function findHermitCwd(cwd: string): Promise<string> {
const relativeCwd = upath.relative(GlobalConfig.get('localDir'), cwd);
const hermitFile = await findUpLocal('bin/hermit', relativeCwd);
if (hermitFile === null) {
throw new Error(`hermit not found for ${cwd}`);
}
// TODO: types (#22198)
return upath.join(GlobalConfig.get('localDir'), upath.dirname(hermitFile));
}
export async function getHermitEnvs(
rawOptions: RawExecOptions,
): Promise<Record<string, string>> {
const cwd = coerceString(rawOptions.cwd);
const hermitCwd = await findHermitCwd(cwd);
logger.debug({ cwd, hermitCwd }, 'fetching hermit environment variables');
// with -r will output the raw unquoted environment variables to consume
const hermitEnvResp = await rawExec('./hermit env -r', {
...rawOptions,
cwd: hermitCwd,
});
const out: Record<string, string> = {};
const lines = hermitEnvResp.stdout
.split(newlineRegex)
.map((line) => line.trim())
.filter((line) => line.includes('='));
for (const line of lines) {
const equalIndex = line.indexOf('=');
const name = line.substring(0, equalIndex);
out[name] = line.substring(equalIndex + 1);
}
return out;
}