You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a context manager to change cwd in test.test_support and run the test suite in a temp dir. #51961
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/ezio-melotti'closed_at=<Date2010-03-14.11:28:20.836>created_at=<Date2010-01-16.05:42:25.267>labels= ['type-feature', 'tests']
title='Add a context manager to change cwd in test.test_support and run the test suite in a temp dir.'updated_at=<Date2012-09-19.00:30:26.412>user='https://github.com/ezio-melotti'
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 bpo-3426 to test os.path.abspath() with ASCII and non-ASCII CWDs and realized that it can also be used to fix bpo-5684 where a zipfile fails to extract the content of the archive in the CWD if it is read-only.
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:
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;
a suffix is added to TESTFN to let the tests use TESTFN as a valid filename;
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'.
florentx commented on Jan 19, 2010
florentxmannequin
Mannequin
More actions
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.
changed the 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.[/+]on Feb 6, 2010
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.
florentx commented on Feb 9, 2010
florentxmannequin
Mannequin
More actions
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"
And you could try the patch attached to bpo-7443, and see if it fixes the test_bufio issue.
florentx commented on Feb 9, 2010
florentxmannequin
Mannequin
More actions
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.
With Ezio's latest patch (sent via IRC), test_bufio still fails and additionally test_mailbox fails.
If I apply the patch on bpo-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).
florentx commented on Feb 12, 2010
florentxmannequin
Mannequin
More actions
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
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.
florentx commented on Feb 13, 2010
florentxmannequin
Mannequin
More actions
"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.
Activity
ezio-melotti commentedon Jan 16, 2010
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 bpo-3426 to test os.path.abspath() with ASCII and non-ASCII CWDs and realized that it can also be used to fix bpo-5684 where a zipfile fails to extract the content of the archive in the CWD if it is read-only.
Patch attached.
florentx commentedon Jan 16, 2010
A patch which provides a context manager and a decorator.
(with ideas from Ezio and Antoine)
Sample usages:
florentx commentedon Jan 19, 2010
Changed, after review from Ezio and other developpers.
pitrou commentedon Jan 19, 2010
with_writable_cwd should probably use functools.wraps.
ezio-melotti commentedon Jan 19, 2010
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:
The result will be something like '@test_xxxx_tmp_cwd/dirname'.
florentx commentedon Jan 19, 2010
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.
[-]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.[/+]ezio-melotti commentedon Feb 6, 2010
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.
florentx commentedon Feb 6, 2010
It looks fine.
Few comments:
"warnings.warn('tests may fail, unable to switch to ' + name,
RuntimeWarning, stacklevel=3)"
florentx commentedon Feb 6, 2010
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.
florentx commentedon Feb 6, 2010
Slightly more readable, without 2-spaces indent.
florentx commentedon Feb 6, 2010
There were syntax errors in the previous patch. Sorry.
4 remaining items
briancurtin commentedon Feb 9, 2010
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.
florentx commentedon Feb 9, 2010
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"
briancurtin commentedon Feb 9, 2010
Yep, that's the only one failing. The output I attached is the result of running the test alone.
florentx commentedon Feb 9, 2010
Ok, it may be not related directly with this patch.
Can you diagnose if it something like bpo-7443?
florentx commentedon Feb 9, 2010
And you could try the patch attached to bpo-7443, and see if it fixes the test_bufio issue.
florentx commentedon Feb 9, 2010
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 bpo-7443)
briancurtin commentedon Feb 10, 2010
With Ezio's latest patch (sent via IRC), test_bufio still fails and additionally test_mailbox fails.
If I apply the patch on bpo-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).
florentx commentedon Feb 12, 2010
Fixed on trunk with r78136.
Before closing this issue, we may apply additional cleanup on regrtest:
ezio-melotti commentedon Feb 12, 2010
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.
florentx commentedon Feb 13, 2010
"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.
ezio-melotti commentedon Feb 18, 2010
Ported to py3k in r78214. I will think about the cleanups later, they are not so important right now.
merwok commentedon Mar 6, 2010
Hello
One advice against using __file__: http://lists.debian.org/debian-python/2010/01/msg00172.html
Regards
ezio-melotti commentedon Mar 14, 2010
The cleanups have been committed in r78719 (trunk) and r78723 (py3k).