def test__collect_single_filepath_to_long_for_windows(self): """ Should "collect" an error message which states that the target path is too long for the underlying platform. :return: """ from artefact.localhost.file import FileCopy collector = FileCopy(parameters={}, parent=None) collector.source_path = self._expected_source_file_path expected_message = "File './testfiles/test.txt' could not be copied, because the target path length of " \ "'./unique' is too long for the underlying system." if platform.system() == "Windows": expected_message = "File './testfiles/test.txt' could not be copied, because the target path length of " \ "'.\\unique' is too long for the underlying system." with mock.patch('artefact.localhost.file.os.path.isfile', MagicMock(return_value=True)): with mock.patch( 'artefact.localhost.file.FileCopy._ensure_target_directory', MagicMock(return_value=self._expected_target_path)): with mock.patch( 'artefact.localhost.file.FileCopy._validate_target_path_length', MagicMock(return_value=False)): collector._collect_single(collector.source_path) actual_message = collector.data[-1].collecteddata self.assertEqual(expected_message, actual_message)
def test__collect_single_could_not_copy_file(self): """ Should ensure that unique target directory is deleted. :return: """ from artefact.localhost.file import FileCopy expected_source_file_path = f"{self._expected_unique_directory}/IDoNotExist.txt" collector = FileCopy(parameters={}, parent=None) collector.source_path = expected_source_file_path collector._collect_single() self.assertFalse(os.path.exists(self._expected_target_path))
def test__collect_single_filepath_is_a_directory(self): """ Should "collect" an error message which states that the "filepath" provided is actually a directory. :return: """ from artefact.localhost.file import FileCopy expected_file = "/iamnotafile" expected_message = f"The provided filepath '{expected_file}' is not a file." collector = FileCopy(parameters={'filepath': expected_file}, parent=None) collector._collect_single(file_path=expected_file) actual_message = collector.data[0].collecteddata self.assertEqual(expected_message, actual_message)
def test__collect_single(self): """ Should copy the file, identified by the member 'filepath'. :return: """ from artefact.localhost.file import FileCopy collector = FileCopy(parameters={}, parent=None) collector.source_path = self._expected_source_file_path with mock.patch('artefact.localhost.file.os.path.isfile', MagicMock(return_value=True)): with mock.patch( 'artefact.localhost.file.FileCopy._ensure_target_directory', MagicMock(return_value=self._expected_target_path)): # Create the target path here, because we mocked _ensure_target_directory if not os.path.exists(self._expected_target_path): os.mkdir(self._expected_target_path) collector._collect_single(collector.source_path) self.assertTrue(os.path.exists(self._expected_target_file_path))
def test__collect_single_file_does_not_exist(self): """ Should store a message in data, that says that the file from which the content should be collected, does not exist. :return: """ from artefact.localhost.file import FileCopy expected_source_file_path = "./somedir/IDoNotExist.txt" expected_data = f"The file '{expected_source_file_path}' does not exist." collector = FileCopy(parameters={}, parent=None) collector.source_path = expected_source_file_path with mock.patch('artefact.localhost.file.os.path.isfile', MagicMock(return_value=True)): with mock.patch( 'artefact.localhost.file.FileCopy._ensure_target_directory', MagicMock(return_value=self._expected_target_path)): collector._collect_single(file_path=collector.source_path) actual_data = collector.data[0].collecteddata self.assertEqual(expected_data, actual_data)
def test__collect_single_log_data(self): """ Should log inside data which file was copied and what is the target destination of the copy. Should collect the md5 hash of the file, and the sha1 hash of the file. :return: """ from artefact.localhost.file import FileCopy expected_data = f"Copied file '{self._expected_source_file_path}' to '{self._expected_target_file_path}'." collector = FileCopy(parameters={}, parent=None) collector.source_path = self._expected_source_file_path with mock.patch( 'artefact.localhost.file.FileCopy._ensure_target_directory', MagicMock(return_value=self._expected_target_path)): # Create the target path here, because we mocked _ensure_target_directory if not os.path.exists(self._expected_target_path): os.mkdir(self._expected_target_path) collector._collect_single(collector.source_path) actual_data = collector.data[0].collecteddata self.assertEqual(expected_data, actual_data)