From 3173a26388b7724adeb2c5f44325bbbfbad77d10 Mon Sep 17 00:00:00 2001
From: Daniel Hahler <git@thequod.de>
Date: Sat, 19 Oct 2019 04:44:22 +0200
Subject: [PATCH] cache: NFPlugin: keep known nodeids

Caveat: does not forget about old nodeids

Fixes https://github.com/pytest-dev/pytest/issues/5206
---
 changelog/5206.bugfix.rst     | 1 +
 src/_pytest/cacheprovider.py  | 8 ++++++--
 testing/test_cacheprovider.py | 7 ++++++-
 3 files changed, 13 insertions(+), 3 deletions(-)
 create mode 100644 changelog/5206.bugfix.rst

diff --git a/changelog/5206.bugfix.rst b/changelog/5206.bugfix.rst
new file mode 100644
index 000000000..94df9c6b4
--- /dev/null
+++ b/changelog/5206.bugfix.rst
@@ -0,0 +1 @@
+Fix ``--nf`` to not forget about known nodeids with partial test selection.
diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py
index 496931e0f..34186c845 100755
--- a/src/_pytest/cacheprovider.py
+++ b/src/_pytest/cacheprovider.py
@@ -264,8 +264,8 @@ class NFPlugin:
         self.cached_nodeids = config.cache.get("cache/nodeids", [])
 
     def pytest_collection_modifyitems(self, session, config, items):
+        new_items = OrderedDict()
         if self.active:
-            new_items = OrderedDict()
             other_items = OrderedDict()
             for item in items:
                 if item.nodeid not in self.cached_nodeids:
@@ -276,7 +276,11 @@ class NFPlugin:
             items[:] = self._get_increasing_order(
                 new_items.values()
             ) + self._get_increasing_order(other_items.values())
-        self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)]
+        else:
+            for item in items:
+                if item.nodeid not in self.cached_nodeids:
+                    new_items[item.nodeid] = item
+        self.cached_nodeids.extend(new_items)
 
     def _get_increasing_order(self, items):
         return sorted(items, key=lambda item: item.fspath.mtime(), reverse=True)
diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py
index cbba27e5f..28d5bceaf 100644
--- a/testing/test_cacheprovider.py
+++ b/testing/test_cacheprovider.py
@@ -982,8 +982,13 @@ class TestNewFirst:
         )
         testdir.tmpdir.join("test_1/test_1.py").setmtime(1)
 
-        result = testdir.runpytest("-v", "--nf")
+        # Running only a subset does not forget about existing ones.
+        result = testdir.runpytest("-v", "--nf", "test_2/test_2.py")
+        result.stdout.fnmatch_lines(
+            ["*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*"]
+        )
 
+        result = testdir.runpytest("-v", "--nf")
         result.stdout.fnmatch_lines(
             [
                 "*test_1/test_1.py::test_1[3*",