From bdfa0f011297b749d18954e76c8eb36309e3808d Mon Sep 17 00:00:00 2001
From: benaryorg <binary@benary.org>
Date: Tue, 5 Nov 2024 13:22:10 +0000
Subject: [PATCH] python3Packages.pywebview: build fix for tests

Fixes #353686

Basically the *tests/run.sh* used upstream has a few rough edges and this replaces it with a smoother version.
An issue was also opened on the upstream project to maybe get this smoothed out generally.

Story time for those who are curious.
Basically upstream uses this as a script to call for the CI pipeline where [the builds seem to run smoothly in appveyor](https://ci.appveyor.com/project/r0x0r/pywebview/builds/50791017).
However the general structure of the script iterates over the files, which in earlier versions had been done by collecting the list of tests via pytest itself, which replaced the earliest implementation which was a file hard-coding all the tests to run.
The latter had the benefit of being able to disable tests by commenting them out on our end, however the new version, at least for our purpose, is just a more complicated version of running pytest against the entire thing.
We can't just use plain pytest however (which'd presumably be supported by nixpkgs infra already) because we still need to shove the Qt and xvfb-run shims in between.
So with running pytest as a single command we are now (with this commit) able to specifically disable tests that we know to be flakey using regular pytest means.
With the Qt wrapper function passing extra args to *makeWrapper* we can use the extra flags to pass everything we need, and with the env invocation we avoid polluting the build environment so that the *checkPhase* itself doesn't change the output.

Now on to the actual failing tests, apparently those happened to be related to relative paths which use an internal HTTP server to be served (for absolute paths this is optional), and getting rid of the cwd shenanigans which were required by the upstream version of the script (since it globbed on the current directory) means that somehow pytest now runs these tests without changing directory in a subprocess so the asset used for testing is properly accessible (before this change one could "fix" the tests by changing to an absolute path in the tests).

Signed-off-by: benaryorg <binary@benary.org>
---
 .../python-modules/pywebview/default.nix      | 24 ++++++++-----------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/pkgs/development/python-modules/pywebview/default.nix b/pkgs/development/python-modules/pywebview/default.nix
index 96b1213ca5140..6346c96c62fcc 100644
--- a/pkgs/development/python-modules/pywebview/default.nix
+++ b/pkgs/development/python-modules/pywebview/default.nix
@@ -50,21 +50,17 @@ buildPythonPackage rec {
   ];
 
   checkPhase = ''
-    # Cannot create directory /homeless-shelter/.... Error: FILE_ERROR_ACCESS_DENIED
-    export HOME=$TMPDIR
-    # QStandardPaths: XDG_RUNTIME_DIR not set
-    export XDG_RUNTIME_DIR=$HOME/xdg-runtime-dir
+    # a Qt wrapper is required to run the Qt backend
+    # since the upstream script does not have a way to disable tests individually pytest is used directly instead
+    makeQtWrapper "$(command -v pytest)" tests/run.sh \
+      --set PYWEBVIEW_LOG debug \
+      --add-flags "--deselect tests/test_js_api.py::test_concurrent"
 
-    pushd tests
-    substituteInPlace run.sh \
-      --replace "PYTHONPATH=.." "PYTHONPATH=$PYTHONPATH" \
-      --replace "pywebviewtest test_js_api.py::test_concurrent ''${PYTEST_OPTIONS}" "# skip flaky test_js_api.py::test_concurrent"
-
-    patchShebangs run.sh
-    wrapQtApp run.sh
-
-    xvfb-run -s '-screen 0 800x600x24' ./run.sh
-    popd
+    # HOME and XDG directories are required for the tests
+    env \
+      HOME=$TMPDIR \
+      XDG_RUNTIME_DIR=$TMPDIR/xdg-runtime-dir \
+      xvfb-run -s '-screen 0 800x600x24' tests/run.sh
   '';
 
   pythonImportsCheck = [ "webview" ];
