def _execute_inst(self, i): """ Instantiates single template file :param i: string list :return: None """ # append src and dest root parts to names (must be done once) if not self._is_instr_file_parsed: i['args'][0] = self.src_root_dir + self.PATH_DELIMITER_CHAR + i['args'][0] i['args'][1] = self.dest_root_dir + self.PATH_DELIMITER_CHAR + i['args'][1] src_filename = i['args'][0] dest_filename = i['args'][1] split_df = lolly_helpers.path_base_and_leaf(dest_filename) contents = lolly_helpers.silent_read_text_file(src_filename) if contents['error']: self._report_file_operation_error("can't read file '" + src_filename + "'.") return # process conditions contents = self._process_conditional_directives(contents['contents'], src_filename) contents = lolly_helpers.replace_keys(contents, self.replacement_dict) # make sure dest directory exists lolly_helpers.silent_create_path(split_df['base']) if not lolly_helpers.dir_exists(split_df['base']): self._report_file_operation_error("can't create folder: '" + split_df['base'] + "'") return lolly_helpers.silent_write_text_file(dest_filename, contents) if not lolly_helpers.file_exists(dest_filename): self._report_file_operation_error("can't write file: '" + dest_filename + "'") return
def test_dir_empty(self): self.__create_test_dir_if_not_exists() base_dir = self.TMP_DIR + '/empty_dir_test' lolly_helpers.silent_create_path(base_dir, overwrite=True) assert lolly_helpers.dir_empty(base_dir) lolly_helpers.silent_write_text_file(base_dir + '/test.txt', 'test') assert not lolly_helpers.dir_empty(base_dir)
def test_silent_create_path(self): # sad path can't be easily tested because python replaces illegal characters in file names with legal # some_wrong_path = self.TMP_DIR + "/:::" # assert lollyhelpers.silent_create_path(some_wrong_path)['error'] # happy path path = self.TMP_DIR + '/some/test/directory' assert not lolly_helpers.silent_create_path(path)['error'] assert lolly_helpers.dir_exists(path) # create a file inside that directory filepath = path + '/testfile.txt' lolly_helpers.silent_write_text_file(filepath, 'TEST')
def test_get_file_list(self): self.__create_test_dir_if_not_exists() base_dir = self.TMP_DIR + '/file_list_test' lolly_helpers.silent_create_path(base_dir) file_names = ['one', 'two', 'three'] file_names = sorted(file_names) for name in file_names: lolly_helpers.silent_write_text_file(base_dir + '/' + name, name) result = lolly_helpers.get_file_list(base_dir) sorted_result = sorted(result['files']) assert not result['error'] assert sorted_result == file_names
def test_silent_copy_file(self): self.__create_test_dir_if_not_exists() dest = self.TMP_DIR + '/copied.txt' # sad path bad_origin = self.TMP_DIR + '/non_existent.txt' assert lolly_helpers.silent_copy_file(bad_origin, dest)['error'] # happy path origin = self.TMP_DIR + '/copy_origin.txt' msg = 'copy_file_test' lolly_helpers.silent_write_text_file(origin, msg) assert not lolly_helpers.silent_copy_file(origin, dest)['error'] assert lolly_helpers.silent_read_text_file(dest)['contents'] == msg
def test_get_dir_item_count(self): self.__create_test_dir_if_not_exists() base_dir = self.TMP_DIR + '/item_count' lolly_helpers.silent_create_path(base_dir, overwrite=True) assert lolly_helpers.get_dir_item_count(base_dir)['count'] == 0 file_names = ['one.txt', 'two.txt', 'three.txt'] file_names = sorted(file_names) for name in file_names: lolly_helpers.silent_write_text_file(base_dir + '/' + name, name) assert lolly_helpers.get_dir_item_count(base_dir)['count'] == 3 dir_names = ['one', 'two', 'three'] dir_names = sorted(dir_names) for name in dir_names: lolly_helpers.silent_create_path(base_dir + '/' + name) assert lolly_helpers.get_dir_item_count(base_dir)['count'] == 6
def test_silent_write_text_file(self): self.__create_test_dir_if_not_exists() path = self.TMP_DIR + '/' + 'silent_write_text_file.txt' # Check file not yet exists if lolly_helpers.file_exists(path): lolly_helpers.silent_remove_file(path) contents = 'TEST' assert not lolly_helpers.silent_write_text_file(path, contents)['error'] assert lolly_helpers.file_exists(path) data = lolly_helpers.silent_read_text_file(path) assert data['contents'] == contents # Check that existing file is rewritten contents = 'YET ANOTHER CONTENTS' assert not lolly_helpers.silent_write_text_file(path, contents)['error'] assert lolly_helpers.file_exists(path) data = lolly_helpers.silent_read_text_file(path) assert data['contents'] == contents
def test_silent_remove_file(self): self.__create_test_dir_if_not_exists() # sad path can't be easily tested # happy path non_existent_file = self.TMP_DIR + '/non_existent.txt' assert not lolly_helpers.silent_remove_file(non_existent_file)['error'] existent_file = self.TMP_DIR + '/existent.txt' assert not lolly_helpers.silent_write_text_file(existent_file, 'TEST')['error'] assert lolly_helpers.file_exists(existent_file) assert not lolly_helpers.silent_remove_file(existent_file)['error'] assert not lolly_helpers.file_exists(existent_file)