1. pendletongp
  2. pyjaco

Commits

pendletongp  committed 830e9f7

add simple import tests for demonstration

  • Participants
  • Parent commits 493c767
  • Branches devel

Comments (0)

Files changed (7)

File tests/import/basic/basic.py

View file
+import foo
+foo.Foo()

File tests/import/basic/foo.py

View file
+def Foo():
+    print "Foo"

File tests/import/nested/foo/__init__.py

View file
+import bar
+def Foo():
+    return "Foo and " + bar.Bar()

File tests/import/nested/foo/bar/__init__.py

View file
+def Bar():
+    return "Bar"

File tests/import/nested/nested.py

View file
+import foo
+s = foo.Foo()
+
+import foo.bar
+print s == foo.Foo()
+print foo.bar.Bar()
+
+import foo
+print foo.bar.Bar()
+print s == foo.Foo()

File testtools/tests.py

View file
                     )
                 )
             )
+    
+    test_paths = glob.glob("tests/import/*/")
+    temp = []
+    for path in test_paths:
+        path = path.replace('\\', '/').strip('/')
+        path = "%s/%s.py" % (path, os.path.basename(path))
+        if os.path.exists(path):
+            temp.append(path)
+    test_paths = temp
+    test_paths.sort()
+    for test_path in test_paths:
+        test_cases.addTest(
+            unittest.TestLoader().loadTestsFromTestCase(
+                util.compile_as_module_and_run_file_test(
+                    test_path, 
+                    os.path.basename(test_path),
+                    uses_imports = True
+                    )
+                )
+            )
         
     return test_cases , failing_test_cases
 

File testtools/util.py

View file
 
     return FailingTest
 
-def compile_as_module_and_run_file_test(file_path, file_name=None):
+def compile_as_module_and_run_file_test(file_path, file_name=None, uses_imports = False):
     """Creates a test that compiles as a module and runs the python file as js"""
     file_name = file_name if file_name else file_path
 
                 '"%(compiler_error)s"'
                 ) % self.templ 
             
+            templ = self.templ.copy()
+            import_commands = []
+            templ["import_js"] = ""
+            
+            if uses_imports:
+                dirpath = os.path.dirname(file_path)
+                for root, dirs, files in os.walk(dirpath):
+                    for fn in [x for x in files if x.endswith('.py')]:
+                        py_path = os.path.join(root, fn).replace("\\","/")
+                        if py_path == self.templ["py_path"].replace("\\","/"):
+                            continue
+                        js_path = py_path + ".js"
+                        cmd = (
+                            '%(py_executable)s pyjs.py -q -m '
+                            '"%(py_path)s" > "%(js_path)s"'
+                        ) % {
+                             "py_executable": self.templ["py_executable"],
+                             "py_path": py_path,
+                             "js_path": js_path
+                        }
+                        import_commands.append(cmd)
+                        templ["import_js"] = '%s -f "%s"' % (templ["import_js"], js_path)
+                        
+            
             javascript_command = (
-                'js -f "%(js_path)s" -f "%(js_run_file)s" > "%(js_out_path)s" 2> '
+                'js -f "%(js_path)s" %(import_js)s -f "%(js_run_file)s" > "%(js_out_path)s" 2> '
                 '"%(js_error)s"' 
-                ) % self.templ
-
+                ) % templ
+                
             # create javascript run file
             with open(self.templ['js_run_file'], 'w') as f:
                 dotted = os.path.splitext(self.templ['py_path'])[0].replace("\\","/").replace("/",".")
                 commands.append(python_command)
             if mtime_js_res < mtime_src:
                 commands.append(compile_command)
+            commands.extend(import_commands)
             commands.append(javascript_command)
 
             for cmd in commands: