Skip to content

Commit

Permalink
test: added cli testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaJack committed Feb 4, 2024
1 parent 867a5c4 commit f551173
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 34 deletions.
115 changes: 83 additions & 32 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

# capture stderr to variable
# https://stackoverflow.com/a/61533524/13448666
import io
import contextlib
from io import StringIO
from contextlib import redirect_stderr
from contextlib import redirect_stdout


# current directory
from pathlib import Path
Expand All @@ -34,33 +36,82 @@

# module to test
from toc.toc import Toc
from toc.cli import main, parse_args
from toc.cli import *
#from toc.__version__ import __version__

# ################################################################ TEST CLASSES

"""
# ################################ MOCK SETUP

class MockArgs():
def __init__(self):
self.from_list = False
self.output_file = None
self.files = ['empty.py']
self.character = None
self.line_numbers = False
self.to_file = False
#class MockArgs():
# def __init__(self):
# self.from_list = False
# self.output_file = None
# self.files = ['empty.py']
# self.character = None
# self.line_numbers = False
# self.to_file = False


# ################################ CLI TESTS


class TestCli(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.t = project_root / "tests"
# project folder
cls.p = cls.t.parent
cls.i = cls.t / "input"
#cls.o = cls.t / "output"

def test_no_args(self):
test_args = [f"{self.p / 'toc' / 'cli.py'}"]
with patch.object(sys, "argv", test_args):
output = StringIO()
with redirect_stderr(output):
main()
print("output: '" + output.getvalue().strip() + "'")
self.assertEqual("No files provided\n", output.getvalue())

# returns SystemExit: 0 from argparse module with "-v"
#def test_version(self):
# test_args = ["toc/cli.py", "-v"]
# with patch.object(sys, "argv", test_args):
# output = StringIO()
# with redirect_stdout(output):
# main()
# print("output: '" + output.getvalue().strip() + "'")
# self.assertTrue(f'toc {__version__}' in output.getvalue())

# fails if not project root
def test_empty(self):
test_args = [f"{self.p / 'toc' / 'cli.py'}", f"{self.i / 'go_empty.go'}"]
with patch.object(sys, "argv", test_args):
output = StringIO()
with redirect_stderr(output):
main()
#print("output: '" + output.getvalue().strip() + "'")
self.assertIn('No "//" toc for "', output.getvalue())

def test_list(self):
test_args = [f"{self.p / 'toc' / 'cli.py'}", "-l", f"{self.p / '.tocfiles'}"]
with patch.object(sys, "argv", test_args):
output = StringIO()
with redirect_stdout(output):
main()
#print("output: '" + output.getvalue() + "'")
self.assertIn("Contents of README.md", output.getvalue())

"""
#@patch('builtins.open', new_callable=mock_open, read_data="test\n")
@patch('toc.cli.parse_args')
@patch('toc.toc.Toc')
#def test_main(self, mock_toc, mock_args, mock_files):
def test_nonexisting_file(self, mock_toc, mock_args):
f = io.StringIO()
with contextlib.redirect_stderr(f):
f = StringIO()
with redirect_stderr(f):
mock_args.return_value = MockArgs()
#mock_toc.return_value = MockToc(mock_args.files)
main()
Expand All @@ -70,38 +121,38 @@ def test_nonexisting_file(self, mock_toc, mock_args):
@patch('toc.cli.parse_args')
@patch('toc.toc.Toc')
def test_empty_file(self, mock_toc, mock_args, mock_files):
f = io.StringIO()
with contextlib.redirect_stderr(f):
f = StringIO()
with redirect_stderr(f):
mock_args.return_value = MockArgs()
main()
print(f.getvalue())
# if main() has run without errors, this will pass
#self.assertTrue(True)
self.assertTrue(f'Skipping empty "#" toc for empty.py' in f.getvalue())
def test_nonexisting_list(self):
with patch("sys.argv", ["-l", "empty.list"]):
#print(sys.argv)
f = io.StringIO()
f = StringIO()
with contextlib.redirect_stderr(f):
args = parse_args()
main()
self.assertTrue(f'No files provided' in f.getvalue())
@patch('builtins.open', new_callable=mock_open, read_data="test\n")
@patch('toc.cli.parse_args')
@patch('toc.toc.Toc')
def test_empty_list(self):
with patch("sys.argv", ["-l", "empty.list"]):
#print(sys.argv)
f = io.StringIO()
with contextlib.redirect_stderr(f):
mock_args.return_value = MockArgs()
main()
self.assertTrue(f'No files provided' in f.getvalue())
"""
#@patch('builtins.open', new_callable=mock_open, read_data="test\n")
#@patch('toc.cli.parse_args')
#@patch('toc.toc.Toc')
#def test_empty_list(self):
# with patch("sys.argv", ["-l", "empty.list"]):
# #print(sys.argv)
# f = StringIO()
# with contextlib.redirect_stderr(f):
# mock_args.return_value = MockArgs()
# main()
# self.assertTrue(f'No files provided' in f.getvalue())
"""
# ################################################################ ENTRYPOINT

if __name__ == "__main__":
unittest.main()

unittest.main(buffer=True)
4 changes: 2 additions & 2 deletions tests/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def setUpClass(cls):
def test_input_files(self):
# process each file in the input_dir
for file in Path.iterdir(self.input_dir):
print(f"\nProcessing {file.name}")
#print(f"\nProcessing {file.name}")
input_file = self.input_dir / file.name
output_file = self.output_dir / file.name
reference_file = self.reference_dir / file.name
Expand Down Expand Up @@ -191,5 +191,5 @@ def test_input_files(self):
# ################################################################ ENTRYPOINT

if __name__ == "__main__":
unittest.main()
unittest.main(buffer=True)

1 change: 1 addition & 0 deletions toc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def process_file(inputFile, args):

def main():
# parse arguments
#print(sys.argv)
args = parse_args()
files = get_files(args)
# process all files individually
Expand Down

0 comments on commit f551173

Please sign in to comment.