File: checkpwd.py

package info (click to toggle)
datalad 1.1.5-2.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,140 kB
  • sloc: python: 69,392; sh: 1,521; makefile: 220
file content (95 lines) | stat: -rwxr-xr-x 2,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
"""
Check which test did not come back to initial directory and fail right away
when detected.

Could be used as easy as replacement of `python -m nose ...`, with
`python checkpwd.py --with-checkpwd ...`
"""

import logging
import os
from os import path as op

from nose.plugins.base import Plugin
from nose.util import (
    src,
    tolist,
)

log = logging.getLogger(__name__)


def getpwd():
    """Try to return a CWD without dereferencing possible symlinks

    If no PWD found in the env, output of getcwd() is returned
    """
    cwd = os.getcwd()
    try:
        env_pwd = os.environ['PWD']
        from datalad.utils import Path
        if Path(env_pwd).resolve() != Path(cwd).resolve():
            # uses os.chdir directly, pwd is not updated
            # could be an option to fail (tp not allow direct chdir)
            return cwd
        return env_pwd
    except KeyError:
        return cwd


class CheckPWD(Plugin):
    """
    Activate a coverage report using Ned Batchelder's coverage module.
    """
    name = 'checkpwd'

    def options(self, parser, env):
        """
        Add options to command line.
        """
        # throw_exception = True
        super(CheckPWD, self).options(parser, env)

    def configure(self, options, conf):
        """
        Configure plugin.
        """
        super(CheckPWD, self).configure(options, conf)
        self._pwd = getpwd()
        print("Initial PWD: %s" % self._pwd)

    def beforeTest(self, *args, **kwargs):
        """
        Begin recording coverage information.
        """
        assert getpwd() == self._pwd

    def afterTest(self, *args, **kwargs):
        """
        Stop recording coverage information.
        """
        pwd = getpwd()
        # print("Checking %s" % pwd)
        print("PWD: %s" % pwd)
        assert pwd == self._pwd, \
            "PWD original:%s  current: %s (after %s)" \
            % (self._pwd, pwd, args[0])


def test_ok():
    pass


def test_fail():
    os.chdir('/dev')


def test_fail_not_again():
    # will never reach here if test_fail fails
    pass


if __name__ == '__main__':
    import nose
    nose.main(addplugins=[CheckPWD()])