1import tempfile
2import shutil
3import sys
4from unittest import mock
5
6import pytest
7
8from tools.wpt import run
9from tools import localpaths  # noqa: F401
10from wptrunner.browsers import product_list
11
12
13@pytest.fixture(scope="module")
14def venv():
15    from tools.wpt import virtualenv
16
17    class Virtualenv(virtualenv.Virtualenv):
18        def __init__(self):
19            self.path = tempfile.mkdtemp()
20            self.skip_virtualenv_setup = False
21
22        def create(self):
23            return
24
25        def activate(self):
26            return
27
28        def start(self):
29            return
30
31        def install(self, *requirements):
32            return
33
34        def install_requirements(self, requirements_path):
35            return
36
37    venv = Virtualenv()
38    yield venv
39
40    shutil.rmtree(venv.path)
41
42
43@pytest.fixture(scope="module")
44def logger():
45    run.setup_logging({})
46
47
48@pytest.mark.parametrize("platform", ["Windows", "Linux", "Darwin"])
49def test_check_environ_fail(platform):
50    m_open = mock.mock_open(read_data=b"")
51
52    with mock.patch.object(run, "open", m_open):
53        with mock.patch.object(run.platform, "uname",
54                               return_value=(platform, "", "", "", "", "")):
55            with pytest.raises(run.WptrunError) as excinfo:
56                run.check_environ("foo")
57
58    assert "wpt make-hosts-file" in str(excinfo.value)
59
60
61@pytest.mark.parametrize("product", product_list)
62def test_setup_wptrunner(venv, logger, product):
63    if product == "firefox_android":
64        pytest.skip("Android emulator doesn't work on docker")
65    parser = run.create_parser()
66    kwargs = vars(parser.parse_args(["--channel=nightly", product]))
67    kwargs["prompt"] = False
68    # Hack to get a real existing path
69    kwargs["binary"] = sys.argv[0]
70    kwargs["webdriver_binary"] = sys.argv[0]
71    if kwargs["product"] == "sauce":
72        kwargs["sauce_browser"] = "firefox"
73        kwargs["sauce_version"] = "63"
74    run.setup_wptrunner(venv, **kwargs)
75