def test_main_line_length_pass(make_test_file, capsys): line_data = [ "# first line is a comment. some more random stuff to say here" " but not going to say it\n", "import time\n", "\n", "\n", "def main(): # this is the main function where everything runs in" " runs in this function when we call it\n", " print('hello')\n", " sleep(1)\n", " print('world')\n", " print('print this string out to see what it looks like" "on screen to see\n" "\n", "\n", "main()\n", "\n", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) expected_cap = "Line Checker\n1 files checked: \033[1;32mPassed\033[0m\n" # details = f"{tf}\n{details}" result = line_checker.main([tf, "-l105"]) captured_output = capsys.readouterr().out assert captured_output == expected_cap assert result == 0
def test_main_multiple_files_passed(capsys, make_temp_directory): line_data = [ "# first line is a comment\n", "\n", "\n", "def main():\n", " print('hello world')\n", "\n", "\n", "main()\n", "\n", ] file_data = "".join(line_data) td = make_temp_directory() td.add_empty_file("README.md") td.add_file("foo.py", file_data) td.add_file("bar.py", file_data) td.add_empty_file("foobar.py") td.add_empty_file("test") td.add_empty_file("data") td.add_directories_root(["tests"]) test_dir = td.get_temp_directory() expected_cap = "Line Checker\n3 files checked: \033[1;32mPassed\033[0m\n" result = line_checker.main([test_dir]) captured_output = capsys.readouterr().out assert captured_output == expected_cap assert result == 0
def test_main_multiple_files_one_fail(capsys, make_temp_directory): line_data = [ "# first line is a comment and someday this long line will be " " removed from this file\n", "\n", "\n", "def main():\n", " print('hello world')\n", "\n", "\n", "# this is the main function call. some more random stuff to fill " "this line with to get it over\n" "main()\n", "\n", ] file_data = "".join(line_data) td = make_temp_directory() td.add_empty_file("README.md") td.add_file("foo.py", file_data) td.add_file("bar.py", "# new file to add\nprint('hello world')\n\n") td.add_empty_file("foobar.py") td.add_empty_file("test") td.add_empty_file("data") td.add_directories_root(["tests"]) test_dir = td.get_temp_directory() summary = "Line Checker\n3 files checked: 2 \033[1;32mPassed\033[0m," summary += " 1 \033[1;31mFailed\033[0m\n" details = f"{test_dir + '/' + 'foo.py'}\n line: 1 - length: 84\n" details += f" line: 8 - length: 95\n" expected_capture = summary + details result = line_checker.main([test_dir]) captured_output = capsys.readouterr().out assert captured_output == expected_capture assert result == 0
def test_main_one_file_failed(capsys, make_test_file): line_data = [ "# first line is a comment\n", "import time\n", "\n", "\n", "def main(): # this is the main function where everything runs in" " runs in this function when we call it\n", " print('hello')\n", " sleep(1)\n", " print('world')\n", "\n", "\n", "main()\n", "\n", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) summary = "Line Checker\n1 files checked: \033[1;31mFailed\033[0m\n" details = f"{tf}\n line: 5 - length: 103\n" expected_capture = summary + details result = line_checker.main([tf]) captured_output = capsys.readouterr().out assert captured_output == expected_capture assert result == 0
def test_main_file_not_found(tmpdir, capsys): expected_cap = """Line Checker \033[1;31mError file not found during discovery\033[0m 0 files checked\n""" td = tmpdir.join("foo.py") result = line_checker.main([td.strpath]) captured_output = capsys.readouterr().out assert captured_output == expected_cap assert result == 1
def test_main_not_python_file(capsys, make_test_file): line_data = ["line 1 of a text file\n", "\n", "another line\n"] file_data = "".join(line_data) tf = make_test_file("foo.txt", file_data) expected_cap = "Line Checker\n0 files checked\n" result = line_checker.main([tf]) captured_output = capsys.readouterr().out assert captured_output == expected_cap assert result == 0
def test_main_file_save_to_file_out_file(make_test_file): line_data = [ "# line 1 comment line", "print('hello world')", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) result = line_checker.main([tf, "-S", "--out_file", "test_out"]) with open("test_out", "r") as f: data = f.read() assert data == "line checker\n1 file checked: passed\n" assert result == 0
def test_main_file_passed_quiet_mode(make_test_file, capsys): line_data = [ "# line 1 comment line", "print('hello world')", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) expected_capture = "" result = line_checker.main([tf, "-q"]) captured_output = capsys.readouterr().out assert captured_output == expected_capture assert result == 0
def test_main_file_failed_quiet_mode(make_test_file, capsys): line_data = [ "# line 1 comment line", "print('hello world how are you. foo.py is an test example of a" " line that is too long')" ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) expected_capture = "1 files checked: \033[1;31mFailed\033[0m\n" expected_capture += f"{tf}\n line: 1 - length: 108\n" result = line_checker.main([tf, "-q"]) captured_output = capsys.readouterr().out assert captured_output == expected_capture assert result == 0
def test_main_multiple_files_no_check(capsys, make_temp_directory): td = make_temp_directory() td.add_empty_file("README.md") td.add_empty_file("foo.txt") td.add_empty_file("bar.c") td.add_empty_file("test") td.add_empty_file("data") td.add_directories_root(["tests"]) test_dir = td.get_temp_directory() expected_cap = "Line Checker\n0 files checked\n" result = line_checker.main([test_dir]) captured_output = capsys.readouterr().out assert captured_output == expected_cap assert result == 0
def test_save_results_to_file_passed(make_test_file): line_data = [ "# line 1 comment line", "print('hello world')", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) test_date_time = "2020-01-01-142020" with mock.patch.object(line_checker.time, "strftime", return_value=test_date_time): result = line_checker.main([tf, "-S"]) with open(f"line_checker_out{test_date_time}", "r") as f: data = f.read() assert data == "line checker\n1 file checked: passed\n" assert result == 0
def test_main_file_passed_quiet_mode_save_to_file(make_test_file, capsys): line_data = [ "# line 1 comment line", "print('hello world')", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) expected_capture = "" test_date_time = "2020-01-01-142020" with mock.patch.object(line_checker.time, "strftime", return_value=test_date_time): result = line_checker.main([tf, "-q", "-S"]) captured_output = capsys.readouterr().out assert captured_output == expected_capture assert result == 0 with open(f"line_checker_out{test_date_time}", "r") as f: data = f.read() assert data == "line checker\n1 file checked: passed\n"
def test_save_results_to_file_fail(make_test_file): line_data = [ "# line 1 comment line", "print('hello world how are you. foo.py is an test example of a" " line that is too long')" ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) test_date_time = "2020-01-01-142020" with mock.patch.object(line_checker.time, "strftime", return_value=test_date_time): result = line_checker.main([tf, "-S"]) expected = "line checker\n1 file checked: failed\n" expected += f"{tf}\n line 1 - length: 108\n" with open(f"line_checker_out{test_date_time}", "r") as f: data = f.read() assert data == expected assert result == 0
def test_main_file_failed_quiet_mode_save_to_file(make_test_file, capsys): line_data = [ "# line 1 comment line", "print('hello world how are you. foo.py is an test example of a" " line that is too long')" ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) expected_capture = "1 files checked: \033[1;31mFailed\033[0m\n" expected_capture += f"{tf}\n line: 1 - length: 108\n" test_date_time = "2020-01-01-142020" with mock.patch.object(line_checker.time, "strftime", return_value=test_date_time): result = line_checker.main([tf, "-q", "-S"]) captured_output = capsys.readouterr().out assert captured_output == expected_capture assert result == 0 expected = "line checker\n1 file checked: failed\n" expected += f"{tf}\n line 1 - length: 108\n" with open(f"line_checker_out{test_date_time}", "r") as f: data = f.read() assert data == expected
def test_main_one_file_passed(capsys, make_test_file, options, expected_cap): line_data = [ "# first line is a comment\n", "import time\n", "\n", "\n", "def main():\n", " print('hello')\n", " sleep(1)\n", " print('world')\n", "\n", "\n", "main()\n", "\n", ] file_data = "".join(line_data) tf = make_test_file("foo.py", file_data) # expected_cap = "Line Checker\n1 files checked: \033[1;32mPassed\033[0m\n" result = line_checker.main([tf] + options) captured_output = capsys.readouterr().out assert captured_output == expected_cap assert result == 0