Repository: conan-io/conan
Pull request #17180 (2024-10-16 23:55:16)

refactor conans-conan userio
Changelog: Omit
Docs: Omit





Patch:
diff --git a/conan/api/conan_api.py b/conan/api/conan_api.py
index 7c5f364bd..160492367 100644
--- a/conan/api/conan_api.py
+++ b/conan/api/conan_api.py
@@ -1,5 +1,6 @@
 import sys
 
+from conan.api.output import init_colorama
 from conan.api.subapi.cache import CacheAPI
 from conan.api.subapi.command import CommandAPI
 from conan.api.subapi.local import LocalAPI
@@ -18,7 +19,6 @@ from conan.api.subapi.remove import RemoveAPI
 from conan.api.subapi.search import SearchAPI
 from conan.api.subapi.upload import UploadAPI
 from conans.client.migrations import ClientMigrator
-from conans.client.userio import init_colorama
 from conan.errors import ConanException
 from conans.model.version import Version
 from conan.internal.paths import get_conan_user_home
diff --git a/conans/client/userio.py b/conan/api/input.py
similarity index 75%
rename from conans/client/userio.py
rename to conan/api/input.py
index 814026a86..3fae3b4fa 100644
--- a/conans/client/userio.py
+++ b/conan/api/input.py
@@ -1,49 +1,10 @@
 import getpass
-import os
 import sys
 
 from conan.errors import ConanException
 
 
-def is_terminal(stream):
-    return hasattr(stream, "isatty") and stream.isatty()
-
-
-def color_enabled(stream):
-    """
-    NO_COLOR: No colors
-
-    https://no-color.org/
-
-    Command-line software which adds ANSI color to its output by default should check for the
-    presence of a NO_COLOR environment variable that, when present (**regardless of its value**),
-    prevents the addition of ANSI color.
-
-    CLICOLOR_FORCE: Force color
-
-    https://bixense.com/clicolors/
-    """
-
-    if os.getenv("CLICOLOR_FORCE", "0") != "0":
-        # CLICOLOR_FORCE != 0, ANSI colors should be enabled no matter what.
-        return True
-
-    if os.getenv("NO_COLOR") is not None:
-        return False
-    return is_terminal(stream)
-
-
-def init_colorama(stream):
-    import colorama
-    if color_enabled(stream):
-        if os.getenv("CLICOLOR_FORCE", "0") != "0":
-            # Otherwise it is not really forced if colorama doesn't feel it
-            colorama.init(strip=False, convert=False)
-        else:
-            colorama.init()
-
-
-class UserInput(object):
+class UserInput:
     """Class to interact with the user, used to show messages and ask for information"""
 
     def __init__(self, non_interactive):
diff --git a/conan/api/output.py b/conan/api/output.py
index dd4a8e795..d224498bd 100644
--- a/conan/api/output.py
+++ b/conan/api/output.py
@@ -6,7 +6,6 @@ from threading import Lock
 
 from colorama import Fore, Style
 
-from conans.client.userio import color_enabled
 from conan.errors import ConanException
 
 LEVEL_QUIET = 80  # -q
@@ -50,6 +49,40 @@ if os.environ.get("CONAN_COLOR_DARK"):
     Color.BRIGHT_GREEN = Fore.GREEN
 
 
+def init_colorama(stream):
+    import colorama
+    if _color_enabled(stream):
+        if os.getenv("CLICOLOR_FORCE", "0") != "0":
+            # Otherwise it is not really forced if colorama doesn't feel it
+            colorama.init(strip=False, convert=False)
+        else:
+            colorama.init()
+
+
+def _color_enabled(stream):
+    """
+    NO_COLOR: No colors
+
+    https://no-color.org/
+
+    Command-line software which adds ANSI color to its output by default should check for the
+    presence of a NO_COLOR environment variable that, when present (**regardless of its value**),
+    prevents the addition of ANSI color.
+
+    CLICOLOR_FORCE: Force color
+
+    https://bixense.com/clicolors/
+    """
+
+    if os.getenv("CLICOLOR_FORCE", "0") != "0":
+        # CLICOLOR_FORCE != 0, ANSI colors should be enabled no matter what.
+        return True
+
+    if os.getenv("NO_COLOR") is not None:
+        return False
+    return hasattr(stream, "isatty") and stream.isatty()
+
+
 class ConanOutput:
     # Singleton
     _conan_output_level = LEVEL_STATUS
@@ -62,7 +95,7 @@ class ConanOutput:
         self._scope = scope
         # FIXME:  This is needed because in testing we are redirecting the sys.stderr to a buffer
         #         stream to capture it, so colorama is not there to strip the color bytes
