Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move getChildProcessEnv to break dependency loop #23144

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions lib/util/exec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
removeDockerContainer,
sideCarImage,
} from './docker';
import { getChildProcessEnv } from './env';
import { getHermitEnvs, isHermit } from './hermit';
import type {
DockerOptions,
Expand All @@ -20,37 +19,7 @@ import type {
Opt,
RawExecOptions,
} from './types';

export function getChildEnv({
extraEnv,
env: forcedEnv = {},
}: ExecOptions): Record<string, string> {
const globalConfigEnv = GlobalConfig.get('customEnvVariables');

const inheritedKeys: string[] = [];
for (const [key, val] of Object.entries(extraEnv ?? {})) {
if (is.string(val)) {
inheritedKeys.push(key);
}
}

const parentEnv = getChildProcessEnv(inheritedKeys);
const combinedEnv = {
...extraEnv,
...parentEnv,
...globalConfigEnv,
...forcedEnv,
};

const result: Record<string, string> = {};
for (const [key, val] of Object.entries(combinedEnv)) {
if (is.string(val)) {
result[key] = `${val}`;
}
}

return result;
}
import { getChildEnv } from './utils';

function dockerEnvVars(extraEnv: ExtraEnv, childEnv: ExtraEnv): string[] {
const extraEnvKeys = Object.keys(extraEnv);
Expand Down
35 changes: 35 additions & 0 deletions lib/util/exec/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import is from '@sindresorhus/is';
import { GlobalConfig } from '../../config/global';
import { getChildProcessEnv } from './env';
import type { ExecOptions } from './types';

export function getChildEnv({
extraEnv,
env: forcedEnv = {},
}: ExecOptions): Record<string, string> {
const globalConfigEnv = GlobalConfig.get('customEnvVariables');

const inheritedKeys: string[] = [];
for (const [key, val] of Object.entries(extraEnv ?? {})) {
if (is.string(val)) {
inheritedKeys.push(key);
}
}

const parentEnv = getChildProcessEnv(inheritedKeys);
const combinedEnv = {
...extraEnv,
...parentEnv,
...globalConfigEnv,
...forcedEnv,
};

const result: Record<string, string> = {};
for (const [key, val] of Object.entries(combinedEnv)) {
if (is.string(val)) {
result[key] = `${val}`;
}
}

return result;
}
8 changes: 4 additions & 4 deletions lib/util/template/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { mocked } from '../../../test/util';
import { getOptions } from '../../config/options';
import * as _exec from '../exec';
import * as _execUtils from '../exec/utils';
import * as template from '.';

jest.mock('../exec');
jest.mock('../exec/utils');

const exec = mocked(_exec);
const execUtils = mocked(_execUtils);

describe('util/template/index', () => {
beforeEach(() => {
exec.getChildEnv.mockReturnValue({
execUtils.getChildEnv.mockReturnValue({
CUSTOM_FOO: 'foo',
HOME: '/root',
});
Expand Down
2 changes: 1 addition & 1 deletion lib/util/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import is from '@sindresorhus/is';
import handlebars from 'handlebars';
import { GlobalConfig } from '../../config/global';
import { logger } from '../../logger';
import { getChildEnv } from '../exec';
import { getChildEnv } from '../exec/utils';

handlebars.registerHelper('encodeURIComponent', encodeURIComponent);
handlebars.registerHelper('decodeURIComponent', decodeURIComponent);
Expand Down