File: test_utils.py

package info (click to toggle)
eyed3 0.9.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,380 kB
  • sloc: python: 13,282; makefile: 411; sh: 69
file content (119 lines) | stat: -rw-r--r-- 3,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from unittest.mock import MagicMock, call

import eyed3.utils.console
from eyed3.utils import walk
from eyed3.utils.console import (
    printMsg, printWarning, printHeader, Fore, WARNING_COLOR, HEADER_COLOR
)
from . import RedirectStdStreams


def test_printWarning():
    eyed3.utils.console.USE_ANSI = False
    with RedirectStdStreams() as out:
        printWarning("Built To Spill")
    assert (out.stdout.read() == "Built To Spill\n")

    eyed3.utils.console.USE_ANSI = True
    with RedirectStdStreams() as out:
        printWarning("Built To Spill")
    assert (out.stdout.read() == "%sBuilt To Spill%s\n" % (WARNING_COLOR(),
                                                              Fore.RESET))


def test_printMsg():
    eyed3.utils.console.USE_ANSI = False
    with RedirectStdStreams() as out:
        printMsg("EYEHATEGOD")
    assert (out.stdout.read() == "EYEHATEGOD\n")

    eyed3.utils.console.USE_ANSI = True
    with RedirectStdStreams() as out:
        printMsg("EYEHATEGOD")
    assert (out.stdout.read() == "EYEHATEGOD\n")


def test_printHeader():
    eyed3.utils.console.USE_ANSI = False
    with RedirectStdStreams() as out:
        printHeader("Furthur")
    assert (out.stdout.read() == "Furthur\n")

    eyed3.utils.console.USE_ANSI = True
    with RedirectStdStreams() as out:
        printHeader("Furthur")
    assert (out.stdout.read() == "%sFurthur%s\n" % (HEADER_COLOR(),
                                                       Fore.RESET))


def test_walk_recursive(tmpdir):
    root_d = tmpdir.mkdir("Root")
    d1 = root_d.mkdir("d1")
    f1 = d1 / "file1"
    f1.write_text("file1", "utf8")

    _ = root_d.mkdir("d2")
    d3 = root_d.mkdir("d3")

    handler = MagicMock()
    walk(handler, str(root_d), recursive=True)
    handler.handleFile.assert_called_with(str(f1))
    handler.handleDirectory.assert_called_with(str(d1), [f1.basename])

    # Only dirs with files are handled, so...
    f2 = d3 / "Neurosis"
    f2.write_text("Through Silver and Blood", "utf8")
    f3 = d3 / "High on Fire"
    f3.write_text("Surrounded By Thieves", "utf8")

    d4 = d3.mkdir("d4")
    f4 = d4 / "Cross Rot"
    f4.write_text("VII", "utf8")

    handler = MagicMock()
    walk(handler, str(root_d), recursive=True)
    handler.handleFile.assert_has_calls([call(str(f1)),
                                         call(str(f3)),
                                         call(str(f2)),
                                         call(str(f4)),
                                        ], any_order=True)
    handler.handleDirectory.assert_has_calls(
        [call(str(d1), [f1.basename]),
         call(str(d3), [f3.basename, f2.basename]),
         call(str(d4), [f4.basename]),
        ], any_order=True)


def test_walk(tmpdir):
    root_d = tmpdir.mkdir("Root")
    d1 = root_d.mkdir("d1")
    f1 = d1 / "file1"
    f1.write_text("file1", "utf8")

    _ = root_d.mkdir("d2")
    d3 = root_d.mkdir("d3")

    f2 = d3 / "Neurosis"
    f2.write_text("Through Silver and Blood", "utf8")
    f3 = d3 / "High on Fire"
    f3.write_text("Surrounded By Thieves", "utf8")

    d4 = d3.mkdir("d4")
    f4 = d4 / "Cross Rot"
    f4.write_text("VII", "utf8")

    handler = MagicMock()
    walk(handler, str(root_d))
    handler.handleFile.assert_not_called()
    handler.handleDirectory.assert_not_called()

    handler = MagicMock()
    walk(handler, str(root_d / "d1"), recursive=True)
    handler.handleFile.assert_called_with(str(f1))
    handler.handleDirectory.assert_called_with(str(d1), [f1.basename])

    handler = MagicMock()
    walk(handler, str(root_d / "d3"))
    handler.handleFile.assert_has_calls([call(str(f3)), call(str(f2))], any_order=True)
    handler.handleDirectory.assert_has_calls([call(str(d3), [f3.basename, f2.basename])],
                                             any_order=True)