From 508cec1015a1c29cef630007a3f9420530dd68a9 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Fri, 3 Dec 2021 12:07:32 +0100
Subject: [PATCH] refactor: move preCommands out of docker options (#12938)

---
 lib/manager/bundler/artifacts.ts          |  2 +-
 lib/manager/gradle/deep/index.ts          |  2 +-
 lib/manager/mix/artifacts.ts              |  2 +-
 lib/manager/npm/post-update/lerna.ts      |  2 +-
 lib/manager/npm/post-update/npm.ts        |  2 +-
 lib/manager/npm/post-update/pnpm.ts       |  2 +-
 lib/manager/npm/post-update/yarn.ts       |  2 +-
 lib/manager/pip-compile/artifacts.ts      |  6 +++---
 lib/manager/pip_requirements/artifacts.ts |  2 +-
 lib/manager/pipenv/artifacts.ts           |  4 +---
 lib/manager/poetry/artifacts.ts           |  2 +-
 lib/util/exec/common.ts                   |  1 -
 lib/util/exec/docker/index.spec.ts        | 13 ++++++++-----
 lib/util/exec/docker/index.ts             |  2 +-
 lib/util/exec/index.spec.ts               |  3 +--
 lib/util/exec/index.ts                    |  8 ++++++--
 16 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/lib/manager/bundler/artifacts.ts b/lib/manager/bundler/artifacts.ts
index f4d4860d0d..ee48fb52ad 100644
--- a/lib/manager/bundler/artifacts.ts
+++ b/lib/manager/bundler/artifacts.ts
@@ -183,8 +183,8 @@ export async function updateArtifacts(
         image: 'ruby',
         tagScheme: 'ruby',
         tagConstraint: await getRubyConstraint(updateArtifact),
-        preCommands,
       },
+      preCommands,
     };
     await exec(cmd, execOptions);
 
diff --git a/lib/manager/gradle/deep/index.ts b/lib/manager/gradle/deep/index.ts
index 103c893549..0a9b318c76 100644
--- a/lib/manager/gradle/deep/index.ts
+++ b/lib/manager/gradle/deep/index.ts
@@ -74,8 +74,8 @@ export async function executeGradle(
       tagConstraint:
         config.constraints?.java ?? (await getDockerConstraint(gradleRoot)),
       tagScheme: getJavaVersioning(),
-      preCommands: await getDockerPreCommands(gradleRoot),
     },
+    preCommands: await getDockerPreCommands(gradleRoot),
     extraEnv,
   };
   try {
diff --git a/lib/manager/mix/artifacts.ts b/lib/manager/mix/artifacts.ts
index b8160d722e..be162da958 100644
--- a/lib/manager/mix/artifacts.ts
+++ b/lib/manager/mix/artifacts.ts
@@ -75,8 +75,8 @@ export async function updateArtifacts({
     cwdFile: packageFileName,
     docker: {
       image: 'elixir',
-      preCommands,
     },
+    preCommands,
   };
   const command = [
     'mix',
diff --git a/lib/manager/npm/post-update/lerna.ts b/lib/manager/npm/post-update/lerna.ts
index d696d03a8c..058b0ba1f0 100644
--- a/lib/manager/npm/post-update/lerna.ts
+++ b/lib/manager/npm/post-update/lerna.ts
@@ -83,8 +83,8 @@ export async function generateLockFiles(
         image: 'node',
         tagScheme: 'node',
         tagConstraint,
-        preCommands,
       },
+      preCommands,
     };
     // istanbul ignore if
     if (GlobalConfig.get('exposeAllEnv')) {
diff --git a/lib/manager/npm/post-update/npm.ts b/lib/manager/npm/post-update/npm.ts
index 8adfe5698a..801cb61493 100644
--- a/lib/manager/npm/post-update/npm.ts
+++ b/lib/manager/npm/post-update/npm.ts
@@ -67,8 +67,8 @@ export async function generateLockFile(
         image: 'node',
         tagScheme: 'node',
         tagConstraint,
-        preCommands,
       },
+      preCommands,
     };
     // istanbul ignore if
     if (GlobalConfig.get('exposeAllEnv')) {
diff --git a/lib/manager/npm/post-update/pnpm.ts b/lib/manager/npm/post-update/pnpm.ts
index 1e9c030bc3..ffbaef99ca 100644
--- a/lib/manager/npm/post-update/pnpm.ts
+++ b/lib/manager/npm/post-update/pnpm.ts
@@ -46,8 +46,8 @@ export async function generateLockFile(
         image: 'node',
         tagScheme: 'node',
         tagConstraint,
-        preCommands,
       },
+      preCommands,
     };
     // istanbul ignore if
     if (GlobalConfig.get('exposeAllEnv')) {
diff --git a/lib/manager/npm/post-update/yarn.ts b/lib/manager/npm/post-update/yarn.ts
index e98cc18911..a188bd3297 100644
--- a/lib/manager/npm/post-update/yarn.ts
+++ b/lib/manager/npm/post-update/yarn.ts
@@ -148,8 +148,8 @@ export async function generateLockFile(
         image: 'node',
         tagScheme: 'node',
         tagConstraint,
-        preCommands,
       },
+      preCommands,
     };
     // istanbul ignore if
     if (GlobalConfig.get('exposeAllEnv')) {
diff --git a/lib/manager/pip-compile/artifacts.ts b/lib/manager/pip-compile/artifacts.ts
index c5809a31e6..e77e765803 100644
--- a/lib/manager/pip-compile/artifacts.ts
+++ b/lib/manager/pip-compile/artifacts.ts
@@ -67,10 +67,10 @@ export async function updateArtifacts({
         image: 'python',
         tagConstraint,
         tagScheme: 'pep440',
-        preCommands: [
-          `pip install --user ${quote(`pip-tools${pipToolsConstraint}`)}`,
-        ],
       },
+      preCommands: [
+        `pip install --user ${quote(`pip-tools${pipToolsConstraint}`)}`,
+      ],
     };
     logger.debug({ cmd }, 'pip-compile command');
     await exec(cmd, execOptions);
diff --git a/lib/manager/pip_requirements/artifacts.ts b/lib/manager/pip_requirements/artifacts.ts
index 4725a5a938..b602808714 100644
--- a/lib/manager/pip_requirements/artifacts.ts
+++ b/lib/manager/pip_requirements/artifacts.ts
@@ -40,8 +40,8 @@ export async function updateArtifacts({
       docker: {
         image: 'python',
         tagScheme: 'pip_requirements',
-        preCommands: ['pip install hashin'],
       },
+      preCommands: ['pip install hashin'],
     };
     await exec(cmd, execOptions);
     const newContent = await readLocalFile(packageFileName, 'utf8');
diff --git a/lib/manager/pipenv/artifacts.ts b/lib/manager/pipenv/artifacts.ts
index 92b0f371dd..53bffcd1ac 100644
--- a/lib/manager/pipenv/artifacts.ts
+++ b/lib/manager/pipenv/artifacts.ts
@@ -103,10 +103,8 @@ export async function updateArtifacts({
         image: 'python',
         tagConstraint,
         tagScheme: 'pep440',
-        preCommands: [
-          `pip install --user ${quote(`pipenv${pipenvConstraint}`)}`,
-        ],
       },
+      preCommands: [`pip install --user ${quote(`pipenv${pipenvConstraint}`)}`],
     };
     logger.debug({ cmd }, 'pipenv lock command');
     await exec(cmd, execOptions);
diff --git a/lib/manager/poetry/artifacts.ts b/lib/manager/poetry/artifacts.ts
index fc541b4e31..0022848f8d 100644
--- a/lib/manager/poetry/artifacts.ts
+++ b/lib/manager/poetry/artifacts.ts
@@ -139,8 +139,8 @@ export async function updateArtifacts({
         image: 'python',
         tagConstraint,
         tagScheme: 'poetry',
-        preCommands: [poetryInstall],
       },
+      preCommands: [poetryInstall],
     };
     await exec(cmd, execOptions);
     const newPoetryLockContent = await readLocalFile(lockFileName, 'utf8');
diff --git a/lib/util/exec/common.ts b/lib/util/exec/common.ts
index a55f4f7976..8bd56e6c1d 100644
--- a/lib/util/exec/common.ts
+++ b/lib/util/exec/common.ts
@@ -20,7 +20,6 @@ export interface DockerOptions {
   volumes?: Opt<VolumeOption[]>;
   envVars?: Opt<Opt<string>[]>;
   cwd?: Opt<string>;
-  preCommands?: DockerExtraCommands;
 }
 
 export interface RawExecOptions extends ChildProcessExecOptions {
diff --git a/lib/util/exec/docker/index.spec.ts b/lib/util/exec/docker/index.spec.ts
index 2fa288b3db..8b08837dc5 100644
--- a/lib/util/exec/docker/index.spec.ts
+++ b/lib/util/exec/docker/index.spec.ts
@@ -212,7 +212,6 @@ describe('util/exec/docker/index', () => {
     const envVars = ['FOO', 'BAR'];
     const image = 'sample_image';
     const dockerOptions = {
-      preCommands,
       image,
       cwd: '/tmp/foobar',
       envVars,
@@ -234,7 +233,11 @@ describe('util/exec/docker/index', () => {
 
     it('returns executable command', async () => {
       mockExecAll(exec);
-      const res = await generateDockerCommand(commands, dockerOptions);
+      const res = await generateDockerCommand(
+        commands,
+        preCommands,
+        dockerOptions
+      );
       expect(res).toBe(command(image));
     });
 
@@ -245,7 +248,7 @@ describe('util/exec/docker/index', () => {
         ['/tmp/bar', `/tmp/bar`],
         ['/tmp/baz', `/home/baz`],
       ];
-      const res = await generateDockerCommand(commands, {
+      const res = await generateDockerCommand(commands, preCommands, {
         ...dockerOptions,
         volumes: [...volumes, ...volumes],
       });
@@ -259,7 +262,7 @@ describe('util/exec/docker/index', () => {
 
     it('handles tag parameter', async () => {
       mockExecAll(exec);
-      const res = await generateDockerCommand(commands, {
+      const res = await generateDockerCommand(commands, preCommands, {
         ...dockerOptions,
         tag: '1.2.3',
       });
@@ -275,7 +278,7 @@ describe('util/exec/docker/index', () => {
           { version: '2.0.0' },
         ],
       } as never);
-      const res = await generateDockerCommand(commands, {
+      const res = await generateDockerCommand(commands, preCommands, {
         ...dockerOptions,
         tagScheme: 'npm',
         tagConstraint: '^1.2.3',
diff --git a/lib/util/exec/docker/index.ts b/lib/util/exec/docker/index.ts
index 5b3590b98c..c1d8ef4e83 100644
--- a/lib/util/exec/docker/index.ts
+++ b/lib/util/exec/docker/index.ts
@@ -199,12 +199,12 @@ export async function removeDanglingContainers(): Promise<void> {
 
 export async function generateDockerCommand(
   commands: string[],
+  preCommands: string[],
   options: DockerOptions
 ): Promise<string> {
   const { envVars, cwd, tagScheme, tagConstraint } = options;
   let image = options.image;
   const volumes = options.volumes || [];
-  const preCommands = options.preCommands || [];
   const {
     localDir,
     cacheDir,
diff --git a/lib/util/exec/index.spec.ts b/lib/util/exec/index.spec.ts
index 8ab962ec62..acd141c8f0 100644
--- a/lib/util/exec/index.spec.ts
+++ b/lib/util/exec/index.spec.ts
@@ -464,8 +464,8 @@ describe('util/exec/index', () => {
         inOpts: {
           docker: {
             image,
-            preCommands: ['preCommand1', 'preCommand2', null],
           },
+          preCommands: ['preCommand1', 'preCommand2', null],
         },
         outCmd: [
           dockerPullCmd,
@@ -495,7 +495,6 @@ describe('util/exec/index', () => {
         inOpts: {
           docker: {
             image,
-            preCommands: null,
           },
         },
         outCmd: [
diff --git a/lib/util/exec/index.ts b/lib/util/exec/index.ts
index 4e2310bf73..e76defc337 100644
--- a/lib/util/exec/index.ts
+++ b/lib/util/exec/index.ts
@@ -5,6 +5,7 @@ import { TEMPORARY_ERROR } from '../../constants/error-messages';
 import { logger } from '../../logger';
 import { generateInstallCommands } from './buildpack';
 import {
+  DockerExtraCommands,
   DockerOptions,
   ExecResult,
   Opt,
@@ -22,6 +23,7 @@ export interface ExecOptions extends ChildProcessExecOptions {
   extraEnv?: Opt<ExtraEnv>;
   docker?: Opt<DockerOptions>;
   toolConstraints?: Opt<ToolConstraint[]>;
+  preCommands?: DockerExtraCommands;
 }
 
 function getChildEnv({
@@ -74,6 +76,7 @@ function getRawExecOptions(opts: ExecOptions): RawExecOptions {
   delete execOptions.docker;
   delete execOptions.cwdFile;
   delete execOptions.toolConstraints;
+  delete execOptions.preCommands;
 
   const childEnv = getChildEnv(opts);
   const cwd = getCwd(opts);
@@ -125,12 +128,13 @@ async function prepareRawExec(
     const envVars = dockerEnvVars(extraEnv, childEnv);
     const cwd = getCwd(opts);
     const dockerOptions: DockerOptions = { ...docker, cwd, envVars };
-    dockerOptions.preCommands = [
+    const preCommands = [
       ...(await generateInstallCommands(opts.toolConstraints)),
-      ...(dockerOptions.preCommands || []),
+      ...(opts.preCommands || []),
     ];
     const dockerCommand = await generateDockerCommand(
       rawCommands,
+      preCommands,
       dockerOptions
     );
     rawCommands = [dockerCommand];
-- 
GitLab