summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/webidl-api/test/test_json_schema_platform_diffs.py
blob: 51ae918eb00d18cf7be08c81664f214f84e5a333 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import types
from textwrap import dedent

import helpers  # Import test helpers module.
import mozunit

helpers.setup()

from GenerateWebIDLBindings import Schemas
from InspectJSONSchema import run_inspect_command


def test_inspect_schema_platform_diffs(capsys, write_jsonschema_fixtures, tmpdir):
    """
    Test InspectJSONSchema --dump-platform-diff command.
    """
    browser_schema_dir = os.path.join(tmpdir, "browser")
    mobile_schema_dir = os.path.join(tmpdir, "mobile")
    os.mkdir(browser_schema_dir)
    os.mkdir(mobile_schema_dir)

    write_jsonschema_fixtures(
        {
            "test_api_browser.json": dedent(
                """
                [
                  {
                    "namespace": "apiWithDiff",
                    "functions": [
                      {
                        "name": "sharedMethod",
                        "type": "function",
                        "parameters": [
                          { "name": "sharedParam", "type": "string" },
                          { "name": "desktopOnlyMethodParam", "type": "string" }
                        ]
                      },
                      {
                        "name": "desktopMethod",
                        "type": "function",
                        "parameters": []
                      }
                    ],
                    "events": [
                      {
                        "name": "onSharedEvent",
                        "type": "function",
                        "parameters": [
                           { "name": "sharedParam", "type": "string" },
                           { "name": "desktopOnlyEventParam", "type": "string" }
                        ]
                      }
                    ],
                    "properties": {
                      "sharedProperty": { "type": "string", "value": "desktop-value" },
                      "desktopOnlyProperty": { "type": "string", "value": "desktop-only-value" }
                    }
                  }
                ]
                """
            )
        },
        browser_schema_dir,
    )

    write_jsonschema_fixtures(
        {
            "test_api_mobile.json": dedent(
                """
                [
                  {
                    "namespace": "apiWithDiff",
                    "functions": [
                      {
                        "name": "sharedMethod",
                        "type": "function",
                        "parameters": [
                          { "name": "sharedParam", "type": "string" },
                          { "name": "mobileOnlyMethodParam", "type": "string" }
                        ]
                      },
                      {
                        "name": "mobileMethod",
                        "type": "function",
                        "parameters": []
                      }
                    ],
                    "events": [
                      {
                        "name": "onSharedEvent",
                        "type": "function",
                        "parameters": [
                           { "name": "sharedParam", "type": "string" },
                           { "name": "mobileOnlyEventParam", "type": "string" }
                        ]
                      }
                    ],
                    "properties": {
                      "sharedProperty": { "type": "string", "value": "mobile-value" },
                      "mobileOnlyProperty": { "type": "string", "value": "mobile-only-value" }
                    }
                  }
                ]
                """
            )
        },
        mobile_schema_dir,
    )

    schemas = Schemas()
    schemas.load_schemas(browser_schema_dir, "browser")
    schemas.load_schemas(mobile_schema_dir, "mobile")

    assert schemas.get_all_namespace_names() == ["apiWithDiff"]
    apiNs = schemas.api_namespaces["apiWithDiff"]
    assert apiNs.in_browser
    assert apiNs.in_mobile

    apiNs.parse_schemas()

    fakeArgs = types.SimpleNamespace()
    fakeArgs.dump_namespaces_info = False
    fakeArgs.dump_platform_diffs = True
    fakeArgs.only_if_webidl_diffs = False
    fakeArgs.diff_command = None

    fakeParser = types.SimpleNamespace()
    fakeParser.print_help = lambda: None

    run_inspect_command(fakeArgs, schemas, fakeParser)

    captured = capsys.readouterr()
    assert "API schema desktop vs. mobile for apiWithDiff.sharedMethod" in captured.out
    assert '-   "name": "desktopOnlyMethodParam",' in captured.out
    assert '+   "name": "mobileOnlyMethodParam",' in captured.out
    assert "API schema desktop vs. mobile for apiWithDiff.onSharedEvent" in captured.out
    assert '-   "name": "desktopOnlyEventParam",' in captured.out
    assert '+   "name": "mobileOnlyEventParam",' in captured.out
    assert (
        "API schema desktop vs. mobile for apiWithDiff.sharedProperty" in captured.out
    )
    assert '- "value": "desktop-value"' in captured.out
    assert '+ "value": "mobile-value"' in captured.out
    assert captured.err == ""


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