diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_execfile.py | 31 | ||||
-rw-r--r-- | test/try_execfile.py | 22 |
2 files changed, 49 insertions, 4 deletions
diff --git a/test/test_execfile.py b/test/test_execfile.py index 90340f57..6ad2f58e 100644 --- a/test/test_execfile.py +++ b/test/test_execfile.py @@ -3,17 +3,42 @@ import cStringIO, os, sys, unittest here = os.path.dirname(__file__) +class Tee(object): + def __init__(self, *files): + self.files = files + + def write(self, data): + for f in self.files: + f.write(data) + class RunTests(unittest.TestCase): def setUp(self): self.oldstdout = sys.stdout - self.stdout = sys.stdout = cStringIO.StringIO() + self.stdout = cStringIO.StringIO() + sys.stdout = Tee(sys.stdout, self.stdout) def tearDown(self): self.stdout = self.oldstdout def test_run_python_file(self): tryfile = os.path.join(here, "try_execfile.py") - run_python_file(tryfile) + run_python_file(tryfile, [tryfile, "arg1", "arg2"]) mod_globs = eval(self.stdout.getvalue()) + + # The file should think it is __main__ self.assertEqual(mod_globs['__name__'], "__main__") - self.assertEqual(os.path.basename(mod_globs['__file__']), "try_execfile.py") + + # It should seem to come from a file named try_execfile + dunder_file = os.path.splitext(os.path.basename(mod_globs['__file__']))[0] + self.assertEqual(dunder_file, "try_execfile") + + # It should have its correct module data. + self.assertEqual(mod_globs['__doc__'], "Test file for run_python_file.") + self.assertEqual(mod_globs['DATA'], "xyzzy") + self.assertEqual(mod_globs['FN_VAL'], "my_fn('fooey')") + + # It must be self-importable as __main__. + self.assertEqual(mod_globs['__main__.DATA'], "xyzzy") + + # Argv should have the proper values. + self.assertEqual(mod_globs['argv'], [tryfile, "arg1", "arg2"]) diff --git a/test/try_execfile.py b/test/try_execfile.py index 523823e4..f66b9c5b 100644 --- a/test/try_execfile.py +++ b/test/try_execfile.py @@ -1,5 +1,25 @@ +"""Test file for run_python_file.""" + +import pprint, sys + +DATA = "xyzzy" + +import __main__ + +def my_function(a): + return "my_fn(%r)" % a + +FN_VAL = my_function("fooey") + globals_to_check = { '__name__': __name__, '__file__': __file__, + '__doc__': __doc__, + 'DATA': DATA, + 'FN_VAL': FN_VAL, + '__main__.DATA': getattr(__main__, "DATA", "nothing"), + 'argv': sys.argv, + 'path0': sys.path[0], } -print repr(globals_to_check) + +pprint.pprint(globals_to_check) |