Skip to content

Commit

Permalink
Improve test for --pip-cmd option.
Browse files Browse the repository at this point in the history
In `test_new.test_pip_cmd_option`, check that the correct pip-equivalent command is being invoked in the subprocess call.
  • Loading branch information
fcbertoldi committed May 27, 2024
1 parent 8bc10c9 commit 24e3585
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pew/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pew.pew import pew
from pew.pew import main

pew()
main()
23 changes: 14 additions & 9 deletions pew/pew.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def prevent_path_errors():
In this case, for further details please see: https://github.com/berdario/pew#the-environment-doesnt-seem-to-be-activated''')


def first_run_setup():
def first_run_setup(cmd_args):
shell = supported_shell()
if shell:
if shell == 'fish':
Expand All @@ -792,7 +792,7 @@ def first_run_setup():
print("It seems that you're running pew for the first time\n"
"If you want source shell competions and update your prompt, "
"Add the following line to your shell config file:\n %s" % source_cmd)
print('\nWill now continue with the command:', *sys.argv[1:])
print('\nWill now continue with the command:', *cmd_args)
input('[enter]')

def update_config_file(rcpath, source_cmd):
Expand Down Expand Up @@ -827,25 +827,30 @@ def print_commands(cmds):
print(' ' + cmd)


def pew():
def pew(cmd_args):
first_run = makedirs_and_symlink_if_needed(workon_home)
if first_run and sys.stdin.isatty():
first_run_setup()
first_run_setup(cmd_args)

cmds = dict((cmd[:-4], fun)
for cmd, fun in globals().items() if cmd.endswith('_cmd'))
if sys.argv[1:]:
if sys.argv[1] in cmds:
command = cmds[sys.argv[1]]

if cmd_args:
if cmd_args[0] in cmds:
command = cmds[cmd_args[0]]
try:
return command(sys.argv[2:])
return command(cmd_args[1:])
except CalledProcessError as e:
return e.returncode
except KeyboardInterrupt:
pass
else:
err("ERROR: command", sys.argv[1], "does not exist.")
err("ERROR: command", cmd_args[0], "does not exist.")
print_commands(cmds)
sys.exit(1)
else:
print_commands(cmds)


def main():
pew(sys.argv[1:])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def run(self):
'deb': DebCommand
},
entry_points={
'console_scripts': ['pew = pew.pew:pew']},
'console_scripts': ['pew = pew.pew:main']},
classifiers=[
'Programming Language :: Python :: 3',
'Intended Audience :: Developers',
Expand Down
25 changes: 19 additions & 6 deletions tests/test_new.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
from unittest.mock import patch
from uuid import uuid4

import pytest

from pew._utils import invoke_pew
from pew.pew import pew


@pytest.fixture
def mocked_check_call():
with patch("pew.pew.check_call") as mock:
yield mock


@pytest.mark.usefixtures("workon_home")
def test_pip_option():
try:
invoke_pew("new", "--pip-cmd", "pip", str(uuid4()))
except Exception as e:
pytest.fail(str(e))
@pytest.mark.parametrize(
"pip_cmd,exp_call",
[
("pip", ["pip", "install", "pew"]),
("uv", ["uv", "pip", "install", "pew"]),
]
)
def test_pip_cmd_option(mocked_check_call, pip_cmd, exp_call):
pew(["new", "-d", "--pip-cmd", pip_cmd, "-i", "pew", str(uuid4())])
assert mocked_check_call.called
assert mocked_check_call.call_args[0][0] == exp_call

0 comments on commit 24e3585

Please sign in to comment.