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

Warn user when trying to spin run test.py #148

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ if [[ ${SPIN_PYTHONPATH} == "\$PYTHONPATH" ]]; then
echo "Expected Python path, but got $SPIN_PYTHONPATH instead"
exit 1
fi

echo -e "${MAGENTA}Does \$PYTHONPATH contains site-packages?${NORMAL}"
if [[ ${SPIN_PYTHONPATH} == *"site-packages" ]]; then
echo "Yes"
else
echo "No; it is $SPIN_PYTHONPATH"
fi

echo -e "${MAGENTA}Does \`spin run\` redirect only command output to stdout?${NORMAL}"
# Once we're on Python >3.11, can replace syspath manipulation below with -P flag to Python
VERSION=$(spin run python -c 'import sys; del sys.path[0]; import example_pkg; print(example_pkg.__version__)')
Expand All @@ -38,6 +40,20 @@ else
exit 1
fi

if [[ $PLATFORM == linux || $PLATFORM == darwin ]]; then
# Detecting whether a file is executable is not that easy on Windows,
# as it seems to take into consideration whether that file is associated as an executable.
echo -e "${MAGENTA}Does \`spin run foo.py\` warn that \`spin run python foo.py\` is correct?${NORMAL}"
OUT=$( touch __foo.py && spin run __foo.py || true )
rm __foo.py
if [[ $OUT == *"Did you mean to call"* ]]; then
echo "Yes"
else
echo "No, output is: $OUT"
exit 1
fi
fi

prun spin test
echo -e "${MAGENTA}Running \`spin test\`, but with PYTHONPATH set${NORMAL}"
PYTHONPATH=./tmp spin test
Expand Down
26 changes: 20 additions & 6 deletions spin/cmds/meson.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import copy
import json
import os
import shutil
Expand Down Expand Up @@ -496,15 +497,28 @@ def run(ctx, args):

is_posix = sys.platform in ("linux", "darwin")
shell = len(args) == 1
cmd_args = copy.copy(args)
if shell:
args = args[0]

if shell and not is_posix:
# On Windows, we're going to try to use bash
args = ["bash", "-c", args]
cmd_args = args[0]
if not is_posix:
# On Windows, we're going to try to use bash
cmd_args = ["bash", "-c", cmd_args]

_set_pythonpath(quiet=True)
_run(args, echo=False, shell=shell)
p = _run(cmd_args, echo=False, shell=shell, sys_exit=False)

# Is the user trying to run a Python script, without calling the Python interpreter?
executable = args[0]
if (
(p.returncode != 0)
and args[0].endswith(".py")
and os.path.exists(executable)
and (not os.access(executable, os.X_OK))
):
click.secho(
f"Did you mean to call `spin run python {' '.join(args)}`?", fg="bright_red"
)
sys.exit(p.returncode)


@click.command()
Expand Down