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
|
from __future__ import annotations
import unittest
from os import pardir, path, remove
from os.path import exists
from unittest.mock import patch
from charset_normalizer.cli import cli_detect, query_yes_no
DIR_PATH = path.join(path.dirname(path.realpath(__file__)), pardir)
class TestCommandLineInterface(unittest.TestCase):
@patch("builtins.input", lambda *args: "y")
def test_simple_yes_input(self):
self.assertTrue(query_yes_no("Are u willing to chill a little bit ?"))
@patch("builtins.input", lambda *args: "N")
def test_simple_no_input(self):
self.assertFalse(query_yes_no("Are u willing to chill a little bit ?"))
def test_single_file(self):
self.assertEqual(0, cli_detect([DIR_PATH + "/data/sample-arabic-1.txt"]))
def test_version_output_success(self):
with self.assertRaises(SystemExit):
cli_detect(["--version"])
def test_single_file_normalize(self):
self.assertEqual(
0, cli_detect([DIR_PATH + "/data/sample-arabic-1.txt", "--normalize"])
)
self.assertTrue(exists(DIR_PATH + "/data/sample-arabic-1.cp1256.txt"))
try:
remove(DIR_PATH + "/data/sample-arabic-1.cp1256.txt")
except:
pass
def test_single_verbose_file(self):
self.assertEqual(
0, cli_detect([DIR_PATH + "/data/sample-arabic-1.txt", "--verbose"])
)
def test_multiple_file(self):
self.assertEqual(
0,
cli_detect(
[
DIR_PATH + "/data/sample-arabic-1.txt",
DIR_PATH + "/data/sample-french.txt",
DIR_PATH + "/data/sample-chinese.txt",
]
),
)
def test_with_alternative(self):
self.assertEqual(
0,
cli_detect(
[
"-a",
DIR_PATH + "/data/sample-arabic-1.txt",
DIR_PATH + "/data/sample-french.txt",
DIR_PATH + "/data/sample-chinese.txt",
]
),
)
def test_with_minimal_output(self):
self.assertEqual(
0,
cli_detect(
[
"-m",
DIR_PATH + "/data/sample-arabic-1.txt",
DIR_PATH + "/data/sample-french.txt",
DIR_PATH + "/data/sample-chinese.txt",
]
),
)
def test_with_minimal_and_alt(self):
self.assertEqual(
0,
cli_detect(
[
"-m",
"-a",
DIR_PATH + "/data/sample-arabic-1.txt",
DIR_PATH + "/data/sample-french.txt",
DIR_PATH + "/data/sample-chinese.txt",
]
),
)
def test_non_existent_file(self):
with self.assertRaises(SystemExit) as cm:
cli_detect([DIR_PATH + "/data/not_found_data.txt"])
self.assertEqual(cm.exception.code, 2)
def test_replace_without_normalize(self):
self.assertEqual(
cli_detect([DIR_PATH + "/data/sample-arabic-1.txt", "--replace"]), 1
)
def test_force_replace_without_replace(self):
self.assertEqual(
cli_detect([DIR_PATH + "/data/sample-arabic-1.txt", "--force"]), 1
)
if __name__ == "__main__":
unittest.main()
|