Esempio n. 1
0
    def test_run_sysargv(self):
        bmodules = sys.modules
        bargv = sys.argv
        bpath = sys.path
        bget_executable = runner.get_executable
        try:
            sys.modules['__main__'] \
                = MockModule('/path/to/cwd/afile.py', '__main__', '')
            sys.argv = ['afile.py', '...']
            sys.path = [''] + sys.path[1:]
            runner.get_executable = get_executable

            def func(arg=1):
                raise NotImplementedError

            out = cStringIO()
            err = cStringIO()
            runner.run(func, exit=False, out=out, err=err)
            self.assertFalse(out.getvalue())
            self.assertEqual(
                err.getvalue(), "python -m afile: Bad value for arg: '...'\n"
                "Usage: python -m afile [arg]\n")
        finally:
            sys.modules = bmodules
            sys.argv = bargv
            sys.path = bpath
            runner.get_executable = bget_executable
Esempio n. 2
0
 def test_run_sysargv(self):
     bmodules = sys.modules
     bargv = sys.argv
     bpath = sys.path
     bget_executable = runner.get_executable
     try:
         sys.modules = {
             '__main__':
                 MockModule('/path/to/cwd/afile.py', '__main__', '')
             }
         sys.argv = ['afile.py', '...']
         sys.path = ['', sys.path[1]]
         runner.get_executable = get_executable
         def func(arg=1):
             raise NotImplementedError
         out = cStringIO()
         err = cStringIO()
         runner.run(func, exit=False, out=out, err=err)
         self.assertFalse(out.getvalue())
         self.assertEqual(err.getvalue(),
             "python -m afile: Bad value for arg: '...'\n"
             "Usage: python -m afile [arg]\n")
     finally:
         sys.modules = bmodules
         sys.argv = bargv
         sys.path = bpath
         runner.get_executable = bget_executable
Esempio n. 3
0
 def test_run_out(self):
     bout = sys.stdout
     try:
         sys.stdout = out = cStringIO()
         def func():
             return 'hello'
         runner.run(func, args=['test'], exit=False)
         self.assertEqual(out.getvalue(), 'hello\n')
     finally:
         sys.stdout = bout
Esempio n. 4
0
 def test_run_success_exit(self):
     def func():
         return "test_run_success_exit"
     stdout = cStringIO()
     stderr = cStringIO()
     self.assert_systemexit(
         None, runner.run, func, args=['test'], out=stdout, err=stderr)
     self.assertFalse(stderr.getvalue())
     self.assertEqual(stdout.getvalue(), 'test_run_success_exit\n')
     runner.run(func, args=['test'], out=stdout, err=stderr, exit=False)
Esempio n. 5
0
 def test_run_fail_exit(self):
     def func():
         raise errors.ArgumentError("test_run_fail_exit")
     stdout = cStringIO()
     stderr = cStringIO()
     self.assert_systemexit(
         2, runner.run, func, args=['test'], out=stdout, err=stderr)
     self.assertFalse(stdout.getvalue())
     self.assertTrue(stderr.getvalue())
     runner.run(func, args=['test'], out=stdout, err=stderr, exit=False)
Esempio n. 6
0
    def test_run_out(self):
        bout = sys.stdout
        try:
            sys.stdout = out = cStringIO()

            def func():
                return 'hello'

            runner.run(func, args=['test'], exit=False)
            self.assertEqual(out.getvalue(), 'hello\n')
        finally:
            sys.stdout = bout
Esempio n. 7
0
 def test_run_err(self):
     berr = sys.stderr
     try:
         sys.stderr = err = cStringIO()
         def func(arg=1):
             raise NotImplementedError
         runner.run(func, args=['test', '...'], exit=False)
         self.assertEqual(err.getvalue(),
             "test: Bad value for arg: '...'\n"
             "Usage: test [arg]\n")
     finally:
         sys.stderr = berr
Esempio n. 8
0
 def crun(self, func, args, stdin=None, **kwargs):
     orig = sys.stdin, sys.stdout, sys.stderr
     if stdin is None:
         stdin = cStringIO()
     sys.stdin = stdin
     sys.stdout = stdout = cStringIO()
     sys.stderr = stderr = cStringIO()
     try:
         runner.run(func, args=args, exit=False, out=stdout, err=stderr, **kwargs)
         return stdout, stderr
     finally:
         sys.stdin, sys.stdout, sys.stderr = orig
