HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/396/platform/gsutil/third_party/charset_normalizer/tests/test_cli.py
import unittest
from charset_normalizer.cli import cli_detect, query_yes_no
from unittest.mock import patch
from os.path import exists
from os import remove, path, pardir

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()