File: test_utils.py

package info (click to toggle)
firefox 145.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,528 kB
  • sloc: cpp: 7,594,999; javascript: 6,459,658; ansic: 3,752,909; python: 1,403,455; xml: 629,809; asm: 438,679; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (111 lines) | stat: -rw-r--r-- 3,350 bytes parent folder | download | duplicates (15)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import sys
import tempfile
from unittest.mock import patch

import mozunit
import pytest
import yaml

# need this so raptor imports work both from /raptor and via mach
here = os.path.abspath(os.path.dirname(__file__))

raptor_dir = os.path.join(os.path.dirname(here), "raptor")
sys.path.insert(0, raptor_dir)

from utils import bool_from_str, transform_platform, write_yml_file


@pytest.mark.parametrize("platform", ["win", "mac", "linux64"])
def test_transform_platform(platform):
    transformed = transform_platform("mitmproxy-rel-bin-{platform}.manifest", platform)
    assert "{platform}" not in transformed
    if platform == "mac":
        assert "osx" in transformed
    else:
        assert platform in transformed


def test_transform_platform_no_change():
    starting_string = "nothing-in-here-to-transform"
    assert transform_platform(starting_string, "mac") == starting_string


@pytest.mark.parametrize("processor", ["x86_64", "other"])
def test_transform_platform_processor(processor):
    transformed = transform_platform(
        "string-with-processor-{x64}.manifest", "win", processor
    )
    assert "{x64}" not in transformed
    if processor == "x86_64":
        assert "_x64" in transformed


@pytest.mark.parametrize(
    "platform, processor, version",
    [
        ("mac", None, None),
        ("mac", "arm", None),
        ("mac", "arm", "11.0.0"),
        ("mac", None, "11.0.0"),
        ("mac", None, "8.1.1"),
    ],
)
def test_transform_platform_macos_arm(platform, processor, version):
    # For this test assume platform is mac for all
    transformed = transform_platform(
        "mitmproxy-rel-bin-{platform}.manifest", platform, processor, version
    )
    assert "{platform}" not in transformed
    if processor == "arm" and version != "11.0.0":
        assert "osx-arm64" not in transformed
    if processor == "arm" and version == "11.0.0":
        assert "osx-arm64" in transformed
    if not processor and not version:
        # include check for .manifest extension so no ambiguity
        assert "osx.manifest" in transformed
    if not processor and version == "11.0.0":
        # E.g. intel macs using latest mitmproxy
        assert "osx.manifest" in transformed
    if not processor and version != "11.0.0":
        assert "osx.manifest" in transformed


@patch("logger.logger.RaptorLogger.info")
def test_write_yml_file(mock_info):
    yml_file = os.path.join(tempfile.mkdtemp(), "utils.yaml")

    yml_data = dict(args=["--a", "apple", "--b", "banana"], env=dict(LOG_VERBOSE=1))

    assert not os.path.exists(yml_file)
    write_yml_file(yml_file, yml_data)
    assert os.path.exists(yml_file)

    with open(yml_file) as yml_in:
        yml_loaded = yaml.unsafe_load(yml_in)
        assert yml_loaded == yml_data


@pytest.mark.parametrize(
    "value, expected_result",
    [
        ("true", True),
        ("TRUE", True),
        ("True", True),
        ("false", False),
        ("FALSE", False),
        ("False", False),
    ],
)
def test_bool_from_str(value, expected_result):
    assert expected_result == bool_from_str(value)


@pytest.mark.parametrize("invalid_value", ["invalid_str", ""])
def test_bool_from_str_with_invalid_values(invalid_value):
    with pytest.raises(ValueError):
        bool_from_str(invalid_value)


if __name__ == "__main__":
    mozunit.main()