-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_utils_cli.py
79 lines (66 loc) · 2.35 KB
/
test_utils_cli.py
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
import os
import unittest
from unittest import mock
from huggingface_hub.commands._cli_utils import ANSI, tabulate
class TestCLIUtils(unittest.TestCase):
@mock.patch.dict(os.environ, {}, clear=True)
def test_ansi_utils(self) -> None:
"""Test `ANSI` works as expected."""
self.assertEqual(
ANSI.bold("this is bold"),
"\x1b[1mthis is bold\x1b[0m",
)
self.assertEqual(
ANSI.gray("this is gray"),
"\x1b[90mthis is gray\x1b[0m",
)
self.assertEqual(
ANSI.red("this is red"),
"\x1b[1m\x1b[31mthis is red\x1b[0m",
)
self.assertEqual(
ANSI.gray(ANSI.bold("this is bold and grey")),
"\x1b[90m\x1b[1mthis is bold and grey\x1b[0m\x1b[0m",
)
@mock.patch.dict(os.environ, {"NO_COLOR": "1"}, clear=True)
def test_ansi_no_color(self) -> None:
"""Test `ANSI` respects `NO_COLOR` env var."""
self.assertEqual(
ANSI.bold("this is bold"),
"this is bold",
)
self.assertEqual(
ANSI.gray("this is gray"),
"this is gray",
)
self.assertEqual(
ANSI.red("this is red"),
"this is red",
)
self.assertEqual(
ANSI.gray(ANSI.bold("this is bold and grey")),
"this is bold and grey",
)
def test_tabulate_utility(self) -> None:
"""Test `tabulate` works as expected."""
rows = [[1, 2, 3], ["a very long value", "foo", "bar"], ["", 123, 456]]
headers = ["Header 1", "something else", "a third column"]
self.assertEqual(
tabulate(rows=rows, headers=headers),
"Header 1 something else a third column \n"
"----------------- -------------- -------------- \n"
" 1 2 3 \n"
"a very long value foo bar \n"
" 123 456 ",
)
def test_tabulate_utility_with_too_short_row(self) -> None:
"""
Test `tabulate` throw IndexError when a row has less values than the header
list.
"""
self.assertRaises(
IndexError,
tabulate,
rows=[[1]],
headers=["Header 1", "Header 2"],
)