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]
addopts = --pep8 --cov=./ -v
pep8ignore =
*.py E501
addopts = --pycodestyle --cov=./ -v
[pycodestyle]
ignore = E501,W503
select = E504
from setuptools import setup, find_packages
VERSION = '0.2.2'
VERSION = '0.2.3'
setup(
name='common_helper_files',
......@@ -9,6 +9,13 @@ setup(
install_requires=[
'bitmath'
],
extras_require={
'dev': [
'pytest',
'pytest-pycodestyle',
'pytest-cov'
]
},
description='file operation helper functions',
author='Fraunhofer FKIE',
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 unittest
from pathlib import Path
from tempfile import TemporaryDirectory
......@@ -11,126 +10,159 @@ from common_helper_files import (
)
from common_helper_files.fail_safe_file_operations import _get_counted_file_path, _rm_cr
EMPTY_FOLDER = Path(get_directory_for_filename(__file__)).parent / 'tests' / 'data' / 'empty_folder'
TEST_DATA_DIR = Path(__file__).absolute().parent / 'data'
EMPTY_FOLDER = TEST_DATA_DIR / 'empty_folder'
EMPTY_FOLDER.mkdir(exist_ok=True)
class TestFailSafeFileOperations(unittest.TestCase):
def setUp(self):
self.tmp_dir = TemporaryDirectory(prefix="test_common_helper_file")
def tearDown(self):
self.tmp_dir.cleanup()
@staticmethod
def get_directory_of_current_file():
@pytest.fixture(scope="function")
def tempdir():
tmp_dir = None
try:
tmp_dir = TemporaryDirectory(prefix="test_common_helper_file")
yield tmp_dir
finally:
if tmp_dir:
tmp_dir.cleanup()
@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__)
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)
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
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)
self.assertEqual(file_binary, b'', "content not correct")
assert file_binary == b'', "content not correct"
# 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)
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)
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):
file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file")
def test_fail_safe_write_file(tempdir):
file_path = os.path.join(tempdir.name, "test_folder", "test_file")
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)
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
write_binary_to_file(b'do not overwrite', file_path, overwrite=False)
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
write_binary_to_file(b'overwrite', file_path, overwrite=True)
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
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)
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))
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):
file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file")
def test_delete_file(tempdir):
file_path = os.path.join(tempdir.name, "test_folder", "test_file")
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)
self.assertFalse(os.path.exists(file_path))
assert not os.path.exists(file_path)
# Test delete none existing file
delete_file(file_path)
def test_create_symlink(self):
test_file_path = os.path.join(self.tmp_dir.name, 'test_folder', 'test_file')
symlink_path = os.path.join(self.tmp_dir.name, 'test_symlink')
def test_create_symlink(tempdir):
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)
self.assertEqual(os.readlink(symlink_path), test_file_path)
symlink_path_none_existing_dir = os.path.join(self.tmp_dir.name, 'some_dir/test_symlink')
assert os.readlink(symlink_path) == test_file_path
symlink_path_none_existing_dir = os.path.join(tempdir.name, 'some_dir/test_symlink')
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
create_symlink(test_file_path, symlink_path)
def test_get_safe_name(self):
def test_get_safe_name():
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'
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):
test_dir_path = os.path.join(self.get_directory_of_current_file(), "data")
def test_get_files_in_dir(create_symlinks):
test_dir_path = os.path.join(get_directory_of_current_file(), "data")
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")
self.assertIn(os.path.join(test_dir_path, "test_folder/generic_test_file"), result, "file in sub folder not found")
self.assertEqual(len(result), 6, "number of found files not correct")
assert os.path.join(test_dir_path, "read_test") in result, "file in root folder not found"
assert os.path.join(test_dir_path, "test_folder/generic_test_file") in result, "file in sub folder not found"
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")
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"]
for item in test_dirs:
os.mkdir(os.path.join(self.tmp_dir.name, item))
result = get_dirs_in_dir(self.tmp_dir.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")
os.mkdir(os.path.join(tempdir.name, item))
result = get_dirs_in_dir(tempdir.name)
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")
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")
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)
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', [
......@@ -140,7 +172,7 @@ class TestFailSafeFileOperations(unittest.TestCase):
(False, False, 3),
])
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))
assert len(result) == expected_number
......@@ -160,6 +192,7 @@ def test_safe_rglob_empty_dir():
@pytest.mark.parametrize('input_data, expected', [
('abc', 'abc'),
('ab\r\nc', 'ab\nc')])
('ab\r\nc', 'ab\nc'),
])
def test_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