Exemple #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
Exemple #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
Exemple #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
Exemple #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)
Exemple #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)
Exemple #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
Exemple #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
Exemple #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
Exemple #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
Exemple #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)
Exemple #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)
Exemple #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')
Exemple #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
Exemple #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')
Exemple #15
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
Exemple #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