Skip to content
Snippets Groups Projects
Unverified Commit 36ff13a8 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor: Fix exec utils coverage (#23018)

parent a7f6514e
No related branches found
No related tags found
No related merge requests found
......@@ -59,7 +59,7 @@ function volumesEql(x: VolumesPair, y: VolumesPair): boolean {
return xFrom === yFrom && xTo === yTo;
}
function prepareVolumes(volumes: VolumeOption[] = []): string[] {
function prepareVolumes(volumes: VolumeOption[]): string[] {
const expanded: (VolumesPair | null)[] = volumes.map(expandVolumeOption);
const filtered: VolumesPair[] = expanded.filter(
(vol): vol is VolumesPair => vol !== null
......
import os from 'node:os';
import { codeBlock } from 'common-tags';
import _findUp from 'find-up';
import upath from 'upath';
import { mockExecAll } from '../../../test/exec-util';
......@@ -62,11 +62,10 @@ describe('util/exec/hermit', () => {
it('should return hermit environment variables when hermit env returns successfully', async () => {
findUp.mockResolvedValueOnce(upath.join(localDir, 'bin/hermit'));
mockExecAll({
stdout:
[
'GOBIN=/usr/src/app/repository-a/.hermit/go/bin',
'PATH=/usr/src/app/repository-a/bin',
].join(os.EOL) + os.EOL,
stdout: codeBlock`
GOBIN=/usr/src/app/repository-a/.hermit/go/bin
PATH=/usr/src/app/repository-a/bin
`,
stderr: '',
});
......
import os from 'node:os';
import upath from 'upath';
import { GlobalConfig } from '../../config/global';
import { logger } from '../../logger';
import { findUpLocal } from '../fs';
import { newlineRegex } from '../regex';
import { rawExec } from './common';
import type { RawExecOptions } from './types';
......@@ -24,7 +24,7 @@ export async function findHermitCwd(cwd: string): Promise<string> {
export async function getHermitEnvs(
rawOptions: RawExecOptions
): Promise<Record<string, string>> {
const cwd = rawOptions.cwd ?? '';
const cwd = rawOptions.cwd ?? /* istanbul ignore next */ '';
const hermitCwd = await findHermitCwd(cwd);
logger.debug({ cwd, hermitCwd }, 'fetching hermit environment variables');
// with -r will output the raw unquoted environment variables to consume
......@@ -33,18 +33,16 @@ export async function getHermitEnvs(
cwd: hermitCwd,
});
const lines = hermitEnvResp.stdout.split(os.EOL);
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 trimmedLine = line.trim();
if (trimmedLine === '') {
continue;
}
const equalIndex = trimmedLine.indexOf('=');
const name = trimmedLine.substring(0, equalIndex);
out[name] = trimmedLine.substring(equalIndex + 1);
const equalIndex = line.indexOf('=');
const name = line.substring(0, equalIndex);
out[name] = line.substring(equalIndex + 1);
}
return out;
......
......@@ -20,7 +20,7 @@ jest.mock('../../modules/datasource');
interface TestInput {
processEnv: Record<string, string>;
inCmd: string | string[];
inOpts: ExecOptions;
inOpts?: ExecOptions;
outCmd: string[];
outOpts: RawExecOptions[];
adminConfig?: Partial<RepoGlobalConfig>;
......@@ -106,6 +106,25 @@ describe('util/exec/index', () => {
},
],
[
'Command without options',
{
processEnv,
inCmd,
inOpts: undefined,
outCmd,
outOpts: [
{
cwd,
encoding,
env: envMock.basic,
timeout: 900000,
maxBuffer: 10485760,
},
],
},
],
[
'Multiple commands',
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment