コード例 #1
0
    def testEngineNotReady(self):
        """Tests if the engine is fully configured."""
        test_engine = engine.ScaffolderEngine()
        with self.assertRaises(errors.EngineNotConfigured):
            _ = list(test_engine.GenerateFiles())

        path = 'this is absolutely the correct path'
        test_engine.SetProjectRootPath(path)

        with self.assertRaises(errors.EngineNotConfigured):
            _ = list(test_engine.GenerateFiles())

        test_engine.SetModuleName('TestBarWithFoo')
        with self.assertRaises(errors.EngineNotConfigured):
            _ = list(test_engine.GenerateFiles())

        with self.assertRaises(errors.ScaffolderNotConfigured):
            test_engine.StoreScaffolderAttribute('test1', 'foobar', str)

        test_engine.SetScaffolder(AwesomeScaffolder())
        with self.assertRaises(errors.EngineNotConfigured):
            _ = list(test_engine.GenerateFiles())

        test_string1 = 'Test String'
        test_string2 = 'Part of the scaffolder'
        test_string3 = 'I\'m stored in the scaffolder!'
        test_engine.StoreScaffolderAttribute('test1', test_string1, str)
        test_engine.StoreScaffolderAttribute('test2', test_string2, str)
        test_engine.StoreScaffolderAttribute('test3', test_string3, str)

        self.assertListEqual(list(test_engine.GenerateFiles()), [])
コード例 #2
0
    def testStoreScaffolderAttribute(self):
        """Tests storing attributes in a scaffolder."""
        test_engine = engine.ScaffolderEngine()
        test_scaffolder = AwesomeScaffolder()
        test_engine.SetScaffolder(test_scaffolder)

        test_string1 = 'Test String'
        test_string2 = 'Part of the scaffolder'
        test_string3 = 'I\'m stored in the scaffolder!'

        test_engine.StoreScaffolderAttribute('test1', test_string1, str)
        with self.assertRaises(errors.ScaffolderNotConfigured):
            test_scaffolder.RaiseIfNotReady()

        test_engine.StoreScaffolderAttribute('test2', test_string2, str)
        with self.assertRaises(errors.ScaffolderNotConfigured):
            test_scaffolder.RaiseIfNotReady()

        test_engine.StoreScaffolderAttribute('test3', test_string3, str)
        self.assertIsNone(test_scaffolder.RaiseIfNotReady())

        scaffolder_attributes = test_scaffolder.GetJinjaContext()
        test1_attr = scaffolder_attributes.get('test1', '')
        test2_attr = scaffolder_attributes.get('test2', '')
        test3_attr = scaffolder_attributes.get('test3', '')

        self.assertEqual(test1_attr, test_string1)
        self.assertEqual(test2_attr, test_string2)
        self.assertEqual(test3_attr, test_string3)
コード例 #3
0
    def testSetScaffolder(self):
        """Tests setting the scaffolder of a scaffolder engine."""
        test_engine = engine.ScaffolderEngine()
        test_scaffolder = AwesomeScaffolder()

        test_engine.SetScaffolder(test_scaffolder)

        self.assertTrue(hasattr(test_engine, '_scaffolder'))
        # pylint: disable=protected-access
        self.assertIsInstance(test_engine._scaffolder,
                              scaffolder_interface.Scaffolder)
コード例 #4
0
ファイル: frontend.py プロジェクト: Onager/PlasoScaffolder
    def Start(self, definition_value):
        """Start the CLI.

    Args:
      definition_value (str): the definition string chosen by UI.
    """
        self._output_handler.PrintInfo('   == Starting the scaffolder ==')
        self._output_handler.PrintInfo('Gathering required information.')
        scaffolder_engine = engine.ScaffolderEngine()

        definition = self.GetDefinition(definition_value)

        project_path = self.GetProjectPath(definition)
        scaffolder_engine.SetProjectRootPath(project_path)

        module_name = self.GetModuleName()
        scaffolder_engine.SetModuleName(module_name)
        self._output_handler.PrintInfo(
            'About to create a new feature branch to store newly generated code.'
        )
        try:
            self.CreateGitFeatureBranch(project_path,
                                        scaffolder_engine.module_name)
        except errors.UnableToConfigure as exception:
            self._output_handler.PrintError((
                'Unable to create feature branch, is {0:s} a valid git project path? '
                'The error message was: {1!s}').format(project_path,
                                                       exception))
            self._output_handler.PrintError(
                'Due to fatal error, not proceeding.')
            return

        scaffolder = self.GetScaffolder(definition)
        scaffolder_engine.SetScaffolder(scaffolder)
        try:
            self.GatherScaffolderAnswers(scaffolder, scaffolder_engine)
        except errors.UnableToConfigure as exception:
            self._output_handler.PrintError(
                ('Aborting. Unable to properly configure scaffolder '
                 'with error {0!s}.').format(exception))
            return

        ready = self._output_handler.Confirm('Ready to generate files?')
        if ready:
            for file_path in scaffolder_engine.GenerateFiles():
                self._output_handler.PrintOutput(
                    'File: {0:s} written to disk.'.format(file_path))
                _, _, file_path_inside_project = file_path.partition(
                    project_path)
                if file_path_inside_project.startswith(os.sep):
                    file_path_inside_project = file_path_inside_project[1:]
                self._git_helper.AddFileToTrack(file_path_inside_project)
コード例 #5
0
    def testSetProjectRootPath(self):
        """Tests setting the root path to a project."""
        test_engine = engine.ScaffolderEngine()

        # Test a path that will fail.
        path = os.path.join(os.path.curdir, 'wrong path')
        with self.assertRaises(errors.NoValidDefinition):
            test_engine.SetProjectRootPath(path)

        path = 'this is absolutely the correct path'
        test_engine.SetProjectRootPath(path)

        # pylint: disable=protected-access
        root_path = test_engine._definition_root_path
        project = test_engine._definition

        self.assertEqual(root_path, path)
        self.assertEqual(project, NotWrongDefinition.NAME)
コード例 #6
0
    def testSetModuleName(self):
        """Tests setting the module name."""
        test_engine = engine.ScaffolderEngine()
        test_name = 'foobar'

        test_engine.SetModuleName(test_name)

        module_name = test_engine.module_name
        self.assertEqual(module_name, test_name.title())

        test_name = 'some module this is'
        expected_module_name = 'SomeModuleThisIs'
        expected_file_name = 'some_module_this_is'

        test_engine.SetModuleName(test_name)
        module_name = test_engine.module_name
        # pylint: disable=protected-access
        file_name = test_engine._file_name_prefix

        self.assertEqual(expected_module_name, module_name)
        self.assertEqual(expected_file_name, file_name)