Esempio n. 9
0
    def test_run_err(self):
        berr = sys.stderr
        try:
            sys.stderr = err = cStringIO()

            def func(arg=1):
                raise NotImplementedError

            runner.run(func, args=['test', '...'], exit=False)
            self.assertEqual(
                err.getvalue(), "test: Bad value for arg: '...'\n"
                "Usage: test [arg]\n")
        finally:
            sys.stderr = berr
Esempio n. 10
0
    def test_run_success_exit(self):
        def func():
            return "test_run_success_exit"

        stdout = cStringIO()
        stderr = cStringIO()
        self.assert_systemexit(None,
                               runner.run,
                               func,
                               args=['test'],
                               out=stdout,
                               err=stderr)
        self.assertFalse(stderr.getvalue())
        self.assertEqual(stdout.getvalue(), 'test_run_success_exit\n')
        runner.run(func, args=['test'], out=stdout, err=stderr, exit=False)
Esempio n. 11
0
    def test_run_fail_exit(self):
        def func():
            raise errors.ArgumentError("test_run_fail_exit")

        stdout = cStringIO()
        stderr = cStringIO()
        self.assert_systemexit(2,
                               runner.run,
                               func,
                               args=['test'],
                               out=stdout,
                               err=stderr)
        self.assertFalse(stdout.getvalue())
        self.assertTrue(stderr.getvalue())
        runner.run(func, args=['test'], out=stdout, err=stderr, exit=False)
Esempio n. 12
0
 def test_run_multi(self):
     def func1(): return '1'
     def func2(): return '2'
     stdout, stderr = util.run([func1, func2], args=['test', 'func1'])
     self.assertFalse(stderr.getvalue())
     self.assertEqual(stdout.getvalue(), '1\n')
     stdout, stderr = util.run([func1, func2], args=['test', 'func2'])
     self.assertFalse(stderr.getvalue())
     self.assertEqual(stdout.getvalue(), '2\n')
     stdout = cStringIO()
     stderr = cStringIO()
     runner.run(func1, func2, args=['test', 'func1'],
                out=stdout, err=stderr, exit=False)
     self.assertFalse(stderr.getvalue())
     self.assertEqual(stdout.getvalue(), '1\n')
Esempio n. 13
0
 def crun(self, func, args, stdin=None, **kwargs):
     orig = sys.stdin, sys.stdout, sys.stderr
     if stdin is None:
         stdin = cStringIO()
     sys.stdin = stdin
     sys.stdout = stdout = cStringIO()
     sys.stderr = stderr = cStringIO()
     try:
         runner.run(func,
                    args=args,
                    exit=False,
                    out=stdout,
                    err=stderr,
                    **kwargs)
         return stdout, stderr
     finally:
         sys.stdin, sys.stdout, sys.stderr = orig
Esempio n. 14
0
    def test_run_multi(self):
        def func1():
            return '1'

        def func2():
            return '2'

        stdout, stderr = self.crun([func1, func2], args=['test', 'func1'])
        self.assertFalse(stderr.getvalue())
        self.assertEqual(stdout.getvalue(), '1\n')
        stdout, stderr = self.crun([func1, func2], args=['test', 'func2'])
        self.assertFalse(stderr.getvalue())
        self.assertEqual(stdout.getvalue(), '2\n')
        stdout = cStringIO()
        stderr = cStringIO()
        runner.run(func1,
                   func2,
                   args=['test', 'func1'],
                   out=stdout,
                   err=stderr,
                   exit=False)
        self.assertFalse(stderr.getvalue())
        self.assertEqual(stdout.getvalue(), '1\n')
Esempio n. 15
0
File: util.py Progetto: Rosuav/clize
def run(func, args, **kwargs):
    stdout = cStringIO()
    stderr = cStringIO()
    runner.run(func, args=args, exit=False, out=stdout, err=stderr, **kwargs)
    return stdout, stderr
Esempio n. 16
0
def run(func, args, **kwargs):
    stdout = cStringIO()
    stderr = cStringIO()
    runner.run(func, args=args, exit=False, out=stdout, err=stderr, **kwargs)
    return stdout, stderr