Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
common-package
Manage
Activity
Members
Labels
Plan
Issues
4
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GSHS Utilities
common-package
Commits
16f83e67
Commit
16f83e67
authored
10 months ago
by
Grant
Browse files
Options
Downloads
Patches
Plain Diff
updates to testing to 68% on file_operations
parent
570a38ac
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Pipfile
+1
-0
1 addition, 0 deletions
Pipfile
src/common/env.py
+4
-4
4 additions, 4 deletions
src/common/env.py
tests/test_common/test_file_operations.py
+116
-0
116 additions, 0 deletions
tests/test_common/test_file_operations.py
with
121 additions
and
4 deletions
Pipfile
+
1
−
0
View file @
16f83e67
...
...
@@ -13,6 +13,7 @@ black = "*"
flake8
=
"*"
pylint
=
"*"
tox
=
"*"
pytest-cov
=
"*"
[scripts]
lint
=
"tox -e lint"
...
...
This diff is collapsed.
Click to expand it.
src/common/env.py
+
4
−
4
View file @
16f83e67
...
...
@@ -9,9 +9,9 @@ def boolify(var):
:param var: the variable to check to see if it can be converted to bool
"""
if
var
in
[
0
,
"
0
"
,
"
FALSE
"
,
"
False
"
,
"
false
"
,
'
f
'
,
'
F
'
,
False
]:
if
var
in
[
0
,
"
0
"
,
"
FALSE
"
,
"
False
"
,
"
false
"
,
"
f
"
,
"
F
"
,
False
]:
return
False
if
var
in
[
1
,
"
1
"
,
"
TRUE
"
,
"
True
"
,
"
true
"
,
'
t
'
,
'
T
'
,
True
]:
if
var
in
[
1
,
"
1
"
,
"
TRUE
"
,
"
True
"
,
"
true
"
,
"
t
"
,
"
T
"
,
True
]:
return
True
raise
TypeError
(
"
unable to evaluate expected boolean
"
)
...
...
@@ -35,7 +35,7 @@ def check_environment(env_var, default=None):
return
float
(
os
.
environ
[
env_var
])
if
isinstance
(
default
,
str
):
return
str
(
os
.
environ
[
env_var
])
if
default
is
not
None
:
os
.
environ
[
env_var
]
=
str
(
default
)
return
default
\ No newline at end of file
return
default
This diff is collapsed.
Click to expand it.
tests/test_common/test_file_operations.py
0 → 100644
+
116
−
0
View file @
16f83e67
"""
Tests for File Operations
"""
from
pathlib
import
Path
import
json
import
pytest
import
os
from
common.file_operations
import
(
path_exists
,
create_directory
,
delete_directory
,
tree
,
delete_file
,
read_file
,
append_file
,
write_file
,
)
@pytest.fixture
def
setup_file
(
tmp_path
):
"""
Create a temporary file for testing.
"""
d
=
tmp_path
/
"
sub
"
d
.
mkdir
()
p
=
d
/
"
test.txt
"
p
.
write_text
(
"
content
"
)
return
p
@pytest.fixture
def
setup_directory
(
tmp_path
):
"""
Create a temporary directory for testing.
"""
d
=
tmp_path
/
"
subdir
"
d
.
mkdir
()
return
d
@pytest.fixture
def
setup_populated_directory
(
tmp_path
):
"""
Create a temporary directory for testing.
"""
d
=
tmp_path
/
"
subdir
"
d
.
mkdir
()
p
=
d
/
"
subsubdir
"
p
.
mkdir
()
f
=
d
/
"
myfile.txt
"
f
.
write_text
(
"
foo
"
)
return
d
def
test_path_exists
(
setup_file
):
"""
Test that the path exists function works.
"""
assert
path_exists
(
setup_file
)
is
True
assert
path_exists
(
"
nonexistent_file
"
)
is
False
def
test_create_directory
(
tmp_path
,
setup_directory
):
"""
Test that you can create a directory.
"""
assert
(
create_directory
(
str
(
setup_directory
))
==
f
"
{
str
(
setup_directory
)
}
already exists.
"
)
new_dir
=
tmp_path
/
"
newdir
"
assert
create_directory
(
str
(
new_dir
))
is
True
def
test_delete_directory
(
setup_directory
):
"""
Test that deleting a directory works as expected.
"""
assert
(
delete_directory
(
str
(
setup_directory
))
==
f
"
{
str
(
setup_directory
)
}
deleted successfully.
"
)
assert
delete_directory
(
"
nodir
"
)
==
f
"
nodir does not exist.
"
def
test_tree
(
setup_populated_directory
,
setup_file
,
capsys
):
"""
Test that the tree command works as expected.
"""
print
(
setup_populated_directory
)
tree
(
str
(
setup_populated_directory
))
captured
=
capsys
.
readouterr
()
print
(
captured
)
assert
"
Directory
"
in
captured
.
out
assert
"
File
"
in
captured
.
out
def
test_read_file
(
setup_file
):
"""
Test reading an existing file.
"""
assert
read_file
(
str
(
setup_file
))
==
"
content
"
assert
read_file
(
"
nonexistent
"
)
is
None
def
test_append_file
(
setup_file
):
"""
Test that appending to a file works.
"""
content_to_append
=
"
is my favorite content
"
append_file
(
str
(
setup_file
),
content_to_append
)
assert
setup_file
.
read_text
()
==
"
content is my favorite content
"
def
test_write_file
(
tmp_path
):
"""
Test that writing a file works.
"""
file_path
=
tmp_path
/
"
write_test.txt
"
write_file
(
str
(
file_path
),
"
new content
"
)
assert
file_path
.
read_text
()
==
"
new content
"
json_path
=
tmp_path
/
"
path.json
"
dictionary
=
{
"
key
"
:
"
value
"
}
dict_str
=
str
(
dictionary
).
replace
(
"'"
,
'"'
)
json_path
.
write_text
(
dict_str
)
assert
json_path
.
read_text
()
==
dict_str
# TODO @Bhaskar, fix this so that the write_file method works here
# dict_path = tmp_path / "dict.json"
# write_file(str(dict_path), dictionary)
# assert dict_path.read_text() == dict_str
# write_file('new_dict', dictionary | dictionary2)
def
test_delete_file
(
setup_file
):
"""
Test deletion of an existing file.
"""
assert
delete_file
(
setup_file
)
==
f
"
File:
{
str
(
setup_file
)
}
deleted successfully.
"
assert
delete_file
(
"
nonexistent
"
)
==
f
"
Error in deleting the file: nonexistent.
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment