From f158cc2b6e572cb3019ca2334c4cbdc4183d9fbc Mon Sep 17 00:00:00 2001 From: Bruno Lopes Date: Mon, 21 Apr 2025 09:38:07 -0300 Subject: [PATCH] libgimp/tests: Port libgimp-run-python-test.sh to Python --- libgimp/tests/libgimp-run-c-test.py | 2 +- libgimp/tests/libgimp-run-python-test.py | 33 ++++++++++++++++++++++++ libgimp/tests/libgimp-run-python-test.sh | 27 ------------------- libgimp/tests/meson.build | 2 +- 4 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 libgimp/tests/libgimp-run-python-test.py delete mode 100644 libgimp/tests/libgimp-run-python-test.sh diff --git a/libgimp/tests/libgimp-run-c-test.py b/libgimp/tests/libgimp-run-c-test.py index 87e6ed9efe..4fde32ca1a 100644 --- a/libgimp/tests/libgimp-run-c-test.py +++ b/libgimp/tests/libgimp-run-c-test.py @@ -16,4 +16,4 @@ cmd = ( f"result = proc.run(None); " f"print('SUCCESS') if result.index(0) == Gimp.PDBStatusType.SUCCESS else gimp_c_assert('{TEST_FILE}', result.index(1), False);" ) -subprocess.run(['python3', GIMP_EXE, "-nis", "--batch-interpreter", "python-fu-eval", "-b", cmd, "--quit"], check=True) +subprocess.run([sys.executable, GIMP_EXE, "-nis", "--batch-interpreter", "python-fu-eval", "-b", cmd, "--quit"], check=True) diff --git a/libgimp/tests/libgimp-run-python-test.py b/libgimp/tests/libgimp-run-python-test.py new file mode 100644 index 0000000000..89e9b2ce2f --- /dev/null +++ b/libgimp/tests/libgimp-run-python-test.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +import os +import sys +import subprocess +from pathlib import Path + +GIMP_EXE = sys.argv[1] +TEST_FILE = sys.argv[2] +SRC_DIR = os.path.dirname(TEST_FILE) +SRC_DIR = Path(os.path.realpath(SRC_DIR)).resolve().as_posix() + +if not os.path.isfile(TEST_FILE): + print(f"ERROR: file '{TEST_FILE}' does not exist!") + sys.exit(1) + +with open(TEST_FILE, 'r') as f: + first_char = f.read(1) + +if first_char != '#': + # Note: I don't actually care that it's a shebang, just that it's a comment, + # hence a useless line, because I'm going to remove it and replace it with + # gimp_assert() import line. + # This will make much easier to debug tests with meaningful line numbers. + print(f"ERROR: file '{TEST_FILE}' should start with a shebang: #!/usr/bin/env python3") + sys.exit(1) + +header = f"""import os; import sys; sys.path.insert(0, '{SRC_DIR}'); from pygimp.utils import gimp_assert; +import pygimp.utils; pygimp.utils.gimp_test_filename = '{TEST_FILE}'""" + +with open(TEST_FILE, 'r') as f: + content = f.readlines() +cmd = header + ''.join(content[1:]) +subprocess.run([sys.executable, GIMP_EXE, "-nis", "--batch-interpreter", "python-fu-eval", "-b", cmd, "--quit"], check=True) diff --git a/libgimp/tests/libgimp-run-python-test.sh b/libgimp/tests/libgimp-run-python-test.sh deleted file mode 100644 index d22b88f90f..0000000000 --- a/libgimp/tests/libgimp-run-python-test.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh - -GIMP_EXE=$1 -TEST_FILE=$2 -SRC_DIR=`dirname $TEST_FILE` -SRC_DIR=`realpath $SRC_DIR` - -if [ ! -f "$TEST_FILE" ]; then - echo "ERROR: file '$TEST_FILE' does not exist!" - return 1; -fi - -first_char=`head -c1 "$TEST_FILE"` - -if [ $first_char != '#' ]; then - # Note: I don't actually care that it's a shebang, just that it's a comment, - # hence a useless line, because I'm going to remove it and replace it with - # gimp_assert() import line. - # This will make much easier to debug tests with meaningful line numbers. - echo "ERROR: file '$TEST_FILE' should start with a shebang: #!/usr/bin/env python3" - return 1; -fi - -header="import os; import sys; sys.path.insert(0, '$SRC_DIR'); from pygimp.utils import gimp_assert;" -header="$header import pygimp.utils; pygimp.utils.gimp_test_filename = '$TEST_FILE'" - -(echo "$header" && tail -n +2 "$TEST_FILE") | python3 "$GIMP_EXE" -nis --batch-interpreter "python-fu-eval" -b - --quit diff --git a/libgimp/tests/meson.build b/libgimp/tests/meson.build index b4cfcd673d..4475130156 100644 --- a/libgimp/tests/meson.build +++ b/libgimp/tests/meson.build @@ -23,7 +23,7 @@ test_env=gimp_run_env test_env.append('GIMP_TESTING_PLUGINDIRS', meson.project_build_root() / 'libgimp/tests/c-tests/') test_env.set('GIMP_TESTING_ABS_TOP_SRCDIR', meson.project_source_root()) -run_python_test = find_program('./libgimp-run-python-test.sh') +run_python_test = find_program('./libgimp-run-python-test.py') run_c_test = find_program('./libgimp-run-c-test.py') foreach test_name : tests basename = 'test-' + test_name