Skip to content

Commit

Permalink
Merge pull request #59844 from nyalldawson/fix_blocking_process_test
Browse files Browse the repository at this point in the history
Fix blocking process test
  • Loading branch information
troopa81 authored Dec 12, 2024
2 parents fb501d5 + 7dbd894 commit 1f080b9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 81 deletions.
1 change: 0 additions & 1 deletion .ci/test_blocklist_qt6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ PyQgsStyleStorageMssql
PyQgsAnnotation
PyQgsAuthenticationSystem
PyQgsAuthManagerOAuth2OWSTest
PyQgsBlockingProcess
PyQgsCodeEditor
PyQgsDelimitedTextProvider
PyQgsEditWidgets
Expand Down
23 changes: 23 additions & 0 deletions python/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,29 @@ def get_test_data_path(file_path: str) -> Path:
file_path[1:] if file_path.startswith("/") else file_path
)

@staticmethod
def strip_std_ignorable_errors(output: str) -> str:
"""
Strips out ignorable warnings and errors from stdout/stderr output
"""
return "\n".join(
[
e
for e in output.splitlines()
if e
not in (
"Problem with GRASS installation: GRASS was not found or is not correctly installed",
"QStandardPaths: wrong permissions on runtime directory /tmp, 0777 instead of 0700",
"MESA: error: ZINK: failed to choose pdev",
"MESA: error: ZINK: vkEnumeratePhysicalDevices failed (VK_ERROR_INITIALIZATION_FAILED)",
"glx: failed to create drisw screen",
"failed to load driver: zink",
"QML debugging is enabled. Only use this in a safe environment.",
)
and not "LC_ALL: cannot change locale" in e
]
)

def assertLayersEqual(self, layer_expected, layer_result, **kwargs):
"""
:param layer_expected: The first layer to compare
Expand Down
42 changes: 32 additions & 10 deletions tests/src/python/test_qgsblockingprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,22 @@ def std_err(ba):
self.assertEqual(p.run(f), 0)
self.assertEqual(p.exitStatus(), QProcess.ExitStatus.NormalExit)
self.assertIn("GDAL", std_out.val)
self.assertEqual(std_err.val, "")
self.assertEqual(QgisTestCase.strip_std_ignorable_errors(std_err.val), "")

def test_process_err(self):
temp_folder = tempfile.mkdtemp()

script_file = os.path.join(temp_folder, "stderr_process.sh")
with open(script_file, "w") as f:
f.write(
"""#!/bin/bash
echo "This goes to stdout"
echo "This goes to stderr" >&2
exit 1"""
)

os.chmod(script_file, 0o775)

def std_out(ba):
std_out.val += ba.data().decode("UTF-8")

Expand All @@ -68,15 +81,19 @@ def std_err(ba):

std_err.val = ""

p = QgsBlockingProcess("ogrinfo", [])
p = QgsBlockingProcess("sh", [script_file])
p.setStdOutHandler(std_out)
p.setStdErrHandler(std_err)

f = QgsFeedback()
self.assertEqual(p.run(f), 1)
self.assertEqual(p.exitStatus(), QProcess.ExitStatus.NormalExit)
self.assertIn("Usage", std_out.val)
self.assertIn("FAILURE", std_err.val)
self.assertIn(
"This goes to stdout", QgisTestCase.strip_std_ignorable_errors(std_out.val)
)
self.assertIn(
"This goes to stderr", QgisTestCase.strip_std_ignorable_errors(std_err.val)
)

def test_process_crash(self):
"""
Expand Down Expand Up @@ -163,8 +180,8 @@ def std_err(ba):
f = QgsFeedback()
self.assertEqual(p.run(f), 0)
self.assertEqual(p.exitStatus(), QProcess.ExitStatus.NormalExit)
self.assertFalse(std_out.val.strip())
self.assertFalse(std_err.val.strip())
self.assertFalse(QgisTestCase.strip_std_ignorable_errors(std_out.val).strip())
self.assertFalse(QgisTestCase.strip_std_ignorable_errors(std_err.val).strip())

# set environment variable
os.environ["my_var"] = "my test variable"
Expand All @@ -177,8 +194,11 @@ def std_err(ba):
f = QgsFeedback()
self.assertEqual(p.run(f), 0)
self.assertEqual(p.exitStatus(), QProcess.ExitStatus.NormalExit)
self.assertEqual(std_out.val.strip(), "my test variable")
self.assertFalse(std_err.val.strip())
self.assertEqual(
QgisTestCase.strip_std_ignorable_errors(std_out.val).strip(),
"my test variable",
)
self.assertFalse(QgisTestCase.strip_std_ignorable_errors(std_err.val).strip())

# test python changing path

Expand All @@ -199,8 +219,10 @@ def std_err(ba):
f = QgsFeedback()
self.assertEqual(p.run(f), 0)
self.assertEqual(p.exitStatus(), QProcess.ExitStatus.NormalExit)
self.assertEqual(std_out.val.strip(), new_path)
self.assertFalse(std_err.val.strip())
self.assertEqual(
QgisTestCase.strip_std_ignorable_errors(std_out.val).strip(), new_path
)
self.assertFalse(QgisTestCase.strip_std_ignorable_errors(std_err.val).strip())


