Skip to content
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

Add a context manager to change cwd in test.test_support and run the test suite in a temp dir. #51961

Closed
ezio-melotti opened this issue Jan 16, 2010 · 28 comments
Assignees
Labels
tests Tests in the Lib/test dir type-feature A feature request or enhancement

Comments

@ezio-melotti
Copy link
Member

BPO 7712
Nosy @pitrou, @ezio-melotti, @merwok, @briancurtin, @florentx, @cjerdonek
Files
  • tempcwd.patch: Patch to add the context manager to test_support
  • issue7712.diff: Revised patch, still needs some tweaks.
  • issue7712v2.diff: Patch with context manager and fix for regrtest and a few tests
  • issue7712v3.diff: Final patch
  • test_bufio.stdout.txt
  • issue7712_regrtest_rm_hacks.diff: Patch, apply to trunk
  • 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 = <Date 2010-03-14.11:28:20.836>
    created_at = <Date 2010-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 = <Date 2012-09-19.00:30:26.412>
    user = 'https://github.com/ezio-melotti'

    bugs.python.org fields:

    activity = <Date 2012-09-19.00:30:26.412>
    actor = 'ezio.melotti'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2010-03-14.11:28:20.836>
    closer = 'ezio.melotti'
    components = ['Tests']
    creation = <Date 2010-01-16.05:42:25.267>
    creator = 'ezio.melotti'
    dependencies = []
    files = ['15904', '16146', '16175', '16179', '16181', '16219']
    hgrepos = []
    issue_num = 7712
    keywords = ['patch']
    message_count = 28.0
    messages = ['97865', '97871', '98059', '98060', '98061', '98072', '98936', '98939', '98949', '98951', '98955', '98988', '99002', '99034', '99051', '99079', '99080', '99083', '99088', '99090', '99091', '99144', '99269', '99295', '99311', '99498', '100545', '101051']
    nosy_count = 6.0
    nosy_names = ['pitrou', 'ezio.melotti', 'eric.araujo', 'brian.curtin', 'flox', 'chris.jerdonek']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue7712'
    versions = ['Python 2.7', 'Python 3.2']

    @ezio-melotti
    Copy link
    Member Author

    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.

    @ezio-melotti ezio-melotti self-assigned this Jan 16, 2010
    @ezio-melotti ezio-melotti added tests Tests in the Lib/test dir type-feature A feature request or enhancement labels Jan 16, 2010
    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Jan 16, 2010

    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('.')

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Jan 19, 2010

    Changed, after review from Ezio and other developpers.

    @pitrou
    Copy link
    Member

    pitrou commented Jan 19, 2010

    with_writable_cwd should probably use functools.wraps.

    @ezio-melotti
    Copy link
    Member Author

    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'.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented 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.

    @florentx florentx mannequin 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. Feb 6, 2010
    @ezio-melotti
    Copy link
    Member Author

    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
    Copy link
    Mannequin

    florentx mannequin commented Feb 6, 2010

    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

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented 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
    Copy link
    Mannequin

    florentx mannequin commented Feb 6, 2010

    Slightly more readable, without 2-spaces indent.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Feb 6, 2010

    There were syntax errors in the previous patch. Sorry.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Feb 7, 2010

    Removed the dummy CM hack.
    Now it should be ready for final review.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Feb 7, 2010

    Let's fix some other tests.

    @ezio-melotti
    Copy link
    Member Author

    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.

    @ezio-melotti
    Copy link
    Member Author

    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.

    @briancurtin
    Copy link
    Member

    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
    Copy link
    Mannequin

    florentx mannequin commented 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
    Copy link
    Member

    Yep, that's the only one failing. The output I attached is the result of running the test alone.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Feb 9, 2010

    Ok, it may be not related directly with this patch.

    Can you diagnose if it something like bpo-7443?

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Feb 9, 2010

    And you could try the patch attached to bpo-7443, and see if it fixes the test_bufio issue.

    @florentx
    Copy link
    Mannequin

    florentx mannequin commented 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
    Copy link
    Member

    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
    Copy link
    Mannequin

    florentx mannequin commented Feb 12, 2010

    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

    @ezio-melotti
    Copy link
    Member Author

    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
    Copy link
    Mannequin

    florentx mannequin commented 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
    Copy link
    Member Author

    Ported to py3k in r78214. I will think about the cleanups later, they are not so important right now.

    @merwok
    Copy link
    Member

    merwok commented Mar 6, 2010

    Hello

    One advice against using __file__: http://lists.debian.org/debian-python/2010/01/msg00172.html

    Regards

    @ezio-melotti
    Copy link
    Member Author

    The cleanups have been committed in r78719 (trunk) and r78723 (py3k).

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    tests Tests in the Lib/test dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants