diff --git a/tests/test_cli.py b/tests/test_cli.py index 1f450da..fbb45cd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 @@ -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() @@ -70,10 +121,11 @@ 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()) @@ -81,27 +133,26 @@ def test_empty_file(self, mock_toc, mock_args, mock_files): 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) diff --git a/tests/test_toc.py b/tests/test_toc.py index d5287f3..8b10f82 100644 --- a/tests/test_toc.py +++ b/tests/test_toc.py @@ -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 @@ -191,5 +191,5 @@ def test_input_files(self): # ################################################################ ENTRYPOINT if __name__ == "__main__": - unittest.main() + unittest.main(buffer=True) diff --git a/toc/cli.py b/toc/cli.py index 5b12cdf..522fbed 100755 --- a/toc/cli.py +++ b/toc/cli.py @@ -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