From 150fa90f26cfca02bc5727bb1e64014634b18914 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 14 Sep 2023 12:43:02 +0200 Subject: [PATCH 1/2] improve error messages in ConanApi init failures --- conans/client/cache/cache.py | 9 ++++++--- conans/test/integration/ui/exit_with_code_test.py | 8 ++++++++ conans/test/utils/tools.py | 12 ++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/conans/client/cache/cache.py b/conans/client/cache/cache.py index 8c513cb8858..1036ed8dabf 100644 --- a/conans/client/cache/cache.py +++ b/conans/client/cache/cache.py @@ -48,9 +48,12 @@ def __init__(self, cache_folder): self._store_folder = self.new_config.get("core.cache:storage_path") or \ os.path.join(self.cache_folder, "p") - mkdir(self._store_folder) - db_filename = os.path.join(self._store_folder, 'cache.sqlite3') - self._data_cache = DataCache(self._store_folder, db_filename) + try: + mkdir(self._store_folder) + db_filename = os.path.join(self._store_folder, 'cache.sqlite3') + self._data_cache = DataCache(self._store_folder, db_filename) + except Exception as e: + raise ConanException(f"Couldn't initialize storage in {self._store_folder}: {e}") @property def temp_folder(self): diff --git a/conans/test/integration/ui/exit_with_code_test.py b/conans/test/integration/ui/exit_with_code_test.py index 366c3054486..4cc77217e8c 100644 --- a/conans/test/integration/ui/exit_with_code_test.py +++ b/conans/test/integration/ui/exit_with_code_test.py @@ -2,6 +2,7 @@ from conans.paths import CONANFILE from conans.test.utils.tools import TestClient +from conans.util.files import save class ExitWithCodeTest(unittest.TestCase): @@ -27,3 +28,10 @@ def build(self): error_code = client.run("build .", assert_error=True) self.assertEqual(error_code, 34) self.assertIn("Exiting with code: 34", client.out) + + +def test_wrong_home_error(): + client = TestClient() + save(client.cache.new_config_path, "core.cache:storage_path=P:") + client.run("--version") + assert "Error in Conan initialization: Couldn't initialize storage in" in client.out diff --git a/conans/test/utils/tools.py b/conans/test/utils/tools.py index 376f72234ec..6b62bc41396 100644 --- a/conans/test/utils/tools.py +++ b/conans/test/utils/tools.py @@ -24,7 +24,7 @@ from requests.exceptions import HTTPError from webtest.app import TestApp -from conan.cli.exit_codes import SUCCESS +from conan.cli.exit_codes import SUCCESS, ERROR_GENERAL from conan.internal.cache.cache import PackageLayout, RecipeLayout from conans import REVISIONS from conan.api.conan_api import ConanAPI @@ -32,7 +32,7 @@ from conan.cli.cli import Cli from conans.client.cache.cache import ClientCache from conans.util.env import environment_update -from conans.errors import NotFoundException +from conans.errors import NotFoundException, ConanException from conans.model.manifest import FileTreeManifest from conans.model.package_ref import PkgReference from conans.model.profile import Profile @@ -489,8 +489,12 @@ def _run_cli(self, command_line, assert_error=False): args = shlex.split(command_line) - self.api = ConanAPI(cache_folder=self.cache_folder) - command = Cli(self.api) + try: + self.api = ConanAPI(cache_folder=self.cache_folder) + command = Cli(self.api) + except ConanException as e: + sys.stderr.write("Error in Conan initialization: {}".format(e)) + return ERROR_GENERAL error = SUCCESS trace = None From 99bf14d1760c45261f5766ebeb54e84fcc4d8563 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 14 Sep 2023 13:15:26 +0200 Subject: [PATCH 2/2] fix --- conans/test/integration/configuration/conf/test_conf.py | 5 ++--- .../test/integration/configuration/required_version_test.py | 5 ++--- conans/test/integration/ui/exit_with_code_test.py | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/conans/test/integration/configuration/conf/test_conf.py b/conans/test/integration/configuration/conf/test_conf.py index 2ab82ffd10d..f7e8a01730d 100644 --- a/conans/test/integration/configuration/conf/test_conf.py +++ b/conans/test/integration/configuration/conf/test_conf.py @@ -140,10 +140,9 @@ def test_new_config_file_required_version(): core:required_conan_version=>=2.0 """) save(client.cache.new_config_path, conf) - with pytest.raises(ConanException) as excinfo: - client.run("install .") + client.run("install .", assert_error=True) assert ("Current Conan version (1.26.0) does not satisfy the defined one (>=2.0)" - in str(excinfo.value)) + in client.out) def test_composition_conan_conf_overwritten_by_cli_arg(client): diff --git a/conans/test/integration/configuration/required_version_test.py b/conans/test/integration/configuration/required_version_test.py index 5f3684ef7e5..4be140429e5 100644 --- a/conans/test/integration/configuration/required_version_test.py +++ b/conans/test/integration/configuration/required_version_test.py @@ -17,10 +17,9 @@ def test_wrong_version(self): client = TestClient() client.save({"global.conf": f"core:required_conan_version={required_version}"}, path=client.cache.cache_folder) - with self.assertRaises(ConanException) as error: - client.run("help") + client.run("help", assert_error=True) self.assertIn("Current Conan version (1.26.0) does not satisfy the defined " - "one ({})".format(required_version), str(error.exception)) + "one ({})".format(required_version), client.out) @mock.patch("conans.client.conf.required_version.client_version", "1.22.0") def test_exact_version(self): diff --git a/conans/test/integration/ui/exit_with_code_test.py b/conans/test/integration/ui/exit_with_code_test.py index 4cc77217e8c..8cb0462f583 100644 --- a/conans/test/integration/ui/exit_with_code_test.py +++ b/conans/test/integration/ui/exit_with_code_test.py @@ -32,6 +32,6 @@ def build(self): def test_wrong_home_error(): client = TestClient() - save(client.cache.new_config_path, "core.cache:storage_path=P:") + save(client.cache.new_config_path, "core.cache:storage_path=//") client.run("--version") assert "Error in Conan initialization: Couldn't initialize storage in" in client.out