File: cli_test.py

package info (click to toggle)
tldextract 5.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 532 kB
  • sloc: python: 1,644; makefile: 10
file content (88 lines) | stat: -rw-r--r-- 2,302 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
"""tldextract integration tests."""

import json
import sys

import pytest

from tldextract.cli import main
from tldextract.tldextract import PUBLIC_SUFFIX_LIST_URLS


def test_cli_no_input(monkeypatch: pytest.MonkeyPatch) -> None:
    """Test CLI without args."""
    monkeypatch.setattr(sys, "argv", ["tldextract"])
    with pytest.raises(SystemExit) as ex:
        main()

    assert ex.value.code == 1


def test_cli_parses_args(monkeypatch: pytest.MonkeyPatch) -> None:
    """Test CLI with nonsense args."""
    monkeypatch.setattr(sys, "argv", ["tldextract", "--some", "nonsense"])
    with pytest.raises(SystemExit) as ex:
        main()

    assert ex.value.code == 2


def test_cli_posargs(
    capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch
) -> None:
    """Test CLI with basic, positional args."""
    monkeypatch.setattr(
        sys, "argv", ["tldextract", "example.com", "bbc.co.uk", "forums.bbc.co.uk"]
    )

    main()

    stdout, stderr = capsys.readouterr()
    assert not stderr
    assert stdout == " example com\n bbc co.uk\nforums bbc co.uk\n"


def test_cli_namedargs(
    capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch
) -> None:
    """Test CLI with basic, positional args, and that it parses an optional argument (though it doesn't change output)."""
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "tldextract",
            "--suffix_list_url",
            PUBLIC_SUFFIX_LIST_URLS[0],
            "example.com",
            "bbc.co.uk",
            "forums.bbc.co.uk",
        ],
    )

    main()

    stdout, stderr = capsys.readouterr()
    assert not stderr
    assert stdout == " example com\n bbc co.uk\nforums bbc co.uk\n"


def test_cli_json_output(
    capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch
) -> None:
    """Test CLI with --json option."""
    monkeypatch.setattr(sys, "argv", ["tldextract", "--json", "www.bbc.co.uk"])

    main()

    stdout, stderr = capsys.readouterr()
    assert not stderr
    assert json.loads(stdout) == {
        "subdomain": "www",
        "domain": "bbc",
        "suffix": "co.uk",
        "fqdn": "www.bbc.co.uk",
        "ipv4": "",
        "ipv6": "",
        "is_private": False,
        "registered_domain": "bbc.co.uk",
    }