-        self._color = color_enabled(self.stream)
+        self._color = _color_enabled(self.stream)
 
     @classmethod
     def define_silence_warnings(cls, warnings):
@@ -253,7 +286,7 @@ def cli_out_write(data, fg=None, bg=None, endline="\n", indentation=0):
 
     fg_ = fg or ''
     bg_ = bg or ''
-    if (fg or bg) and color_enabled(sys.stdout):
+    if (fg or bg) and _color_enabled(sys.stdout):
         data = f"{' ' * indentation}{fg_}{bg_}{data}{Style.RESET_ALL}{endline}"
     else:
         data = f"{' ' * indentation}{data}{endline}"
diff --git a/conan/api/subapi/new.py b/conan/api/subapi/new.py
index 081f7e754..ae26f9d13 100644
--- a/conan/api/subapi/new.py
+++ b/conan/api/subapi/new.py
@@ -1,7 +1,7 @@
 import fnmatch
 import os
 
-from jinja2 import Template, StrictUndefined
+from jinja2 import Template, StrictUndefined, UndefinedError, Environment, meta
 
 from conan.errors import ConanException
 from conans.util.files import load
@@ -118,11 +118,35 @@ class NewAPI:
         definitions["package_name"] = as_package_name(name)
         definitions["as_name"] = as_name
         definitions["names"] = lambda x: ", ".join(r.split("/", 1)[0] for r in x)
-        for k, v in template_files.items():
-            k = Template(k, keep_trailing_newline=True, undefined=StrictUndefined).render(
-                **definitions)
-            v = Template(v, keep_trailing_newline=True, undefined=StrictUndefined).render(
-                **definitions)
-            if v:
-                result[k] = v
+        if "name" not in definitions:
+            definitions["name"] = "mypkg"
+        if "version" not in definitions:
+            definitions["version"] = "0.1"
+        version = definitions.get("version")
+        if isinstance(version, list):
+            raise ConanException(f"version argument can't be multiple: {version}")
+
+        try:
+            for k, v in template_files.items():
+                k = Template(k, keep_trailing_newline=True, undefined=StrictUndefined).render(
+                    **definitions)
+                v = Template(v, keep_trailing_newline=True, undefined=StrictUndefined).render(
+                    **definitions)
+                if v:
+                    result[k] = v
+        except UndefinedError:
+            template_vars = []
+            for templ_str in template_files.values():
+                env = Environment()
+                ast = env.parse(templ_str)
+                template_vars.extend(meta.find_undeclared_variables(ast))
+
+            injected_vars = {"conan_version", "package_name", "as_name"}
+            optional_vars = {"requires", "tool_requires", "output_root_dir"}
+            template_vars = list(set(template_vars) - injected_vars - optional_vars)
+            template_vars.sort()
+
+            raise ConanException("Missing definitions for the template. "
+                                 "Required definitions are: {}"
+                                 .format(", ".join("'{}'".format(var) for var in template_vars)))
         return result
diff --git a/conan/cli/commands/new.py b/conan/cli/commands/new.py
index 1461eaff4..18b3b0c96 100644
--- a/conan/cli/commands/new.py
+++ b/conan/cli/commands/new.py
@@ -1,8 +1,6 @@
 import os
 import shutil
 
-from jinja2 import Environment, meta, exceptions
-
 from conan.api.output import ConanOutput
 from conan.cli.command import conan_command
 from conan.errors import ConanException
@@ -57,25 +55,7 @@ def new(conan_api, parser, *args):
     if not template_files and not non_template_files:
         raise ConanException("Template doesn't exist or not a folder: {}".format(args.template))
 
-    try:
-        template_files = conan_api.new.render(template_files, definitions)
-    except exceptions.UndefinedError:
-        def get_template_vars():
-            template_vars = []
-            for _, templ_str in template_files.items():
-                env = Environment()
-                ast = env.parse(templ_str)
-                template_vars.extend(meta.find_undeclared_variables(ast))
-
-            injected_vars = {"conan_version", "package_name", "as_name"}
-            optional_vars = {"requires", "tool_requires", "output_root_dir"}
-            template_vars = list(set(template_vars) - injected_vars - optional_vars)
-            template_vars.sort()
-            return template_vars
-
-        raise ConanException("Missing definitions for the template. "
-                             "Required definitions are: {}"
-                             .format(", ".join("'{}'".format(var) for var in get_template_vars())))
+    template_files = conan_api.new.render(template_files, definitions)
 
     # Saving the resulting files
     output = ConanOutput()
