def test_write_file_action_yaml(tmp_yaml_file: pathlib.Path, sample_nist_component_def): """Test write yaml action.""" element = Element(sample_nist_component_def, 'component-definition') try: # string path should error wa = WriteFileAction(tmp_yaml_file.as_posix(), element, FileContentType.YAML) except TrestleError: pass try: # invalid file extension should error wa = WriteFileAction(tmp_yaml_file, element, FileContentType.JSON) except TrestleError: pass try: # writing to a non-existing file will error wa = WriteFileAction(tmp_yaml_file, element, FileContentType.YAML) wa.execute() except TrestleError: pass tmp_yaml_file.touch() wa = WriteFileAction(tmp_yaml_file, element, FileContentType.YAML) wa.execute() test_utils.verify_file_content(tmp_yaml_file, element.get())
def test_write_action_json(tmp_json_file, sample_target_def): """Test write json action.""" element = Element(sample_target_def, 'target-definition') with open(tmp_json_file, 'w+') as writer: wa = WriteAction(writer, element, FileContentType.JSON) wa.execute() test_utils.verify_file_content(tmp_json_file, element.get()) os.remove(tmp_json_file)
def test_write_action_yaml(tmp_yaml_file, sample_target): """Test write yaml action.""" element = Element(sample_target) with open(tmp_yaml_file, 'w+') as writer: wa = WriteAction(writer, element, FileContentType.YAML) wa.execute() test_utils.verify_file_content(tmp_yaml_file, element.get()) os.remove(tmp_yaml_file)
def test_write_action_json(tmp_json_file, sample_nist_component_def): """Test write json action.""" element = Element(sample_nist_component_def, 'component-definition') with open(tmp_json_file, 'w+', encoding=const.FILE_ENCODING) as writer: wa = WriteAction(writer, element, FileContentType.JSON) wa.execute() writer.flush() writer.close() test_utils.verify_file_content(tmp_json_file, element.get()) os.remove(tmp_json_file)
def test_write_file_rollback(tmp_yaml_file: pathlib.Path, sample_target): """Test rollback.""" element = Element(sample_target) tmp_yaml_file.touch() wa = WriteFileAction(tmp_yaml_file, element, FileContentType.YAML) wa.execute() test_utils.verify_file_content(tmp_yaml_file, element.get()) # verify the file content is not empty with open(tmp_yaml_file, 'r', encoding='utf8') as read_file: assert read_file.tell() >= 0 wa.rollback() # verify the file content is empty with open(tmp_yaml_file, 'r', encoding='utf8') as read_file: assert read_file.tell() == 0
def test_write_file_rollback(tmp_yaml_file: pathlib.Path, sample_nist_component_def): """Test rollback.""" element = Element(sample_nist_component_def, 'component-definition') tmp_yaml_file.touch() wa = WriteFileAction(tmp_yaml_file, element, FileContentType.YAML) wa.execute() test_utils.verify_file_content(tmp_yaml_file, element.get()) # verify the file content is not empty with open(tmp_yaml_file, 'r', encoding=const.FILE_ENCODING) as read_file: assert read_file.tell() >= 0 wa.rollback() # verify the file content is empty with open(tmp_yaml_file, 'r', encoding=const.FILE_ENCODING) as read_file: assert read_file.tell() == 0