Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
MatzeB committed Mar 17, 2020
commit 3b66bf49e1f40850daa3738c272a3ed378db87a6
7 changes: 5 additions & 2 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2206,8 +2206,11 @@ def test_listdir(self):
# 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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write something in the files just to make sure they are created on all platforms?

self.assertEqual(set(os.listdir()), set(os.listdir(tmpdir)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to check that the file you created are in the output?

Comment on lines +2209 to +2213

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a directory with a known content: self.dir.

Suggested change
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)))
os.chdir(self.dir)
self.assertEqual(set(os.listdir()), expected)

finally:
os.chdir(current_directory)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make test_os test_listdir test robust against root directory changing while
the test runs.