From 3b66bf49e1f40850daa3738c272a3ed378db87a6 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Mon, 16 Mar 2020 19:23:19 -0700 Subject: [PATCH] bpo-39986: Make test_listdir from test_os more robust The test_listdir test of test_os assumed that calling listdir on the root directory twice gives the same results, however this can fail if unrelated process create files in the root directory in between the two calls. This changes the test to create a temporary directory with two files inside and call listdir on this temporary directory instead. --- Lib/test/test_os.py | 7 +++++-- .../next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 9c965444f04..f7408a83759 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2206,8 +2206,11 @@ class Pep383Tests(unittest.TestCase): # test listdir without arguments current_directory = os.getcwd() try: - os.chdir(os.sep) - self.assertEqual(set(os.listdir()), set(os.listdir(os.sep))) + with tempfile.TemporaryDirectory() as tmpdir: + os.chdir(tmpdir) + open("a.txt", "w").close() + open("test_file.foo", "w").close() + self.assertEqual(set(os.listdir()), set(os.listdir(tmpdir))) finally: os.chdir(current_directory) diff --git a/Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst b/Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst new file mode 100644 index 00000000000..cdf04faae80 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst @@ -0,0 +1,2 @@ +Make test_os test_listdir test robust against root directory changing while +the test runs.