예제 #1
0
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 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')
예제 #3
0
class TemplatesCollector(SimpleRpcLogicBase):
    '''
    Collects templates into stores in the repository
    to be used in the translation by the TranslationAstNode class.
    '''
    def __post_init__(self):
        self.file_manager = FileManager(self.context)

    def _getRepoPath(self, templates_set):
        return joinPath(os.path.dirname(__file__), templates_set)

    def _getTemplatesPaths(self, pattern, templates_set):
        for root, _, files in os.walk(self._getRepoPath(templates_set),
                                      followlinks=True):
            for basename in files:
                if fnmatch.fnmatch(basename, pattern):
                    filename = os.path.join(root, basename)
                    yield filename

    def _buildNamespace(self, file_path, templates_set):
        repo_split = splitPath(self._getRepoPath(templates_set))
        namespace, _ = os.path.splitext(file_path)
        namespace = splitPath(namespace)[len(repo_split):]
        return '.'.join(namespace)

    def collectBuiltIn(self, templates_set='javascript_templates'):
        templates = dict()
        for file_path in self._getTemplatesPaths('*.js', templates_set):
            namespace = self._buildNamespace(file_path, templates_set)
            template = self.file_manager.getTextFile(file_path)
            templates[namespace] = template
        return templates
예제 #4
0
class ExposedTestBase(SimpleRpcLogicBase):
    tests_context = None

    @staticmethod
    def setTestsContext(context):
        ExposedTestBase.tests_context = context

    def __init__(self, first_arg=None):
        #Initialize the class if inheriting from TestCase
        if isinstance(self, TestCase): #If using unittest, then initialize the class
            #Keep signature
            if isinstance(first_arg, str):
                methodName = first_arg
                first_arg = None
                TestCase.__init__(self, methodName)
            else:
                TestCase.__init__(self)

        context = first_arg
        #Solve context if not provided
        if context == None:
            if ExposedTestBase.tests_context == None: #To enable context free initialization supporting unittest.TestCase
                self.__runs_from_tested_module = False
                ExposedTestBase.tests_context = SimpleRpcContext(self.__class__.__name__)
            else:
                self.__runs_from_tested_module = True
            context = ExposedTestBase.tests_context
        SimpleRpcLogicBase.__init__(self, context)

    def __post_init__(self):
        self.twins_manager = TwinModulesManager(self.context)
        self.module_unit_test_runner = ModuleUnitTestRunner(self.context)
        self.file_manager = FileManager(self.context)

    def runTest(self):
        pass

    def __printTestedClassFile(self, tested_class):
        path = self.file_manager.formatClassFilePath(tested_class)
        name = tested_class.__name__
        self.log.d('Testing %r at:\n %s' % (name, path))

    def testJsJasmine(self):
        tested_class = self._getTestedClass()
        self.module_unit_test_runner.runJsTest(tested_class)

    def testMethodsExistence(self):
        tested_class = self._getTestedClass()
        if not self.__runs_from_tested_module:
            self.__printTestedClassFile(tested_class)
        #tested_instance =
        for name, attrib in tested_class.__dict__.items():
            if callable(attrib) and not name.startswith('_'):
                test_name = 'test_%s' % name
                msg = 'There is no test test_%s for class %r' % (name, tested_class)
                assert test_name in self.__class__.__dict__, msg
                #assert callable(self.__class__.__dict__[test_name]) #TODO: necessary?

    def _getTestedClass(self):
        return self.twins_manager.getTestedFromTester(self.__class__)
class RPCJavascriptGenerator(SimpleRpcLogicBase):
    def __post_init__(self):
        self.file_manager = FileManager(self.context)
        self.package_translator = PackageToJs(self.context)
        self.templates_collector = TemplatesCollector(self.context)
        self.js_util = JsTranslateUtil(self.context)

    def getRpcNode(self, packages):
        packages_node = AutoTemplateAstNode()
        for p in packages:
            n = self.package_translator.translatePackage(p)
            name = p.__name__.split('.')[-1]
            packages_node.translate(name, n)
        exposed_rpc_node = TranslationAstNode('exposed_rpc.CommandQueueApi')
        exposed_rpc_node.translate(EXPOSED_PACKAGES=packages_node)
        return exposed_rpc_node

    def translateToFile(self, packages, js_rpc_file=None, templates=None,
                        overwrite=False):
        js_rpc_file = self.js_util._getJsRpcFile(js_rpc_file)
        if not templates:
            templates = self.templates_collector.collectBuiltIn()
        text = self.getRpcNode(packages).getString(templates)
        self.file_manager.saveTextFile(js_rpc_file, text, overwrite)
예제 #6
0
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)
예제 #7
0
 def __post_init__(self):
     self.file_manager = FileManager(self.context)
 def __post_init__(self):
     self.twins_manager = TwinModulesManager(self.context)
     self.file_manager = FileManager(self.context)
     self.nodejs = NodeJsRunner(self.context)
 def __post_init__(self):
     self.file_manager = FileManager(self.context)
     self.package_translator = PackageToJs(self.context)
     self.templates_collector = TemplatesCollector(self.context)
     self.js_util = JsTranslateUtil(self.context)
예제 #10
0
 def __post_init__(self):
     self.twins_manager = TwinModulesManager(self.context)
     self.module_unit_test_runner = ModuleUnitTestRunner(self.context)
     self.file_manager = FileManager(self.context)
예제 #11
0
 def __post_init__(self):
     self.js_util = JsTranslateUtil(self.context)
     self.file_manager = FileManager(self.context)
예제 #12
0
 def __post_init__(self):
     self.class_to_js = ClassToJs(self.context)
     self.templates = TemplatesCollector(self.context)
     self.file_manager = FileManager(self.context)