classification
Title: Add a context manager to change cwd in test.test_support and run the test suite in a temp dir.
Type: enhancement Stage: resolved
Components: Tests Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: brian.curtin, chris.jerdonek, ezio.melotti, flox, merwok, pitrou
Priority: normal Keywords: patch

Created on 2010-01-16 05:42 by ezio.melotti, last changed 2012-09-19 00:30 by ezio.melotti. 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 (merwok) * (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
2012-09-19 00:30:26ezio.melottisetnosy: + chris.jerdonek
2010-03-14 11:28:20ezio.melottisetstatus: open -> closed
messages: + msg101051

keywords: patch, patch
resolution: accepted
stage: patch review -> resolved
2010-03-06 20:04:45merwoksetnosy: + merwok
messages: + msg100545
2010-02-21 11:25:34ezio.melottiunlinkissue3426 dependencies
2010-02-19 16:28:04floxlinkissue5684 superseder
2010-02-19 16:28:04floxunlinkissue5684 dependencies
2010-02-18 10:05:38ezio.melottisetkeywords: patch, patch

messages: + msg99498
2010-02-13 10:59:24floxsetkeywords: patch, patch

messages: + msg99311
2010-02-12 23:37:16ezio.melottisetkeywords: patch, patch

messages: + msg99295
2010-02-12 15:45:10floxsetkeywords: patch, patch
files: + issue7712_regrtest_rm_hacks.diff
2010-02-12 15:44:11floxsetfiles: - issue7712_regrtest_remove_hacks.diff
2010-02-12 15:39:33floxsetkeywords: patch, patch
files: + issue7712_regrtest_remove_hacks.diff
messages: + msg99269
2010-02-10 00:53:55brian.curtinsetkeywords: patch, patch

messages: + msg99144
2010-02-09 08:08:37floxsetfiles: - issue7712_context_manager_v5.diff
2010-02-09 07:40:11floxsetkeywords: patch, patch

messages: + msg99091
2010-02-09 07:17:46floxsetkeywords: patch, patch

messages: + msg99090
2010-02-09 07:11:02floxsetkeywords: patch, patch

messages: + msg99088
2010-02-09 03:02:18brian.curtinsetkeywords: patch, patch

messages: + msg99083
2010-02-09 00:50:34floxsetkeywords: patch, patch

messages: + msg99080
2010-02-09 00:22:42brian.curtinsetkeywords: patch, patch
files: + test_bufio.stdout.txt
messages: + msg99079
2010-02-08 16:20:10ezio.melottisetkeywords: patch, patch
files: + issue7712v3.diff
messages: + msg99051
2010-02-08 00:47:59ezio.melottisetkeywords: patch, patch
files: + issue7712v2.diff
messages: + msg99034
2010-02-07 14:19:26floxsetkeywords: patch, patch
files: + issue7712_context_manager_v5.diff
messages: + msg99002
2010-02-07 14:18:23floxsetfiles: - issue7712_context_manager_v4.diff
2010-02-07 10:28:07floxsetkeywords: patch, patch
files: + issue7712_context_manager_v4.diff
messages: + msg98988
2010-02-07 10:26:53floxsetfiles: - issue7712_context_manager_v3a.diff
2010-02-06 19:45:19floxsetkeywords: patch, patch
files: + issue7712_context_manager_v3a.diff
messages: + msg98955
2010-02-06 19:44:44floxsetfiles: - issue7712_context_manager_v3.diff
2010-02-06 19:10:02floxsetfiles: - issue7712_context_manager_v2.diff
2010-02-06 19:00:02floxsetkeywords: patch, patch
files: + issue7712_context_manager_v3.diff
messages: + msg98951
2010-02-06 17:54:41floxsetfiles: - issue7712_context_manager.diff
2010-02-06 17:50:29floxsetkeywords: patch, patch
files: + issue7712_context_manager_v2.diff
messages: + msg98949

versions: - Python 2.6, Python 3.1
2010-02-06 10:46:52floxsetkeywords: patch, patch

messages: + msg98939
2010-02-06 04:56:33ezio.melottisetkeywords: patch, patch
files: + issue7712.diff
messages: + msg98936
2010-02-06 00:54:21floxsetkeywords: patch, patch
title: 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:43floxsetfiles: - temp_cwd_decorator_v2.diff
2010-01-19 22:48:30floxsetkeywords: patch, patch
files: + issue7712_context_manager.diff
messages: + msg98072
2010-01-19 15:47:11ezio.melottisetkeywords: patch, patch

messages: + msg98061
2010-01-19 15:42:02pitrousetkeywords: patch, patch

messages: + msg98060
2010-01-19 15:15:00floxsetkeywords: patch, patch
files: + temp_cwd_decorator_v2.diff
messages: + msg98059
2010-01-19 15:12:34floxsetfiles: - temp_cwd_decorator.diff
2010-01-16 21:24:41brian.curtinsetkeywords: patch, patch
nosy: + brian.curtin
2010-01-16 13:04:25floxsetkeywords: - needs review
2010-01-16 11:42:32floxsetfiles: + temp_cwd_decorator.diff

nosy: + pitrou
messages: + msg97871

keywords: patch, patch, needs review
2010-01-16 05:43:38ezio.melottilinkissue3426 dependencies
2010-01-16 05:43:03ezio.melottilinkissue5684 dependencies
2010-01-16 05:42:25ezio.melotticreate