diff --git a/conan/cli/commands/remove.py b/conan/cli/commands/remove.py
index 85e925e79..5e488b9b0 100644
--- a/conan/cli/commands/remove.py
+++ b/conan/cli/commands/remove.py
@@ -1,10 +1,10 @@
 from conan.api.conan_api import ConanAPI
 from conan.api.model import ListPattern, MultiPackagesList
 from conan.api.output import cli_out_write, ConanOutput
+from conan.api.input import UserInput
 from conan.cli import make_abs_path
 from conan.cli.command import conan_command, OnceArgument
 from conan.cli.commands.list import print_list_json, print_serial
-from conans.client.userio import UserInput
 from conan.errors import ConanException
 
 
diff --git a/conan/cli/commands/upload.py b/conan/cli/commands/upload.py
index 282a70df9..696cda874 100644
--- a/conan/cli/commands/upload.py
+++ b/conan/cli/commands/upload.py
@@ -4,7 +4,7 @@ from conan.api.output import ConanOutput
 from conan.cli import make_abs_path
 from conan.cli.command import conan_command, OnceArgument
 from conan.cli.commands.list import print_list_json, print_serial
-from conans.client.userio import UserInput
+from conan.api.input import UserInput
 from conan.errors import ConanException
 
 
diff --git a/conans/client/rest/remote_credentials.py b/conans/client/rest/remote_credentials.py
index c1107f192..a49142c28 100644
--- a/conans/client/rest/remote_credentials.py
+++ b/conans/client/rest/remote_credentials.py
@@ -4,11 +4,11 @@ import platform
 
 from jinja2 import Template
 
+from conan.api.input import UserInput
 from conan.internal.cache.home_paths import HomePaths
 
 from conans.client.loader import load_python_file
 from conan.api.output import ConanOutput
-from conans.client.userio import UserInput
 from conan.internal.errors import scoped_traceback
 from conan.errors import ConanException
 from conans.util.files import load
diff --git a/conans/model/requires.py b/conans/model/requires.py
index d9a9dbc76..cb819d74e 100644
--- a/conans/model/requires.py
+++ b/conans/model/requires.py
@@ -254,6 +254,7 @@ class Requirement:
         self.transitive_libs = self.transitive_libs or other.transitive_libs
         if not other.test:
             self.test = False  # it it was previously a test, but also required by non-test
+        self.is_test = self.is_test or other.is_test
         # package_id_mode is not being propagated downstream. So it is enough to check if the
         # current require already defined it or not
         if self.package_id_mode is None:
@@ -344,6 +345,7 @@ class Requirement:
 
         if self.test:
             downstream_require.test = True
+        downstream_require.is_test = require.is_test
 
         # If the current one is resolving conflicts, the downstream one will be too
         downstream_require.force = require.force


Test patch:
diff --git a/test/integration/command/new_test.py b/test/integration/command/new_test.py
index b66533730..4700ede4e 100644
--- a/test/integration/command/new_test.py
+++ b/test/integration/command/new_test.py
@@ -36,15 +36,6 @@ class TestNewCommand:
         client.run("export . --user=myuser --channel=testing")
         assert "pkg/1.3@myuser/testing" in client.out
 
-    def test_new_missing_definitions(self):
-        client = TestClient()
-        client.run("new cmake_lib", assert_error=True)
-        assert "Missing definitions for the template. Required definitions are: 'name', 'version'" in client.out
-        client.run("new cmake_lib -d name=myname", assert_error=True)
-        assert "Missing definitions for the template. Required definitions are: 'name', 'version'" in client.out
-        client.run("new cmake_lib -d version=myversion", assert_error=True)
-        assert "Missing definitions for the template. Required definitions are: 'name', 'version'" in client.out
-
     def test_new_basic_template(self):
         tc = TestClient()
         tc.run("new basic")
@@ -56,6 +47,20 @@ class TestNewCommand:
         assert 'self.requires("ai/1.0")' in conanfile
         assert 'name = "mygame"' in conanfile
 
+    def test_new_defaults(self):
+        c = TestClient()
+        for t in ("cmake_lib", "cmake_exe", "meson_lib", "meson_exe", "msbuild_lib", "msbuild_exe",
+                  "bazel_lib", "bazel_exe", "autotools_lib", "autotools_exe"):
+            c.run(f"new {t} -f")
+            conanfile = c.load("conanfile.py")
+            assert 'name = "mypkg"' in conanfile
+            assert 'version = "0.1"' in conanfile
+
+            c.run(f"new {t} -d name=mylib -f")
+            conanfile = c.load("conanfile.py")
+            assert 'name = "mylib"' in conanfile
+            assert 'version = "0.1"' in conanfile
+
 
 class TestNewCommandUserTemplate:
 
