Issue 7712: Add a context manager to change cwd in test.test_support and run the test suite in a temp dir. (original) (raw)

Created on 2010-01-16 05:42 by ezio.melotti, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
tempcwd.patch ezio.melotti,2010-01-16 05:42 Patch to add the context manager to test_support
issue7712.diff ezio.melotti,2010-02-06 04:56 Revised patch, still needs some tweaks.
issue7712v2.diff ezio.melotti,2010-02-08 00:47 Patch with context manager and fix for regrtest and a few tests
issue7712v3.diff ezio.melotti,2010-02-08 16:20 Final patch
test_bufio.stdout.txt brian.curtin,2010-02-09 00:22
issue7712_regrtest_rm_hacks.diff flox,2010-02-12 15:45 Patch, apply to trunk
Messages (28)
msg97865 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-01-16 05:42
To simplify the tests that require a different CWD I wrote a context manager that allows to change the working directory. I used it on #3426 to test os.path.abspath() with ASCII and non-ASCII CWDs and realized that it can also be used to fix #5684 where a zipfile fails to extract the content of the archive in the CWD if it is read-only. Patch attached.
msg97871 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-16 11:42
A patch which provides a context manager and a decorator. (with ideas from Ezio and Antoine) Sample usages: with temp_cwd() as cwd: assert cwd == os.getcwd() @writablecwd def test_zipfile(): # do something useful print os.path.abspath('.')
msg98059 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-19 15:14
Changed, after review from Ezio and other developpers.
msg98060 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-01-19 15:42
with_writable_cwd should probably use functools.wraps.
msg98061 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-01-19 15:47
The patch looks good, I'd just move _test_cwd inside the function and drop the [:-3] from TESTFN, but apart from that it's OK. I also agree that functools.wraps should be added. To summarize the discussion we had on #python-dev: 1) the context manager should always create a writable cwd and to be able to run with -J it should contain the process id. Using TESTFN as first-level dir solves both the issues; 2) a suffix is added to TESTFN to let the tests use TESTFN as a valid filename; 3) the second-level dir is 'tempcwd' by default or can be passed to the function in case a test needs a specific name for the cwd; The result will be something like '@test_xxxx_tmp_cwd/dirname'.
msg98072 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-19 22:48
Different approach, after some other talks with Ezio and David. Now the directory is changed before running any test. The developer can assume that the current directory if always writable. It makes the tests easier to write, and repeatable. Additionally, TESTFN always contains a single filename (without path). Before this change, it could be "/tmp/@test" or "@test", which is error-prone. The decorator becomes useless.
msg98936 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-06 04:56
Here is a patch based on the previous patches and some discussion. I still have to test it better and add a few comments, but it should be almost ok.
msg98939 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-06 10:46
It looks fine. Few comments: - "{}_python_{}" could be better, to identify the culprit for leftover directories. - the warning message may be more specific: "warnings.warn('tests may fail, unable to switch to ' + name, RuntimeWarning, stacklevel=3)" - style guide recommends two spaces after a sentence-ending period
msg98949 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-06 17:50
Patch which fixes the "relative import" issue. ~ $ cd Lib/ ~ $ ../python -m test.regrtest Note: There are some 2-spaces indents for patch readability. Change them before commit.
msg98951 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-06 19:00
Slightly more readable, without 2-spaces indent.
msg98955 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-06 19:45
There were syntax errors in the previous patch. Sorry.
msg98988 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-07 10:28
Removed the dummy CM hack. Now it should be ready for final review.
msg99002 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-07 14:19
Let's fix some other tests.
msg99034 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-08 00:47
Almost completed patch, the code should be OK, I just have to add a few comments and check that it works fine in all the situations.
msg99051 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-08 16:20
Final version of the patch, with the temp_cwd context manager added to test_support and used in test_regrtest to run the test suite in a temporary directory. It also includes a fix for test_subprocess that was failing when the tests are not run in the original cwd.
msg99079 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-09 00:22
With the latest patch I get one failure: test_bufio. I piped that test to a file and attached the results here. There are numerous "Permission denied: @test" IOErrors.
msg99080 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-09 00:50
Brian, is it the only one failing? Did you have some test running before? Which one? (alphabetic order?) Do you reproduce the error when running this test alone? "-m test.regrtest -uall test_bufio"
msg99083 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-09 03:02
Yep, that's the only one failing. The output I attached is the result of running the test alone.
msg99088 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-09 07:11
Ok, it may be not related directly with this patch. Can you diagnose if it something like #7443?
msg99090 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-09 07:17
And you could try the patch attached to #7443, and see if it fixes the test_bufio issue.
msg99091 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-09 07:40
The command line used to run the tests is important. If you run through "test.regrtest", it should "chdir" to a sandbox directory to run the test. If you call directly the test_bufio.py file, it runs in the current directory (as before the patch). And the permission of the current directory may give a difference in the 2nd case. Variants: python.exe -m test.regrtest test_bufio python.exe Lib/test/regrtest.py test_bufio python.exe Lib/test/test_bufio.py (fail if home directory is RO) And another full run: python.exe -m test.regrtest Try some of these variants, it may help diagnose the windows issue. (Note: maybe some antivirus or other software may be related to the race condition described on #7443)
msg99144 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-10 00:53
With Ezio's latest patch (sent via IRC), test_bufio still fails and additionally test_mailbox fails. If I apply the patch on #7443 along with Ezio's patch, everything looks fine. I haven't thoroughly looked at that issue, but on the surface it looks similar (same setup, same result).
msg99269 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-12 15:39
Fixed on trunk with r78136. Before closing this issue, we may apply additional cleanup on regrtest: - the "sys.path" hack is not needed anymore (no risk of relative imports) - the hack for sys.argv[0] could be removed too, and use __file__ instead
msg99295 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-12 23:37
I'm not sure it's safe to remove those hacks, they might be necessary in some corner case. I'll also port this to py3k before closing the issue.
msg99311 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-13 10:59
"Complex is better than complicated... Special cases aren't special enough to break the rules." The module "regrtest" is complex enough. We don't need to keep useless hacks inside. It would be more interesting to replace the hack with some words, or an assertion, and understand the real (or hypothetic) issue behind this hack. Eventually, if we keep these hacks around, it may hide real bugs.
msg99498 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-18 10:05
Ported to py3k in r78214. I will think about the cleanups later, they are not so important right now.
msg100545 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-03-06 20:04
Hello One advice against using __file__: http://lists.debian.org/debian-python/2010/01/msg00172.html Regards
msg101051 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-03-14 11:28
The cleanups have been committed in r78719 (trunk) and r78723 (py3k).
History
Date User Action Args
2022-04-11 14:56:56 admin set github: 51961
2012-09-19 00:30:26 ezio.melotti set nosy: + chris.jerdonek
2010-03-14 11:28:20 ezio.melotti set status: open -> closedmessages: + keywords:patch, patchresolution: acceptedstage: patch review -> resolved
2010-03-06 20:04:45 eric.araujo set nosy: + eric.araujomessages: +
2010-02-21 11:25:34 ezio.melotti unlink issue3426 dependencies
2010-02-19 16:28:04 flox link issue5684 superseder
2010-02-19 16:28:04 flox unlink issue5684 dependencies
2010-02-18 10:05:38 ezio.melotti set keywords:patch, patchmessages: +
2010-02-13 10:59:24 flox set keywords:patch, patchmessages: +
2010-02-12 23:37:16 ezio.melotti set keywords:patch, patchmessages: +
2010-02-12 15:45:10 flox set keywords:patch, patchfiles: + issue7712_regrtest_rm_hacks.diff
2010-02-12 15:44:11 flox set files: - issue7712_regrtest_remove_hacks.diff
2010-02-12 15:39:33 flox set keywords:patch, patchfiles: + issue7712_regrtest_remove_hacks.diffmessages: +
2010-02-10 00:53:55 brian.curtin set keywords:patch, patchmessages: +
2010-02-09 08:08:37 flox set files: - issue7712_context_manager_v5.diff
2010-02-09 07:40:11 flox set keywords:patch, patchmessages: +
2010-02-09 07:17:46 flox set keywords:patch, patchmessages: +
2010-02-09 07:11:02 flox set keywords:patch, patchmessages: +
2010-02-09 03:02:18 brian.curtin set keywords:patch, patchmessages: +
2010-02-09 00:50:34 flox set keywords:patch, patchmessages: +
2010-02-09 00:22:42 brian.curtin set keywords:patch, patchfiles: + test_bufio.stdout.txtmessages: +
2010-02-08 16:20:10 ezio.melotti set keywords:patch, patchfiles: + issue7712v3.diffmessages: +
2010-02-08 00:47:59 ezio.melotti set keywords:patch, patchfiles: + issue7712v2.diffmessages: +
2010-02-07 14:19:26 flox set keywords:patch, patchfiles: + issue7712_context_manager_v5.diffmessages: +
2010-02-07 14🔞23 flox set files: - issue7712_context_manager_v4.diff
2010-02-07 10:28:07 flox set keywords:patch, patchfiles: + issue7712_context_manager_v4.diffmessages: +
2010-02-07 10:26:53 flox set files: - issue7712_context_manager_v3a.diff
2010-02-06 19:45:19 flox set keywords:patch, patchfiles: + issue7712_context_manager_v3a.diffmessages: +
2010-02-06 19:44:44 flox set files: - issue7712_context_manager_v3.diff
2010-02-06 19:10:02 flox set files: - issue7712_context_manager_v2.diff
2010-02-06 19:00:02 flox set keywords:patch, patchfiles: + issue7712_context_manager_v3.diffmessages: +
2010-02-06 17:54:41 flox set files: - issue7712_context_manager.diff
2010-02-06 17:50:29 flox set keywords:patch, patchfiles: + issue7712_context_manager_v2.diffmessages: + versions: - Python 2.6, Python 3.1
2010-02-06 10:46:52 flox set keywords:patch, patchmessages: +
2010-02-06 04:56:33 ezio.melotti set keywords:patch, patchfiles: + issue7712.diffmessages: +
2010-02-06 00:54:21 flox set keywords:patch, patchtitle: Add a context manager to change cwd in test.test_support -> Add a context manager to change cwd in test.test_support and run the test suite in a temp dir.
2010-02-06 00:52:43 flox set files: - temp_cwd_decorator_v2.diff
2010-01-19 22:48:30 flox set keywords:patch, patchfiles: + issue7712_context_manager.diffmessages: +
2010-01-19 15:47:11 ezio.melotti set keywords:patch, patchmessages: +
2010-01-19 15:42:02 pitrou set keywords:patch, patchmessages: +
2010-01-19 15:15:00 flox set keywords:patch, patchfiles: + temp_cwd_decorator_v2.diffmessages: +
2010-01-19 15:12:34 flox set files: - temp_cwd_decorator.diff
2010-01-16 21:24:41 brian.curtin set keywords:patch, patchnosy: + brian.curtin
2010-01-16 13:04:25 flox set keywords: - needs review
2010-01-16 11:42:32 flox set files: + temp_cwd_decorator.diffnosy: + pitroumessages: + keywords:patch, patch, needs review
2010-01-16 05:43:38 ezio.melotti link issue3426 dependencies
2010-01-16 05:43:03 ezio.melotti link issue5684 dependencies
2010-01-16 05:42:25 ezio.melotti create