1from unittest.mock import MagicMock, call 2 3import eyed3.utils.console 4from eyed3.utils import walk 5from eyed3.utils.console import ( 6 printMsg, printWarning, printHeader, Fore, WARNING_COLOR, HEADER_COLOR 7) 8from . import RedirectStdStreams 9 10 11def test_printWarning(): 12 eyed3.utils.console.USE_ANSI = False 13 with RedirectStdStreams() as out: 14 printWarning("Built To Spill") 15 assert (out.stdout.read() == "Built To Spill\n") 16 17 eyed3.utils.console.USE_ANSI = True 18 with RedirectStdStreams() as out: 19 printWarning("Built To Spill") 20 assert (out.stdout.read() == "%sBuilt To Spill%s\n" % (WARNING_COLOR(), 21 Fore.RESET)) 22 23 24def test_printMsg(): 25 eyed3.utils.console.USE_ANSI = False 26 with RedirectStdStreams() as out: 27 printMsg("EYEHATEGOD") 28 assert (out.stdout.read() == "EYEHATEGOD\n") 29 30 eyed3.utils.console.USE_ANSI = True 31 with RedirectStdStreams() as out: 32 printMsg("EYEHATEGOD") 33 assert (out.stdout.read() == "EYEHATEGOD\n") 34 35 36def test_printHeader(): 37 eyed3.utils.console.USE_ANSI = False 38 with RedirectStdStreams() as out: 39 printHeader("Furthur") 40 assert (out.stdout.read() == "Furthur\n") 41 42 eyed3.utils.console.USE_ANSI = True 43 with RedirectStdStreams() as out: 44 printHeader("Furthur") 45 assert (out.stdout.read() == "%sFurthur%s\n" % (HEADER_COLOR(), 46 Fore.RESET)) 47 48 49def test_walk_recursive(tmpdir): 50 root_d = tmpdir.mkdir("Root") 51 d1 = root_d.mkdir("d1") 52 f1 = d1 / "file1" 53 f1.write_text("file1", "utf8") 54 55 _ = root_d.mkdir("d2") 56 d3 = root_d.mkdir("d3") 57 58 handler = MagicMock() 59 walk(handler, str(root_d), recursive=True) 60 handler.handleFile.assert_called_with(str(f1)) 61 handler.handleDirectory.assert_called_with(str(d1), [f1.basename]) 62 63 # Only dirs with files are handled, so... 64 f2 = d3 / "Neurosis" 65 f2.write_text("Through Silver and Blood", "utf8") 66 f3 = d3 / "High on Fire" 67 f3.write_text("Surrounded By Thieves", "utf8") 68 69 d4 = d3.mkdir("d4") 70 f4 = d4 / "Cross Rot" 71 f4.write_text("VII", "utf8") 72 73 handler = MagicMock() 74 walk(handler, str(root_d), recursive=True) 75 handler.handleFile.assert_has_calls([call(str(f1)), 76 call(str(f3)), 77 call(str(f2)), 78 call(str(f4)), 79 ], any_order=True) 80 handler.handleDirectory.assert_has_calls( 81 [call(str(d1), [f1.basename]), 82 call(str(d3), [f3.basename, f2.basename]), 83 call(str(d4), [f4.basename]), 84 ], any_order=True) 85 86 87def test_walk(tmpdir): 88 root_d = tmpdir.mkdir("Root") 89 d1 = root_d.mkdir("d1") 90 f1 = d1 / "file1" 91 f1.write_text("file1", "utf8") 92 93 _ = root_d.mkdir("d2") 94 d3 = root_d.mkdir("d3") 95 96 f2 = d3 / "Neurosis" 97 f2.write_text("Through Silver and Blood", "utf8") 98 f3 = d3 / "High on Fire" 99 f3.write_text("Surrounded By Thieves", "utf8") 100 101 d4 = d3.mkdir("d4") 102 f4 = d4 / "Cross Rot" 103 f4.write_text("VII", "utf8") 104 105 handler = MagicMock() 106 walk(handler, str(root_d)) 107 handler.handleFile.assert_not_called() 108 handler.handleDirectory.assert_not_called() 109 110 handler = MagicMock() 111 walk(handler, str(root_d / "d1"), recursive=True) 112 handler.handleFile.assert_called_with(str(f1)) 113 handler.handleDirectory.assert_called_with(str(d1), [f1.basename]) 114 115 handler = MagicMock() 116 walk(handler, str(root_d / "d3")) 117 handler.handleFile.assert_has_calls([call(str(f3)), call(str(f2))], any_order=True) 118 handler.handleDirectory.assert_has_calls([call(str(d3), [f3.basename, f2.basename])], 119 any_order=True) 120