Skip to content

Commit

Permalink
Warn user when trying to spin run test.py (#148)
Browse files Browse the repository at this point in the history
* Warn user when trying to `spin run test.py`

They are probably looking for `spin run python test.py`

Closes #147

* Fix shell invocation on Windows

* Skip test on Windows
  • Loading branch information
stefanv authored Jan 11, 2024
1 parent cf7cf53 commit 9e504d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
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

0 comments on commit 9e504d6

Please sign in to comment.