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.
This commit is contained in:
Matthias Braun 2020-03-16 19:23:19 -07:00
parent 2037502613
commit 3b66bf49e1
2 changed files with 7 additions and 2 deletions

View File

@ -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)

View File

@ -0,0 +1,2 @@
Make test_os test_listdir test robust against root directory changing while
the test runs.