Unverified Commit 86686b18 by Jörg Stucke Committed by GitHub

Merge pull request #4 from fkie-cad/3-remove-broken-links

3 remove broken links
parents 6733b09b 585806d9
name: Run Tests
on: [pull_request]
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: install deps
run: |
python3 -m pip install -U pip
python3 -m pip install --editable .[dev]
- name: run pytest
run: pytest
[pytest] [pytest]
addopts = --pep8 --cov=./ -v addopts = --pycodestyle --cov=./ -v
pep8ignore =
*.py E501
[pycodestyle]
ignore = E501,W503
select = E504
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = '0.2.2' VERSION = '0.2.3'
setup( setup(
name='common_helper_files', name='common_helper_files',
...@@ -9,6 +9,13 @@ setup( ...@@ -9,6 +9,13 @@ setup(
install_requires=[ install_requires=[
'bitmath' 'bitmath'
], ],
extras_require={
'dev': [
'pytest',
'pytest-pycodestyle',
'pytest-cov'
]
},
description='file operation helper functions', description='file operation helper functions',
author='Fraunhofer FKIE', author='Fraunhofer FKIE',
author_email='peter.weidenbach@fkie.fraunhofer.de', author_email='peter.weidenbach@fkie.fraunhofer.de',
......
nonexistent
\ No newline at end of file
recursive_broken_link
\ No newline at end of file
import os import os
import unittest
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
...@@ -11,126 +10,159 @@ from common_helper_files import ( ...@@ -11,126 +10,159 @@ from common_helper_files import (
) )
from common_helper_files.fail_safe_file_operations import _get_counted_file_path, _rm_cr from common_helper_files.fail_safe_file_operations import _get_counted_file_path, _rm_cr
TEST_DATA_DIR = Path(__file__).absolute().parent / 'data'
EMPTY_FOLDER = Path(get_directory_for_filename(__file__)).parent / 'tests' / 'data' / 'empty_folder' EMPTY_FOLDER = TEST_DATA_DIR / 'empty_folder'
EMPTY_FOLDER.mkdir(exist_ok=True) EMPTY_FOLDER.mkdir(exist_ok=True)
class TestFailSafeFileOperations(unittest.TestCase): @pytest.fixture(scope="function")
def tempdir():
def setUp(self): tmp_dir = None
self.tmp_dir = TemporaryDirectory(prefix="test_common_helper_file") try:
tmp_dir = TemporaryDirectory(prefix="test_common_helper_file")
def tearDown(self): yield tmp_dir
self.tmp_dir.cleanup() finally:
if tmp_dir:
@staticmethod tmp_dir.cleanup()
def get_directory_of_current_file():
@pytest.fixture(scope="module")
def create_symlinks():
recursive_broken_link = TEST_DATA_DIR / "recursive_broken_link"
broken_link = TEST_DATA_DIR / "broken_link"
try:
if not recursive_broken_link.is_symlink():
recursive_broken_link.symlink_to("recursive_broken_link")
if not broken_link.is_symlink():
broken_link.symlink_to("nonexistent")
yield
finally:
if recursive_broken_link.is_symlink():
recursive_broken_link.unlink()
if broken_link.is_symlink():
broken_link.unlink()
def get_directory_of_current_file():
return get_directory_for_filename(__file__) return get_directory_for_filename(__file__)
def test_fail_safe_read_file(self):
test_file_path = os.path.join(self.get_directory_of_current_file(), "data", "read_test") def test_fail_safe_read_file():
test_file_path = os.path.join(get_directory_of_current_file(), "data", "read_test")
file_binary = get_binary_from_file(test_file_path) file_binary = get_binary_from_file(test_file_path)
self.assertEqual(file_binary, b'this is a test', "content not correct") assert file_binary == b'this is a test', "content not correct"
# Test none existing file # Test none existing file
none_existing_file_path = os.path.join(self.get_directory_of_current_file(), "data", "none_existing_file") none_existing_file_path = os.path.join(get_directory_of_current_file(), "data", "none_existing_file")
file_binary = get_binary_from_file(none_existing_file_path) file_binary = get_binary_from_file(none_existing_file_path)
self.assertEqual(file_binary, b'', "content not correct") assert file_binary == b'', "content not correct"
# Test link # Test link
link_path = os.path.join(self.get_directory_of_current_file(), "data", "link_test") link_path = os.path.join(get_directory_of_current_file(), "data", "link_test")
file_binary = get_binary_from_file(link_path) file_binary = get_binary_from_file(link_path)
assert file_binary == 'symbolic link -> read_test' assert file_binary == 'symbolic link -> read_test'
def test_fail_safe_read_file_string_list(self):
test_file_path = os.path.join(self.get_directory_of_current_file(), "data", "multiline_test.txt") def test_fail_safe_read_file_string_list():
test_file_path = os.path.join(get_directory_of_current_file(), "data", "multiline_test.txt")
lines = get_string_list_from_file(test_file_path) lines = get_string_list_from_file(test_file_path)
self.assertEqual(lines, ['first line', 'second line', 'th\ufffdrd line', '', 'first line'], "lines not correct") assert lines == ['first line', 'second line', 'th\ufffdrd line', '', 'first line'], "lines not correct"
def test_fail_safe_write_file(self): def test_fail_safe_write_file(tempdir):
file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file") file_path = os.path.join(tempdir.name, "test_folder", "test_file")
write_binary_to_file(b'this is a test', file_path) write_binary_to_file(b'this is a test', file_path)
self.assertTrue(os.path.exists(file_path), "file not created") assert os.path.exists(file_path), "file not created"
read_binary = get_binary_from_file(file_path) read_binary = get_binary_from_file(file_path)
self.assertEqual(read_binary, b'this is a test', "written data not correct") assert read_binary == b'this is a test', "written data not correct"
# Test not overwrite flag # Test not overwrite flag
write_binary_to_file(b'do not overwrite', file_path, overwrite=False) write_binary_to_file(b'do not overwrite', file_path, overwrite=False)
read_binary = get_binary_from_file(file_path) read_binary = get_binary_from_file(file_path)
self.assertEqual(read_binary, b'this is a test', "written data not correct") assert read_binary == b'this is a test', "written data not correct"
# Test overwrite flag # Test overwrite flag
write_binary_to_file(b'overwrite', file_path, overwrite=True) write_binary_to_file(b'overwrite', file_path, overwrite=True)
read_binary = get_binary_from_file(file_path) read_binary = get_binary_from_file(file_path)
self.assertEqual(read_binary, b'overwrite', "written data not correct") assert read_binary == b'overwrite', "written data not correct"
# Test copy_file_flag # Test copy_file_flag
write_binary_to_file(b'second_overwrite', file_path, file_copy=True) write_binary_to_file(b'second_overwrite', file_path, file_copy=True)
self.assertTrue(os.path.exists("{}-1".format(file_path)), "new file copy does not exist") assert os.path.exists("{}-1".format(file_path)), "new file copy does not exist"
read_binary_original = get_binary_from_file(file_path) read_binary_original = get_binary_from_file(file_path)
self.assertEqual(read_binary_original, b'overwrite', "original file no longer correct") assert read_binary_original == b'overwrite', "original file no longer correct"
read_binary_new = get_binary_from_file("{}-1".format(file_path)) read_binary_new = get_binary_from_file("{}-1".format(file_path))
self.assertEqual(read_binary_new, b'second_overwrite', "binary of new file not correct") assert read_binary_new == b'second_overwrite', "binary of new file not correct"
def test_get_counted_file_path():
assert _get_counted_file_path("/foo/bar") == "/foo/bar-1", "simple case"
assert _get_counted_file_path("/foo/bar-11") == "/foo/bar-12", "simple count two digits"
assert _get_counted_file_path("foo-34/bar") == "foo-34/bar-1", "complex case"
def test_get_counted_file_path(self):
self.assertEqual(_get_counted_file_path("/foo/bar"), "/foo/bar-1", "simple case")
self.assertEqual(_get_counted_file_path("/foo/bar-11"), "/foo/bar-12", "simple count two digits")
self.assertEqual(_get_counted_file_path("foo-34/bar"), "foo-34/bar-1", "complex case")
def test_delete_file(self): def test_delete_file(tempdir):
file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file") file_path = os.path.join(tempdir.name, "test_folder", "test_file")
write_binary_to_file(b'this is a test', file_path) write_binary_to_file(b'this is a test', file_path)
self.assertTrue(os.path.exists(file_path), "file not created") assert os.path.exists(file_path), "file not created"
delete_file(file_path) delete_file(file_path)
self.assertFalse(os.path.exists(file_path)) assert not os.path.exists(file_path)
# Test delete none existing file # Test delete none existing file
delete_file(file_path) delete_file(file_path)
def test_create_symlink(self):
test_file_path = os.path.join(self.tmp_dir.name, 'test_folder', 'test_file') def test_create_symlink(tempdir):
symlink_path = os.path.join(self.tmp_dir.name, 'test_symlink') test_file_path = os.path.join(tempdir.name, 'test_folder', 'test_file')
symlink_path = os.path.join(tempdir.name, 'test_symlink')
create_symlink(test_file_path, symlink_path) create_symlink(test_file_path, symlink_path)
self.assertEqual(os.readlink(symlink_path), test_file_path) assert os.readlink(symlink_path) == test_file_path
symlink_path_none_existing_dir = os.path.join(self.tmp_dir.name, 'some_dir/test_symlink') symlink_path_none_existing_dir = os.path.join(tempdir.name, 'some_dir/test_symlink')
create_symlink(test_file_path, symlink_path_none_existing_dir) create_symlink(test_file_path, symlink_path_none_existing_dir)
self.assertEqual(os.readlink(symlink_path_none_existing_dir), test_file_path) assert os.readlink(symlink_path_none_existing_dir) == test_file_path
# check error handling # check error handling
create_symlink(test_file_path, symlink_path) create_symlink(test_file_path, symlink_path)
def test_get_safe_name(self):
def test_get_safe_name():
a = "/()=Hello%&World!? Foo" a = "/()=Hello%&World!? Foo"
self.assertEqual(get_safe_name(a), "HelloWorld_Foo", "result not correct") assert get_safe_name(a) == "HelloWorld_Foo", "result not correct"
b = 250 * 'a' b = 250 * 'a'
self.assertEqual(len(get_safe_name(b)), 200, "lenght not cutted correctly") assert len(get_safe_name(b)) == 200, "lenght not cutted correctly"
def test_get_files_in_dir(self): def test_get_files_in_dir(create_symlinks):
test_dir_path = os.path.join(self.get_directory_of_current_file(), "data") test_dir_path = os.path.join(get_directory_of_current_file(), "data")
result = get_files_in_dir(test_dir_path) result = get_files_in_dir(test_dir_path)
self.assertIn(os.path.join(test_dir_path, "read_test"), result, "file in root folder not found") assert os.path.join(test_dir_path, "read_test") in result, "file in root folder not found"
self.assertIn(os.path.join(test_dir_path, "test_folder/generic_test_file"), result, "file in sub folder not found") assert os.path.join(test_dir_path, "test_folder/generic_test_file") in result, "file in sub folder not found"
self.assertEqual(len(result), 6, "number of found files not correct") assert len(result) == 6, "number of found files not correct"
def test_get_files_in_dir_error(self):
def test_get_files_in_dir_error():
result = get_files_in_dir("/none_existing/dir") result = get_files_in_dir("/none_existing/dir")
self.assertEqual(result, [], "error result should be an empty list") assert result == [], "error result should be an empty list"
def test_get_dirs(self): def test_get_dirs(tempdir):
test_dirs = ["dir_1", "dir_2", "dir_1/sub_dir"] test_dirs = ["dir_1", "dir_2", "dir_1/sub_dir"]
for item in test_dirs: for item in test_dirs:
os.mkdir(os.path.join(self.tmp_dir.name, item)) os.mkdir(os.path.join(tempdir.name, item))
result = get_dirs_in_dir(self.tmp_dir.name) result = get_dirs_in_dir(tempdir.name)
self.assertEqual(sorted(result), [os.path.join(self.tmp_dir.name, "dir_1"), os.path.join(self.tmp_dir.name, "dir_2")], "found dirs not correct") expected_result = [os.path.join(tempdir.name, "dir_1"), os.path.join(tempdir.name, "dir_2")]
assert sorted(result) == expected_result, "found dirs not correct"
def test_get_dirs_in_dir_error(self):
def test_get_dirs_in_dir_error():
result = get_dirs_in_dir("/none_existing/dir") result = get_dirs_in_dir("/none_existing/dir")
self.assertEqual(result, [], "error result should be an empty list") assert result == [], "error result should be an empty list"
def test_get_dir_of_file_relative_path(self): def test_get_dir_of_file_relative_path():
relative_path_result = get_dir_of_file("test/some_file") relative_path_result = get_dir_of_file("test/some_file")
expected_result = os.path.join(os.getcwd(), "test") expected_result = os.path.join(os.getcwd(), "test")
self.assertEqual(relative_path_result, expected_result) assert relative_path_result == expected_result
def test_get_dir_of_file_absolute_path(self):
test_file_path = os.path.join(self.tmp_dir.name, 'test_file') def test_get_dir_of_file_absolute_path(tempdir):
test_file_path = os.path.join(tempdir.name, 'test_file')
write_binary_to_file('test', test_file_path) write_binary_to_file('test', test_file_path)
absolute_file_path_result = get_dir_of_file(test_file_path) absolute_file_path_result = get_dir_of_file(test_file_path)
self.assertEqual(absolute_file_path_result, self.tmp_dir.name) assert absolute_file_path_result == tempdir.name
@pytest.mark.parametrize('symlinks, directories, expected_number', [ @pytest.mark.parametrize('symlinks, directories, expected_number', [
...@@ -140,7 +172,7 @@ class TestFailSafeFileOperations(unittest.TestCase): ...@@ -140,7 +172,7 @@ class TestFailSafeFileOperations(unittest.TestCase):
(False, False, 3), (False, False, 3),
]) ])
def test_safe_rglob(symlinks, directories, expected_number): def test_safe_rglob(symlinks, directories, expected_number):
test_dir_path = Path(TestFailSafeFileOperations.get_directory_of_current_file()).parent / 'tests' / 'data' test_dir_path = Path(get_directory_of_current_file()).parent / 'tests' / 'data'
result = list(safe_rglob(test_dir_path, include_symlinks=symlinks, include_directories=directories)) result = list(safe_rglob(test_dir_path, include_symlinks=symlinks, include_directories=directories))
assert len(result) == expected_number assert len(result) == expected_number
...@@ -160,6 +192,7 @@ def test_safe_rglob_empty_dir(): ...@@ -160,6 +192,7 @@ def test_safe_rglob_empty_dir():
@pytest.mark.parametrize('input_data, expected', [ @pytest.mark.parametrize('input_data, expected', [
('abc', 'abc'), ('abc', 'abc'),
('ab\r\nc', 'ab\nc')]) ('ab\r\nc', 'ab\nc'),
])
def test_rm_cr(input_data, expected): def test_rm_cr(input_data, expected):
assert _rm_cr(input_data) == expected assert _rm_cr(input_data) == expected
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment