1# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2
3# Copyright 2020-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
4#
5# This file is part of qutebrowser.
6#
7# qutebrowser is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# qutebrowser is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.
19
20"""Tests for qutebrowser.qutebrowser.
21
22(Mainly commandline flag parsing)
23"""
24
25import re
26
27import pytest
28
29from qutebrowser import qutebrowser
30
31
32@pytest.fixture
33def parser():
34    return qutebrowser.get_argparser()
35
36
37class TestDebugFlag:
38
39    def test_valid(self, parser):
40        args = parser.parse_args(['--debug-flag', 'chromium',
41                                  '--debug-flag', 'stack'])
42        assert args.debug_flags == ['chromium', 'stack']
43
44    def test_invalid(self, parser, capsys):
45        with pytest.raises(SystemExit):
46            parser.parse_args(['--debug-flag', 'invalid'])
47
48        _out, err = capsys.readouterr()
49        assert 'Invalid debug flag - valid flags:' in err
50
51
52class TestLogFilter:
53
54    def test_valid(self, parser):
55        args = parser.parse_args(['--logfilter', 'misc'])
56        assert args.logfilter == 'misc'
57
58    def test_invalid(self, parser, capsys):
59        with pytest.raises(SystemExit):
60            parser.parse_args(['--logfilter', 'invalid'])
61
62        _out, err = capsys.readouterr()
63        print(err)
64        assert 'Invalid log category invalid - valid categories' in err
65
66
67class TestJsonArgs:
68
69    def test_partial(self, parser):
70        """Make sure we can provide a subset of all arguments.
71
72        This ensures that it's possible to restart into an older version of qutebrowser
73        when a new argument was added.
74        """
75        args = parser.parse_args(['--json-args', '{"debug": true}'])
76        args = qutebrowser._unpack_json_args(args)
77        # pylint: disable=no-member
78        assert args.debug
79        assert not args.temp_basedir
80
81
82class TestValidateUntrustedArgs:
83
84    @pytest.mark.parametrize('args', [
85        [],
86        [':nop'],
87        [':nop', '--untrusted-args'],
88        [':nop', '--debug', '--untrusted-args'],
89        [':nop', '--untrusted-args', 'foo'],
90        ['--debug', '--untrusted-args', 'foo'],
91        ['foo', '--untrusted-args', 'bar'],
92    ])
93    def test_valid(self, args):
94        qutebrowser._validate_untrusted_args(args)
95
96    @pytest.mark.parametrize('args, message', [
97        (
98            ['--untrusted-args', '--debug'],
99            "Found --debug after --untrusted-args, aborting.",
100        ),
101        (
102            ['--untrusted-args', ':nop'],
103            "Found :nop after --untrusted-args, aborting.",
104        ),
105        (
106            ['--debug', '--untrusted-args', '--debug'],
107            "Found --debug after --untrusted-args, aborting.",
108        ),
109        (
110            [':nop', '--untrusted-args', '--debug'],
111            "Found --debug after --untrusted-args, aborting.",
112        ),
113        (
114            [':nop', '--untrusted-args', ':nop'],
115            "Found :nop after --untrusted-args, aborting.",
116        ),
117        (
118            [
119                ':nop',
120                '--untrusted-args',
121                ':nop',
122                '--untrusted-args',
123                'https://www.example.org',
124            ],
125            (
126                "Found multiple arguments (:nop --untrusted-args "
127                "https://www.example.org) after --untrusted-args, aborting."
128            )
129        ),
130        (
131            ['--untrusted-args', 'okay1', 'okay2'],
132            "Found multiple arguments (okay1 okay2) after --untrusted-args, aborting.",
133        ),
134    ])
135    def test_invalid(self, args, message):
136        with pytest.raises(SystemExit, match=re.escape(message)):
137            qutebrowser._validate_untrusted_args(args)
138