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

Fix Conan V2 cli broken help #7394

Merged
merged 9 commits into from Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions conans/cli/cli.py
Expand Up @@ -81,13 +81,15 @@ def __init__(self, conan_api):
self._commands = {}
conan_commands_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "commands")
for module in pkgutil.iter_modules([conan_commands_path]):
self._add_command("conans.cli.commands.{}".format(module.name), module.name)
module_name = module[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a Py2.7 thing? If that is the case, I am happy with the check you did in the test to run the test only in Py3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because in 3.5 they changed the return from a tuple to a named tuple for iter_modules return value:
https://docs.python.org/3.5/library/pkgutil.html#pkgutil.iter_modules
https://docs.python.org/3.6/library/pkgutil.html#pkgutil.iter_modules
Python2 issue is another one.

self._add_command("conans.cli.commands.{}".format(module_name), module_name)
if get_env("CONAN_USER_COMMANDS", default=False):
user_commands_path = os.path.join(self._conan_api.cache_folder, "commands")
sys.path.append(user_commands_path)
for module in pkgutil.iter_modules([user_commands_path]):
if module.name.startswith("cmd_"):
self._add_command(module.name, module.name.replace("cmd_", ""))
module_name = module[1]
if module_name.startswith("cmd_"):
self._add_command(module_name, module_name.replace("cmd_", ""))

def _add_command(self, import_path, method_name):
try:
Expand Down Expand Up @@ -131,7 +133,8 @@ def _print_similar(self, command):
self._out.writeln("")

def help_message(self):
self.commands["help"].method(self.conan_api, self.commands, self.groups)
self.commands["help"].method(conan_api=self.conan_api, parser=self.commands["help"].parser,
commands=self.commands, groups=self.groups)

def run(self, *args):
""" Entry point for executing commands, dispatcher to class
Expand Down Expand Up @@ -164,7 +167,8 @@ def run(self, *args):
self._print_similar(command_argument)
raise ConanException("Unknown command %s" % str(exc))

command.run(self.conan_api, args[0][1:], parser=self.commands[command_argument].parser,
command.run(args[0][1:], conan_api=self.conan_api,
parser=self.commands[command_argument].parser,
commands=self.commands, groups=self.groups)

return SUCCESS
Expand Down
2 changes: 1 addition & 1 deletion conans/cli/command.py
Expand Up @@ -34,7 +34,7 @@ def __init__(self, method, group, formatters=None):
self._parser.add_argument('-o', '--output', default=default_output, choices=formatters_list,
action=OnceArgument, help=self._output_help_message)

def run(self, conan_api, *args, **kwargs):
def run(self, *args, conan_api=None, **kwargs):
czoido marked this conversation as resolved.
Show resolved Hide resolved
try:
info = self._method(*args, conan_api=conan_api, **kwargs)
parser_args = self._parser.parse_args(*args)
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions conans/test/functional/command_v2/help_test.py
@@ -0,0 +1,21 @@
import unittest

from conans.client.tools import environment_append, save, six
from conans.test.utils.tools import TestClient


@unittest.skipIf(six.PY2, "v2.0: Only testing for Python 3")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably both the skipIf and the run() method can be put in a common class and all command_v2 tests use it.

class CliHelpTest(unittest.TestCase):

def run(self, *args, **kwargs):
with environment_append({"CONAN_V2_CLI": "1"}):
super(CliHelpTest, self).run(*args, **kwargs)

def help_command_test(self):
client = TestClient()

client.run("help")
self.assertIn("Shows help for a specific command", client.out)

client.run("help search")
self.assertIn("Searches for package recipes whose name contain", client.out)
8 changes: 6 additions & 2 deletions conans/test/utils/tools.py
Expand Up @@ -27,7 +27,6 @@
from conans import load
from conans.client.cache.cache import ClientCache
from conans.client.cache.remote_registry import Remotes
from conans.client.command import Command
from conans.client.conan_api import Conan
from conans.client.output import ConanOutput
from conans.client.rest.file_uploader import IterableToFileAdapter
Expand Down Expand Up @@ -830,7 +829,12 @@ def run(self, command_line, user_io=None, assert_error=False):
"""
conan = self.get_conan_api(user_io)
self.api = conan
command = Command(conan)
if os.getenv("CONAN_V2_CLI"):
from conans.cli.cli import Cli
command = Cli(conan)
else:
from conans.client.command import Command
command = Command(conan)
args = shlex.split(command_line)
current_dir = os.getcwd()
os.chdir(self.current_folder)
Expand Down