Skip to content

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

Closed

Description

@ezio-melotti
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']

    Activity

    1. ezio-melotti commented on Jan 16, 2010

      @ezio-melotti
      MemberAuthor

      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.

    2. self-assigned this
      on Jan 16, 2010
    3. florentx commented on Jan 16, 2010

      florentxmannequin
      Mannequin

      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('.')
    4. florentx commented on Jan 19, 2010

      florentxmannequin
      Mannequin

      Changed, after review from Ezio and other developpers.

    5. pitrou commented on Jan 19, 2010

      @pitrou
      Member

      with_writable_cwd should probably use functools.wraps.

    6. ezio-melotti commented on Jan 19, 2010

      @ezio-melotti
      MemberAuthor

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

    7. florentx commented on Jan 19, 2010

      florentxmannequin
      Mannequin

      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.

    8. 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
    9. ezio-melotti commented on Feb 6, 2010

      @ezio-melotti
      MemberAuthor

      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.

    10. florentx commented on Feb 6, 2010

      florentxmannequin
      Mannequin

      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
    11. florentx commented on Feb 6, 2010

      florentxmannequin
      Mannequin

      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.

    12. florentx commented on Feb 6, 2010

      florentxmannequin
      Mannequin

      Slightly more readable, without 2-spaces indent.

    13. florentx commented on Feb 6, 2010

      florentxmannequin
      Mannequin

      There were syntax errors in the previous patch. Sorry.

    14. 4 remaining items

    15. briancurtin commented on Feb 9, 2010

      @briancurtin
      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.

    16. florentx commented on Feb 9, 2010

      florentxmannequin
      Mannequin

      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"

    17. briancurtin commented on Feb 9, 2010

      @briancurtin
      Member

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

    18. florentx commented on Feb 9, 2010

      florentxmannequin
      Mannequin

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

      Can you diagnose if it something like bpo-7443?

    19. florentx commented on Feb 9, 2010

      florentxmannequin
      Mannequin

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

    20. florentx commented on Feb 9, 2010

      florentxmannequin
      Mannequin

      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)

    21. briancurtin commented on Feb 10, 2010

      @briancurtin
      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).

    22. florentx commented on Feb 12, 2010

      florentxmannequin
      Mannequin

      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
    23. ezio-melotti commented on Feb 12, 2010

      @ezio-melotti
      MemberAuthor

      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.

    24. florentx commented on Feb 13, 2010

      florentxmannequin
      Mannequin

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

    25. ezio-melotti commented on Feb 18, 2010

      @ezio-melotti
      MemberAuthor

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

    26. merwok commented on Mar 6, 2010

      @merwok
      Member

      Hello

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

      Regards

    27. ezio-melotti commented on Mar 14, 2010

      @ezio-melotti
      MemberAuthor

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

    28. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    testsTests in the Lib/test dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions