From 5144af5572d95655679994c3b6abdfaf364fee5d Mon Sep 17 00:00:00 2001
From: Erik Skultety <eskultet@redhat.com>
Date: Thu, 27 Jun 2024 07:32:13 +0200
Subject: [PATCH] package_managers: yarn: Disable corepack prompt when fetching
 yarn

Starting with corepack 0.25.0 [1], corepack now defaults to asking
users if they're okay if its yarn shim downloads a particular version
from the registry:

  ! Corepack is about to download https://repo.yarnpkg.com/.../yarn.js
  ? Do you want to continue? [Y/n]

^This will break tests once we either explicitly move our
corepack == 0.20.0 to corepack >= 0.25.0 implicitly by adopting a newer
NodeJS releases in the multi-stage Docker build conversion future
patches will bring.
Either way, let's prepare for it by setting the new env variable
corepack introduced for this COREPACK_ENABLE_DOWNLOAD_PROMPT [2].

[1] https://github.com/nodejs/corepack/releases/tag/v0.25.0
[2] https://github.com/nodejs/corepack/pull/360#discussion_r1468225799

Signed-off-by: Erik Skultety <eskultet@redhat.com>
---
 cachi2/core/package_managers/yarn/main.py     | 4 +++-
 tests/unit/package_managers/yarn/test_main.py | 8 ++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/cachi2/core/package_managers/yarn/main.py b/cachi2/core/package_managers/yarn/main.py
index 3eb602137..664d49d1c 100644
--- a/cachi2/core/package_managers/yarn/main.py
+++ b/cachi2/core/package_managers/yarn/main.py
@@ -228,7 +228,9 @@ def _generate_environment_variables() -> list[EnvironmentVariable]:
 
 def _verify_corepack_yarn_version(expected_version: semver.Version, source_dir: RootedPath) -> None:
     """Verify that corepack installed the correct version of yarn by checking `yarn --version`."""
-    installed_yarn_version = run_yarn_cmd(["--version"], source_dir).strip()
+    installed_yarn_version = run_yarn_cmd(
+        ["--version"], source_dir, env={"COREPACK_ENABLE_DOWNLOAD_PROMPT": "0"}
+    ).strip()
     try:
         if installed_yarn_version != expected_version:
             raise PackageManagerError(
diff --git a/tests/unit/package_managers/yarn/test_main.py b/tests/unit/package_managers/yarn/test_main.py
index c9f9aa94e..99d861755 100644
--- a/tests/unit/package_managers/yarn/test_main.py
+++ b/tests/unit/package_managers/yarn/test_main.py
@@ -151,7 +151,9 @@ def test_corepack_installed_correct_yarn_version(
     mock_run_yarn_cmd.return_value = corepack_yarn_version
 
     _verify_corepack_yarn_version(expected_yarn_version, rooted_tmp_path)
-    mock_run_yarn_cmd.assert_called_once_with(["--version"], rooted_tmp_path)
+    mock_run_yarn_cmd.assert_called_once_with(
+        ["--version"], rooted_tmp_path, env={"COREPACK_ENABLE_DOWNLOAD_PROMPT": "0"}
+    )
 
 
 @pytest.mark.parametrize(
@@ -173,7 +175,9 @@ def test_corepack_installed_correct_yarn_version_fail(
     with pytest.raises(PackageManagerError):
         _verify_corepack_yarn_version(expected_yarn_version, rooted_tmp_path)
 
-    mock_run_yarn_cmd.assert_called_once_with(["--version"], rooted_tmp_path)
+    mock_run_yarn_cmd.assert_called_once_with(
+        ["--version"], rooted_tmp_path, env={"COREPACK_ENABLE_DOWNLOAD_PROMPT": "0"}
+    )
 
 
 @pytest.mark.parametrize(