Skip to content

Commit 9e504d6

Browse files
authored
Warn user when trying to spin run test.py (#148)
* 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
1 parent cf7cf53 commit 9e504d6

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

.github/workflows/test.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ if [[ ${SPIN_PYTHONPATH} == "\$PYTHONPATH" ]]; then
2222
echo "Expected Python path, but got $SPIN_PYTHONPATH instead"
2323
exit 1
2424
fi
25+
2526
echo -e "${MAGENTA}Does \$PYTHONPATH contains site-packages?${NORMAL}"
2627
if [[ ${SPIN_PYTHONPATH} == *"site-packages" ]]; then
2728
echo "Yes"
2829
else
2930
echo "No; it is $SPIN_PYTHONPATH"
3031
fi
32+
3133
echo -e "${MAGENTA}Does \`spin run\` redirect only command output to stdout?${NORMAL}"
3234
# Once we're on Python >3.11, can replace syspath manipulation below with -P flag to Python
3335
VERSION=$(spin run python -c 'import sys; del sys.path[0]; import example_pkg; print(example_pkg.__version__)')
@@ -38,6 +40,20 @@ else
3840
exit 1
3941
fi
4042

43+
if [[ $PLATFORM == linux || $PLATFORM == darwin ]]; then
44+
# Detecting whether a file is executable is not that easy on Windows,
45+
# as it seems to take into consideration whether that file is associated as an executable.
46+
echo -e "${MAGENTA}Does \`spin run foo.py\` warn that \`spin run python foo.py\` is correct?${NORMAL}"
47+
OUT=$( touch __foo.py && spin run __foo.py || true )
48+
rm __foo.py
49+
if [[ $OUT == *"Did you mean to call"* ]]; then
50+
echo "Yes"
51+
else
52+
echo "No, output is: $OUT"
53+
exit 1
54+
fi
55+
fi
56+
4157
prun spin test
4258
echo -e "${MAGENTA}Running \`spin test\`, but with PYTHONPATH set${NORMAL}"
4359
PYTHONPATH=./tmp spin test

spin/cmds/meson.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import copy
23
import json
34
import os
45
import shutil
@@ -496,15 +497,28 @@ def run(ctx, args):
496497

497498
is_posix = sys.platform in ("linux", "darwin")
498499
shell = len(args) == 1
500+
cmd_args = copy.copy(args)
499501
if shell:
500-
args = args[0]
501-
502-
if shell and not is_posix:
503-
# On Windows, we're going to try to use bash
504-
args = ["bash", "-c", args]
502+
cmd_args = args[0]
503+
if not is_posix:
504+
# On Windows, we're going to try to use bash
505+
cmd_args = ["bash", "-c", cmd_args]
505506

506507
_set_pythonpath(quiet=True)
507-
_run(args, echo=False, shell=shell)
508+
p = _run(cmd_args, echo=False, shell=shell, sys_exit=False)
509+
510+
# Is the user trying to run a Python script, without calling the Python interpreter?
511+
executable = args[0]
512+
if (
513+
(p.returncode != 0)
514+
and args[0].endswith(".py")
515+
and os.path.exists(executable)
516+
and (not os.access(executable, os.X_OK))
517+
):
518+
click.secho(
519+
f"Did you mean to call `spin run python {' '.join(args)}`?", fg="bright_red"
520+
)
521+
sys.exit(p.returncode)
508522

509523

510524
@click.command()

0 commit comments

Comments
 (0)