Skip to content

Commit

Permalink
refactor: Fix exec utils coverage (#23018)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jun 28, 2023
1 parent a7f6514 commit 36ff13a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/util/exec/docker/index.ts
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions lib/util/exec/hermit.spec.ts
@@ -1,4 +1,4 @@
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';
Expand Down Expand Up @@ -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: '',
});

Expand Down
20 changes: 9 additions & 11 deletions lib/util/exec/hermit.ts
@@ -1,8 +1,8 @@
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';

Expand All @@ -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
Expand All @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion lib/util/exec/index.spec.ts
Expand Up @@ -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>;
Expand Down Expand Up @@ -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',
{
Expand Down

0 comments on commit 36ff13a

Please sign in to comment.