if __name__ == "__main__":
Expand Down
52 changes: 16 additions & 36 deletions tests/src/python/test_qgsprocessexecutable_pt1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
import sys
import tempfile

from qgis.testing import unittest

from qgis.testing import QgisTestCase, unittest
from utilities import unitTestDataPath

print("CTEST_FULL_OUTPUT")

TEST_DATA_DIR = unitTestDataPath()


class TestQgsProcessExecutablePt1(unittest.TestCase):
class TestQgsProcessExecutablePt1(QgisTestCase):

TMP_DIR = ""

Expand All @@ -44,25 +43,6 @@ def tearDownClass(cls):
super().tearDownClass()
shutil.rmtree(cls.TMP_DIR, ignore_errors=True)

@staticmethod
def _strip_ignorable_errors(output: str):
return "\n".join(
[
e
for e in output.splitlines()
if e
not in (
"Problem with GRASS installation: GRASS was not found or is not correctly installed",
"QStandardPaths: wrong permissions on runtime directory /tmp, 0777 instead of 0700",
"MESA: error: ZINK: failed to choose pdev",
"MESA: error: ZINK: vkEnumeratePhysicalDevices failed (VK_ERROR_INITIALIZATION_FAILED)",
"glx: failed to create drisw screen",
"failed to load driver: zink",
"QML debugging is enabled. Only use this in a safe environment.",
)
]
)

def run_process(self, arguments):
call = [QGIS_PROCESS_BIN] + arguments
print(" ".join(call))
Expand Down Expand Up @@ -100,7 +80,7 @@ def run_process_stdin(self, arguments, stdin_string: str):
def testNoArgs(self):
rc, output, err = self.run_process([])
self.assertIn("Available commands", output)
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

def testPlugins(self):
Expand All @@ -109,7 +89,7 @@ def testPlugins(self):
self.assertIn("available plugins", output.lower())
self.assertIn("processing", output.lower())
self.assertNotIn("metasearch", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

def testPluginsSkipLoading(self):
Expand All @@ -118,7 +98,7 @@ def testPluginsSkipLoading(self):
self.assertIn("available plugins", output.lower())
self.assertIn("processing", output.lower())
self.assertNotIn("metasearch", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

def testPluginStatus(self):
Expand All @@ -138,12 +118,12 @@ def testPluginStatus(self):
rc, output, err = self.run_process(["plugins"])
self.assertIn("available plugins", output.lower())
self.assertIn("* grassprovider", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

# disable
rc, output, err = self.run_process(["plugins", "disable", "grassprovider"])
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

# try to re-disable
Expand All @@ -154,17 +134,17 @@ def testPluginStatus(self):
rc, output, err = self.run_process(["plugins"])
self.assertIn("available plugins", output.lower())
self.assertNotIn("* grassprovider", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

rc, output, err = self.run_process(["plugins", "enable", "grassprovider"])
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

rc, output, err = self.run_process(["plugins"])
self.assertIn("available plugins", output.lower())
self.assertIn("* grassprovider", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

if not previously_enabled:
Expand Down Expand Up @@ -198,15 +178,15 @@ def testAlgorithmList(self):
self.assertIn("available algorithms", output.lower())
self.assertIn("native:reprojectlayer", output.lower())
self.assertIn("gdal:translate", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

def testAlgorithmListNoPython(self):
rc, output, err = self.run_process(["--no-python", "list"])
self.assertIn("available algorithms", output.lower())
self.assertIn("native:reprojectlayer", output.lower())
self.assertNotIn("gdal:translate", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

def testAlgorithmsListJson(self):
Expand Down Expand Up @@ -240,7 +220,7 @@ def testAlgorithmHelp(self):
rc, output, err = self.run_process(["help", "--no-python", "native:centroids"])
self.assertIn("representing the centroid", output.lower())
self.assertIn("argument type", output.lower())
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertEqual(rc, 0)

def testAlgorithmHelpJson(self):
Expand Down Expand Up @@ -293,7 +273,7 @@ def testAlgorithmRunLegacy(self):
f"--OUTPUT={output_file}",
]
)
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertIn("0...10...20...30...40...50...60...70...80...90", output.lower())
self.assertIn("results", output.lower())
self.assertIn("OUTPUT:\t" + output_file, output)
Expand All @@ -312,7 +292,7 @@ def testAlgorithmRun(self):
f"OUTPUT={output_file}",
]
)
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))
self.assertIn("0...10...20...30...40...50...60...70...80...90", output.lower())
self.assertIn("results", output.lower())
self.assertIn("OUTPUT:\t" + output_file, output)
Expand All @@ -329,7 +309,7 @@ def testAlgorithmRunStdIn(self):
rc, output, err = self.run_process_stdin(
["run", "--no-python", "native:centroids", "-"], json.dumps(params)
)
self.assertFalse(self._strip_ignorable_errors(err))
self.assertFalse(self.strip_std_ignorable_errors(err))

res = json.loads(output)

Expand Down
Loading

0 comments on commit 1f080b9

Please sign in to comment.