Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 Functions added #3578

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tests/test_get_env_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import unittest

branch_coverage = {
"branch_1": False,
"branch_2": False
}

def get_env_int( name, default):
v = os.environ.get( name)
if v is None:
branch_coverage["branch_1"] = True
ret = default
else:
branch_coverage["branch_2"] = True
ret = int(v)
return ret

def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")


def test_get_env_int_default_is_not_None_returns_1():
os.environ['VAR'] = '1'
result = get_env_int('VAR', 1)
print_coverage()

def test_get_env_int_default_is_not_None_returns_0():
os.environ['VAR'] = '0'
result = get_env_int('VAR', 1)
print_coverage()

def test_get_env_int_default_isNone_returns_default():
del os.environ['VAR']
result = get_env_int('VAR', 0)
print_coverage()

test_get_env_int_default_is_not_None_returns_1()
test_get_env_int_default_is_not_None_returns_0()
test_get_env_int_default_isNone_returns_default()
66 changes: 66 additions & 0 deletions tests/test_set_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import unittest

branch_coverage = {
"branch_1": False,
"branch_2": False,
"branch_3": False,
"branch_4": False,
"branch_5": False
}

def _set_stream(name, default):
t = os.environ.get(name)
if t is None:
branch_coverage["branch_1"] = True
return default
elif t.startswith('fd:'):
branch_coverage["branch_2"] = True
return open(int(t[3:]), mode='w', closefd=False)
elif t.startswith('path:'):
branch_coverage["branch_3"] = True
return open(t[5:], 'w')
elif t.startswith('path+:'):
branch_coverage["branch_4"] = True
return open(t[6:], 'a')
else:
branch_coverage["branch_5"] = True
raise Exception(f'Unrecognised stream specification for {name!r} should match `fd:<int>`, `path:<string>` or `path+:<string>`: {t!r}')

def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")

def test_set_stream_name_startsWithFd_opensFile():
os.environ['VAR'] = 'fd:1'
result = _set_stream('VAR', "")
print_coverage()

def test_set_stream_name_startsWithPath_opensFile():
os.environ['VAR'] = 'path:example.txt'
result = _set_stream('VAR', "")
print_coverage()

def test_set_stream_name_startsWithPathPlus_opensFile():
os.environ['VAR'] = 'path+:example.txt'
result = _set_stream('VAR', "")
print_coverage()

def test_set_stream_name_unrecognized_raisesException():
os.environ['VAR'] = 'unrecognized'
try:
result = _set_stream('VAR', "")
except Exception as e:
print(e)
print_coverage()

def test_set_stream_name_isNone_returns_default():
del os.environ['VAR']
result = _set_stream('VAR', 0)
print_coverage()

test_set_stream_name_startsWithFd_opensFile()
test_set_stream_name_startsWithPath_opensFile()
test_set_stream_name_startsWithPathPlus_opensFile()
test_set_stream_name_unrecognized_raisesException()
test_set_stream_name_isNone_returns_default()
Loading