示例#1
0
    def test_parse(self):
        self.parser_test.parse('file-fixtures')
        assert os.path.exists(utils.get_cache_filename()) is True
        assert self.parser_test.test_files_structure == self.parsed_structure

        with open(utils.get_cache_filename()) as file:
            data = json.load(file)
        assert data == self.parsed_structure and data == self.parser_test.test_files_structure
示例#2
0
    def test_cache_auto_update(self):
        self.do_parse()
        with open(get_cache_filename(), 'r') as file:
            test_data = json.load(file)

        rigth_time = test_data.get('m_time')
        new_time = {'m_time': time.time()}
        test_data.update(new_time)
        with open(get_cache_filename(), 'w') as file:
            json.dump(test_data, file)

        assert rigth_time == self.finder._files_structure.get('m_time')
示例#3
0
    def __getattr__(self, item):
        # lazy loading of data from the cache
        if item == '_files_structure':
            if os.path.exists(utils.get_cache_filename()):
                with open(utils.get_cache_filename()) as file:
                    self._files_structure = json.load(file)

                # automatic update cache
                m_time = self._files_structure.get('m_time')
                test_folder = self._files_structure.get('test_folder')
                if utils.is_folder_modified(m_time, test_folder):
                    self._files_structure = self.cache_update(
                        self._files_structure.get('test_folder'))
            else:
                raise utils.BTTError(
                    'Cache file not found. First run the command "btt parse folderpath"'
                )

        return self.__dict__.get(item)
示例#4
0
def parse(path):
    file_parser = parser.ParserTests()
    try:
        files_number = file_parser.parse(path)
    except utils.BTTError as error:
        click.secho(error.message, fg=error.color)
    else:
        click.secho('Parsing completed. Found {} files.'.format(files_number),
                    fg='green')
        click.secho('Cache saved into "{}"'.format(utils.get_cache_filename()),
                    fg='green')
示例#5
0
class ParserTests:
    """
    The class contains commands for parsing the structure of tests,
    saving to the cache and displaying it on the screen
    """

    _cache_file = utils.get_cache_filename()

    def __init__(self):
        self.file_scaner = FilesScaner()
        self.file_parser = FilesParser()
        self.test_files_structure = {}

    def parse(self, folder_path):
        """
        Searches for test files and parses them
        :param str folder_path: path to target folder
        :return int: number of found test files
        """
        utils.check_test_folder(folder_path)
        m_time = self.file_scaner.scan(folder_path)

        if not self.file_scaner.files:
            raise utils.BTTError('Nothing to parse - no test files')

        for filepath in self.file_scaner.files:
            self.test_files_structure[filepath] = self.file_parser.parse_file(
                filepath)

        self.test_files_structure.update({
            'm_time': m_time,
            'test_folder': folder_path
        })
        self._saves_cache()

        return len(self.file_scaner.files)

    def _saves_cache(self):
        """
        Saves a parsed test structure to a json file
        :return:
        """
        with open(self._cache_file, 'w') as outfile:
            json.dump(self.test_files_structure, outfile)
示例#6
0
 def teardown_method(self, method):
     if os.path.exists(utils.get_cache_filename()):
         os.remove(utils.get_cache_filename())
示例#7
0
 def test_parse(self):
     result = self.runner.invoke(parse, 'file-fixtures')
     assert result.exit_code == 0
     assert ('Parsing completed. Found 4 files.\n'
             'Cache saved into "{}"\n'.format(
                 utils.get_cache_filename()) == result.output)
示例#8
0
 def remove_cache(self):
     if os.path.exists(utils.get_cache_filename()):
         os.remove(utils.get_cache_filename())
示例#9
0
def test_get_cache_filename():
    assert (utils.get_cache_filename() == '{}/better-test-tool.json'.format(
        get_cache_folder()))