Skip to content

bpo-39986: Make test_listdir from test_os more robust#19035

Open
MatzeB wants to merge 1 commit intopython:mainfrom
MatzeB:fix-issue-39986
Open

bpo-39986: Make test_listdir from test_os more robust#19035
MatzeB wants to merge 1 commit intopython:mainfrom
MatzeB:fix-issue-39986

Conversation

@MatzeB
Copy link
Copy Markdown
Contributor

@MatzeB MatzeB commented Mar 17, 2020

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

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.
Comment thread Lib/test/test_os.py
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?

Comment thread Lib/test/test_os.py
os.chdir(tmpdir)
open("a.txt", "w").close()
open("test_file.foo", "w").close()
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 thread Lib/test/test_os.py
Comment on lines +2209 to +2213
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)))
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)

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes and removed needs backport to 3.11 only security fixes labels May 9, 2024
@hugovk hugovk removed the needs backport to 3.12 only security fixes label Apr 10, 2025
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 7, 2026

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news stale Stale PR or inactive for long period of time. tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants