From d829e9caac428405d1a76976a8df95a31867c698 Mon Sep 17 00:00:00 2001
From: Kirill Smelkov <kirr@nexedi.com>
Date: Sun, 8 Nov 2020 12:33:02 +0300
Subject: [PATCH] PyTest: summary: Skip trail text after test summary

For example wendelin.core 2 uses pytest_unconfigure to unmount wcfs
servers that it spawned:

https://lab.nexedi.com/kirr/wendelin.core/blob/1be4d730/conftest.py#L27-53

This usually leads to some test as shown in added test.

-> Skip that text backwards and actually detect pytest summary line
instead of returning empty dict, because empty dict forces Nexedi ERP5
to treat such test run to have UNKNOWN status, e.g.

https://erp5.nexedi.net/test_result_module/20201108-C170850/11

/cc @jerome
/reviewed-on https://lab.nexedi.com/nexedi/nxdtest/merge_requests/5
---
 nxdtest/__init__.py            | 13 ++++++++++---
 nxdtest/nxdtest_pytest_test.py | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/nxdtest/__init__.py b/nxdtest/__init__.py
index 7c96893..1656649 100755
--- a/nxdtest/__init__.py
+++ b/nxdtest/__init__.py
@@ -351,12 +351,19 @@ class LocalTestResultLine:
 class PyTest:
     @staticmethod
     def summary(out): # -> status_dict
-        # last line is like
+        # end of output is like
         # ================ 1 failed, 1 passed, 12 skipped in 0.39 seconds ================
+        # ...
         textv = out.splitlines()
-        tail = textv[-1]
+        for l in reversed(textv):
+            if re.match(b'^====* .* ====*$', l):
+                pytail = l
+                break
+        else:
+            return {}
+
         def get(name, default=None):
-            m = re.search(r'\b([0-9]+) '+name+r'\b', tail)
+            m = re.search(r'\b([0-9]+) '+name+r'\b', pytail)
             if m is None:
                 return default
             return int(m.group(1))
diff --git a/nxdtest/nxdtest_pytest_test.py b/nxdtest/nxdtest_pytest_test.py
index e7380be..338921d 100644
--- a/nxdtest/nxdtest_pytest_test.py
+++ b/nxdtest/nxdtest_pytest_test.py
@@ -114,6 +114,24 @@ golang/time_test.py:106: AssertionError
 """,
 '?\ttestname\t1.000s\t# 112t ?e 1f 13s')
 
+case1('ok+tailtext', """\
+date:   Sun, 08 Nov 2020 12:26:24 MSK
+xnode:  kirr@deco.navytux.spb.ru
+uname:  Linux deco 5.9.0-1-amd64 #1 SMP Debian 5.9.1-1 (2020-10-17) x86_64
+cpu:    Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
+
+>>> test.py/fs-wcfs:
+$ make test.py # GOMAXPROCS= WENDELIN_CORE_TEST_DB=<fs> WENDELIN_CORE_VIRTMEM=r:wcfs+w:uvmm
+...
+==================== 54 passed, 1 xpassed in 13.47 seconds =====================
+# unmount/stop wcfs pid39670 @ /tmp/wcfs/cff0a836e51839ee7b10ba76277c639fe11bdb11
+wcfs: 2020/11/08 12:26:38 /tmp/testdb_fs.Z9IvT0/1.fs: watcher: stat /tmp/testdb_fs.Z9IvT0/1.fs: use of closed file
+# unmount/stop wcfs pid39653 @ /tmp/wcfs/40cc7154ed758d6a867205e79e320c1d3b56458d
+wcfs: 2020/11/08 12:26:38 /tmp/testdb_fs.B3rbby/1.fs: watcher: stat /tmp/testdb_fs.B3rbby/1.fs: use of closed file
+# unmount/stop wcfs pid39595 @ /tmp/wcfs/d0b5d036a2cce47fe73003cf2d9f0b22c7043817
+""",
+'?\ttestname\t1.000s\t# 55t ?e ?f ?s')
+
 
 @pytest.mark.parametrize("name,textout,summaryok", testv)
 def test_pytest_summary(name,textout, summaryok):
-- 
2.30.9