class ModuleUnitTestRunner(SimpleRpcLogicBase): def __post_init__(self): self.twins_manager = TwinModulesManager(self.context) self.file_manager = FileManager(self.context) self.nodejs = NodeJsRunner(self.context) def runTestByNumber(self, package, test_number): tests_dict = self._getTestDict() sorted_names = self.__getSortedNames(tests_dict) new_dict = dict((module.__name__, class_) for module, class_ in tests_dict.items()) tester_class = new_dict[sorted_names[test_number]] self.runPythonTest(tester_class) def __runUnitTest(self, tester_class): tester_class.setTestsContext(self.context) suite = TestLoader().loadTestsFromTestCase(tester_class) TextTestRunner().run(suite) def runPythonTest(self, tested_class): tester_class = self.twins_manager.getTesterFromTested(tested_class) path = self.file_manager.formatClassFilePath(tester_class) name = tester_class.__name__ self.log.d('Running %r test at:\n %s' % (name, path)) #self.module_unit_test_runner.runPythonTest(tester_class) self.__runUnitTest(tester_class) def runJsTest(self, tested_class): file_path = self.twins_manager.getJsUnittest(tested_class) path = self.file_manager.formatFilePath(file_path) self.log.d('Running jasmine test at:\n%s' % path) ret_val = self.nodejs.runJasmineTest(file_path) if ret_val: raise SimpleRpcError('Jasmine test failed')
class ClassToJsUnitTest(SimpleRpcLogicBase): ''' #TODO: document ''' def __post_init__(self): self.class_to_js = ClassToJs(self.context) self.templates = TemplatesCollector(self.context) self.file_manager = FileManager(self.context) def translateClass(self, class_): ''' ''' ast_tree = self.class_to_js.translateClass(class_) templates_set = 'js_unittest_templates' templates = self.templates.collectBuiltIn(templates_set) js_str = ast_tree.getString(templates) return js_str def translateToFile(self, tested_class, file_path, overwrite=False): js_str = self.__getClassJsUnit(tested_class) #create directory just in case test_dir = os.path.dirname(file_path) self.file_manager.makedirs(test_dir) #Say where it is in a pretty way path = self.file_manager.formatFilePath(file_path) name = tested_class.__name__ self.log.d('Creating js for %s test at:\n %s' % (name, path)) #save file self.file_manager.saveTextFile(file_path, js_str, overwrite) def __getClassJsUnit(self, tested_class): js_str = self.translateClass(tested_class) translate = dict(EXPOSED_RPC_API_CLASS=self.context.js_rpc_file) js_str = Template(js_str).safe_substitute(translate) return js_str
class NodeJsRunner(SimpleRpcLogicBase): def __post_init__(self): self.js_util = JsTranslateUtil(self.context) self.file_manager = FileManager(self.context) def setNodePath(self): NODE_PATH = 'NODE_PATH' path = self.js_util._getProjectJsPath() if NODE_PATH not in os.environ: os.environ[NODE_PATH] = path else: os.environ[NODE_PATH] += ':' + path def __printJasmineErrors(self, file_path, ret_val, out, err): if ret_val: path = self.file_manager.formatFilePath(file_path) self.log.w('Error running jasmine test return value %s:\n%s' % (ret_val, path)) self.log.w('Stdout:\n%s' % out) self.log.w('Stderr:\%s' % err) return ret_val def runJasmineTest(self, file_path): argv = [self.context.jasmine_node_command, file_path, '--matchall', '--captureExceptions', '--noColor'] return self.__printJasmineErrors(file_path, *self.__runProcess(argv, pipe=True)) def __runProcess(self, argv, pipe): #we need to know where the .js are self.setNodePath() #run the process! if pipe: p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return p.wait(), p.stdout.read(), p.stderr.read() else: p = subprocess.Popen(argv) return p.wait() def runNodeJs(self, argv=None, pipe=False): #node_command = 'node' if not argv: argv = sys.argv[1:] argv = [self.context.node_command] + argv return self.__runProcess(argv, pipe)