@@ -151,5 +156,5 @@ class TestNewErrors:
         assert "ERROR: name argument can't be multiple: ['hello', '0.1']" in client.out
 
         # It will create a list and assign to it, but it will not fail ugly
-        client.run("new cmake_lib -d name=pkg -d version=0.1 -d version=0.2")
-        assert "['0.1', '0.2']" in client.load("conanfile.py")
+        client.run("new cmake_lib -d name=pkg -d version=0.1 -d version=0.2", assert_error=True)
+        assert "ERROR: version argument can't be multiple: ['0.1', '0.2']" in client.out
diff --git a/test/integration/graph/test_test_requires.py b/test/integration/graph/test_test_requires.py
index f85391d9d..5342bc3ad 100644
--- a/test/integration/graph/test_test_requires.py
+++ b/test/integration/graph/test_test_requires.py
@@ -198,6 +198,38 @@ def test_requires_transitive_diamond_components():
     assert "libc/0.1: Created package" in c.out
 
 
+def test_requires_transitive_diamond_components_order():
+    """
+    like the above, but in different order
+     libc  --(test-requires)---> liba
+       |-----> libb -----------/
+    https://github.com/conan-io/conan/issues/17164
+    """
+    c = TestClient(light=True)
+    libc = textwrap.dedent("""
+        from conan import ConanFile
+        class LibC(ConanFile):
+            name = "libc"
+            version = "0.1"
+
+            def requirements(self):
+                self.test_requires("liba/1.0")
+                self.requires("libb/1.0")
+
+            def package_info(self):
+                 self.cpp_info.components["comp"].libs = ["libc"]
+                 self.cpp_info.components["comp"].requires.append("libb::libb")
+        """)
+    c.save({"liba/conanfile.py": GenConanfile("liba", "1.0"),
+            "libb/conanfile.py": GenConanfile("libb", "1.0").with_requires("liba/1.0"),
+            "libc/conanfile.py": libc})
+    c.run("create liba")
+    c.run("create libb")
+    c.run("create libc")
+    # This used to crash due to component not defined to liba
+    assert "libc/0.1: Created package" in c.out
+
+
 def test_test_requires_options():
     """ the default_options = {} values also propagate to ``test_requires()`` """
     c = TestClient(light=True)
diff --git a/test/integration/remote/rest_api_test.py b/test/integration/remote/rest_api_test.py
index 014af5b00..568550796 100644
--- a/test/integration/remote/rest_api_test.py
+++ b/test/integration/remote/rest_api_test.py
@@ -9,7 +9,7 @@ from conans.client.rest.conan_requester import ConanRequester
 from conans.client.rest.rest_client import RestApiClient
 from conans.model.conf import ConfDefinition
 from conan.test.utils.env import environment_update
-from conans.client.userio import UserInput
+from conan.api.input import UserInput
 from conans.model.manifest import FileTreeManifest
 from conans.model.package_ref import PkgReference
 from conans.model.recipe_ref import RecipeReference
diff --git a/test/unittests/client/conan_output_test.py b/test/unittests/client/conan_output_test.py
index 772a8b4b1..fd35c68a4 100644
--- a/test/unittests/client/conan_output_test.py
+++ b/test/unittests/client/conan_output_test.py
@@ -1,4 +1,3 @@
-# coding=utf-8
 import sys
 import unittest
 from unittest import mock
@@ -6,8 +5,7 @@ from unittest import mock
 import pytest
 from parameterized import parameterized
 
-from conan.api.output import ConanOutput
-from conans.client.userio import init_colorama
+from conan.api.output import ConanOutput, init_colorama
 
 
 class ConanOutputTest(unittest.TestCase):
diff --git a/test/unittests/client/userio_test.py b/test/unittests/client/userio_test.py
index 21fd2d1de..f75af150b 100644
--- a/test/unittests/client/userio_test.py
+++ b/test/unittests/client/userio_test.py
@@ -1,16 +1,14 @@
-# coding=utf-8
-
 import unittest
 
 from mock import mock
 
-from conans.client.userio import UserInput
+from conan.api.input import UserInput
 
 
 class UserInputTest(unittest.TestCase):
 
-    @mock.patch("conans.client.userio.UserInput.get_username", return_value="username")
-    @mock.patch("conans.client.userio.UserInput.get_password", return_value="passwd")
+    @mock.patch("conan.api.input.UserInput.get_username", return_value="username")
+    @mock.patch("conan.api.input.UserInput.get_password", return_value="passwd")
     def test_request_login(self, m1, m2):
         user_input = UserInput(non_interactive=False)