1#!/usr/bin/env python
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5import mozunit
6import pytest
7
8from mozperftest.script import (
9    BadOptionTypeError,
10    ScriptInfo,
11    MissingFieldError,
12    ScriptType,
13    ParseError,
14)
15from mozperftest.tests.support import (
16    EXAMPLE_TEST,
17    HERE,
18    EXAMPLE_XPCSHELL_TEST,
19    EXAMPLE_XPCSHELL_TEST2,
20)
21
22
23def check_options(info):
24    assert info["options"]["default"]["perfherder"]
25    assert info["options"]["linux"]["perfherder_metrics"] == [
26        {"name": "speed", "unit": "bps_lin"}
27    ]
28    assert info["options"]["win"]["perfherder_metrics"] == [
29        {"name": "speed", "unit": "bps_win"}
30    ]
31    assert info["options"]["mac"]["perfherder_metrics"] == [
32        {"name": "speed", "unit": "bps_mac"}
33    ]
34
35
36def test_scriptinfo_bt():
37    info = ScriptInfo(EXAMPLE_TEST)
38    assert info["author"] == "N/A"
39    display = str(info)
40    assert "The description of the example test." in display
41    assert info.script_type == ScriptType.browsertime
42    check_options(info)
43
44
45@pytest.mark.parametrize("script", [EXAMPLE_XPCSHELL_TEST, EXAMPLE_XPCSHELL_TEST2])
46def test_scriptinfo_xpcshell(script):
47    info = ScriptInfo(script)
48    assert info["author"] == "N/A"
49
50    display = str(info)
51    assert "The description of the example test." in display
52    assert info.script_type == ScriptType.xpcshell
53    check_options(info)
54
55
56def test_scriptinfo_failure():
57    bad_example = HERE / "data" / "failing-samples" / "perftest_doc_failure_example.js"
58    with pytest.raises(MissingFieldError):
59        ScriptInfo(bad_example)
60
61
62def test_parserror():
63    exc = Exception("original")
64    error = ParseError("script", exc)
65    assert error.exception is exc
66    assert "original" in str(error)
67
68
69def test_update_args():
70    args = {"perfherder_metrics": [{"name": "yey"}]}
71    info = ScriptInfo(EXAMPLE_TEST)
72    new_args = info.update_args(**args)
73
74    # arguments should not be overriden
75    assert new_args["perfherder_metrics"] == [{"name": "yey"}]
76
77    # arguments in platform-specific options should
78    # override default options
79    assert new_args["verbose"]
80
81
82def test_update_args_metrics_list_failure():
83    args = {"perfherder_metrics": "yey"}
84    info = ScriptInfo(EXAMPLE_TEST)
85
86    with pytest.raises(BadOptionTypeError):
87        info.update_args(**args)
88
89
90def test_update_args_metrics_json_failure():
91    args = {"perfherder_metrics": ["yey"]}
92    info = ScriptInfo(EXAMPLE_TEST)
93
94    with pytest.raises(BadOptionTypeError):
95        info.update_args(**args)
96
97
98if __name__ == "__main__":
99    mozunit.main()
100