def save_tests(tests_string: Text,
                   filename: Optional[Text] = None) -> List[Text]:
        """Saves `test_string` to a file `filename`.

        Args:
            tests_string: Test that needs to be saved.
            filename: Path to a test file.
        """

        if not filename:
            filename = utils.get_project_directory() / DEFAULT_FILENAME

        existing_tests = get_tests_from_file(filename)
        new_tests = [
            test for test in _split_tests(tests_string)
            if test not in existing_tests
        ]

        if new_tests:
            all_tests = [f"## {test}\n" for test in existing_tests + new_tests]

            io_utils.create_directory_for_file(filename)
            io_utils.write_text_file("\n".join(all_tests), filename)

        return [f"## {test}" for test in new_tests]
def delete_tests_from_file(filename: Optional[Text] = None):
    """Deletes a file.

    Args:
        filename: Path to a test file.
    """

    if not filename:
        filename = utils.get_project_directory() / DEFAULT_FILENAME

    try:
        os.remove(filename)
    except OSError:
        logger.exception(f"Unable to delete tests from {filename}")
示例#3
0
    def dump_domain(self,
                    filename: Optional[Text] = None,
                    project_id: Text = config.project_name):
        """Dump domain to `filename` in yml format."""
        domain = self.get_domain(project_id)

        if not domain:
            return

        if not filename:
            filename = domain.get("path") or config.default_domain_path

        cleaned_domain = RasaDomain.from_dict(domain).cleaned_domain()
        domain_path = utils.get_project_directory() / filename
        dump_yaml_to_file(domain_path, cleaned_domain)
示例#4
0
    def assign_filename(self, team: Text) -> Text:
        """Finds the filename of the oldest document in the collection.

        Returns config.default_stories_filename if no filename was found.
        """

        oldest_file = (self.query(Story.filename).join(User).filter(
            User.team == team).order_by(Story.id.asc()).first())

        file_name = utils.get_project_directory()
        if oldest_file:
            file_name = file_name / oldest_file[0]
        else:
            file_name = file_name / config.data_dir / config.default_stories_filename

        return str(file_name)
    def dump_config(self,
                    team: Text,
                    project_id: Text,
                    filename: Optional[Text] = None) -> None:
        """Dump domain to `filename` in yml format.

        Args:
            team: Team whose config should be dumped.
            project_id: Project whose config should be dumped.
            filename: Name of the file the config should be dumped into.
        """

        project = self._get_project(team, project_id)
        if project:
            if not filename:
                default_path = str(utils.get_project_directory() /
                                   config.default_config_path)
                filename = json.loads(
                    project.config).get("path") or default_path
            utils.dump_yaml_to_file(filename=filename,
                                    content=project.get_model_config())
def get_tests_from_file(filename: Optional[Text] = None) -> List[Text]:
    """Returns an list of tests from a `filename`.

    Args:
        filename: Path to a test file.
    """

    if not filename:
        filename = utils.get_project_directory() / DEFAULT_FILENAME

    try:
        content = io_utils.read_file(filename)
        return _split_tests(content)
    except ValueError as e:
        rasa_utils.raise_warning(
            f"Unable to get tests from {filename}:\n{e} "
            f"Please, make sure you have end-to-end tests added to your assistant. "
            f"See https://rasa.com/docs/rasa/user-guide/testing-your-assistant/ "
            f"for more information.",
            UserWarning,
        )
        return []
示例#7
0
    def absolute_file_path(self) -> Text:
        from rasax.community import utils as rasax_utils

        return str(rasax_utils.get_project_directory() / self.relative_file_path)
    def _get_responses_file_name() -> Text:
        """Load the name of the file with responses."""
        from rasax.community import utils

        return str(utils.get_project_directory() / config.data_dir /
                   config.default_responses_filename)