-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-39986: Make test_listdir from test_os more robust #19035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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.
with tempfile.TemporaryDirectory() as tmpdir: | ||
os.chdir(tmpdir) | ||
open("a.txt", "w").close() | ||
open("test_file.foo", "w").close() |
There was a problem hiding this comment.
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?
os.chdir(tmpdir) | ||
open("a.txt", "w").close() | ||
open("test_file.foo", "w").close() | ||
self.assertEqual(set(os.listdir()), set(os.listdir(tmpdir))) |
There was a problem hiding this comment.
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?
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))) |
There was a problem hiding this comment.
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
.
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) |
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.
https://bugs.python.org/issue39986