Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_files
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
common_helper_files
Commits
91492bf1
Commit
91492bf1
authored
Aug 16, 2021
by
Jörg Stucke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved broken links to fixture + refactoring
parent
5f1eab0a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
72 deletions
+101
-72
broken_link
tests/data/broken_link
+0
-2
recursive_broken_link
tests/data/recursive_broken_link
+0
-2
test_fail_safe_file_operations.py
tests/test_fail_safe_file_operations.py
+101
-68
No files found.
tests/data/broken_link
deleted
120000 → 0
View file @
5f1eab0a
nonexistent
\ No newline at end of file
tests/data/recursive_broken_link
deleted
120000 → 0
View file @
5f1eab0a
recursive_broken_link
\ No newline at end of file
tests/test_fail_safe_file_operations.py
View file @
91492bf1
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
\ufffd
rd line'
,
''
,
'first line'
],
"lines not correct"
)
assert
lines
==
[
'first line'
,
'second line'
,
'th
\ufffd
rd 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
(
temp
dir
.
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
(
temp
dir
.
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
(
temp
dir
.
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\n
c'
,
'ab
\n
c'
)])
(
'ab
\r\n
c'
,
'ab
\n
c'
),
])
def
test_rm_cr
(
input_data
,
expected
):
assert
_rm_cr
(
input_data
)
==
expected
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment