aboutsummaryrefslogtreecommitdiff
path: root/tools/tests/test_check_resources.py
blob: 3f0f9461c50689f4d44915c978235f7e1b50223e (plain) (blame)
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
# Author:  cirakg & claude ai
# Summary: Tests for tools/check_resources.py (schema validation and the settings cross-check).
"""Tests for tools/check_resources.py."""

from __future__ import annotations

from pathlib import Path

import check_resources as cr
import xmlschema

TINY_XSD = """<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="strings">
    <xs:complexType><xs:sequence>
      <xs:element name="string" maxOccurs="unbounded">
        <xs:complexType><xs:simpleContent><xs:extension base="xs:string">
          <xs:attribute name="id" type="xs:string" use="required"/>
        </xs:extension></xs:simpleContent></xs:complexType>
      </xs:element>
    </xs:sequence></xs:complexType>
  </xs:element>
</xs:schema>
"""

PROPERTIES = """<properties>
  <property id="kind" type="number">1</property>
  <property id="status" type="string"></property>
  <property id="count" type="number">0</property>
  <property id="orphan" type="string"></property>
</properties>
"""

SETTINGS = """<settings>
  <group id="g" title="@Strings.Title">
    <setting propertyKey="@Properties.kind" title="@Strings.Kind">
      <settingConfig type="list">
        <listEntry value="0">@Strings.Zero</listEntry>
        <listEntry value="2">@Strings.Missing</listEntry>
      </settingConfig>
    </setting>
    <setting propertyKey="@Properties.status" title="@Strings.Title">
      <settingConfig type="alphaNumeric" readonly="true" />
    </setting>
    <setting propertyKey="@Properties.count" title="@Strings.Title">
      <settingConfig type="numeric" readonly="true" />
    </setting>
    <setting propertyKey="@Properties.count" title="@Strings.Title">
      <settingConfig type="numeric" />
    </setting>
    <setting propertyKey="@Properties.ghost" title="@Strings.Title">
      <settingConfig type="boolean" />
    </setting>
  </group>
</settings>
"""

STRINGS = '<strings><string id="Title">T</string><string id="Kind">K</string><string id="Zero">0</string></strings>'


def write(folder: Path, name: str, text: str) -> Path:
    path = folder / name
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(text, encoding="utf-8")
    return path


def test_cross_check_finds_every_kind_of_mismatch(tmp_path: Path) -> None:
    settings = write(tmp_path, "settings.xml", SETTINGS)
    properties = write(tmp_path, "properties.xml", PROPERTIES)
    strings = write(tmp_path, "strings.xml", STRINGS)
    problems = cr.cross_check(settings, properties, [strings])
    assert problems == [
        "settings.xml: @Strings.Missing is not defined",
        "settings.xml: @Properties.ghost is not defined in properties.xml",
        "kind: default '1' is not one of the list values ['0', '2']",
        "count: read-only settings must be string properties, not number",
        "settings.xml: count appears 2 times",
        "properties.xml: orphan is not in the settings form",
    ]


def test_validate_reports_schema_errors(tmp_path: Path) -> None:
    schema = xmlschema.XMLSchema(TINY_XSD)
    good = write(tmp_path, "resources/strings/strings.xml", STRINGS)
    bad = write(tmp_path, "resources/strings/bad.xml", "<strings><string>no id</string></strings>")
    assert cr.resource_files(tmp_path) == [bad, good]
    problems = cr.validate(schema, [bad, good], tmp_path)
    assert len(problems) == 1
    assert problems[0].startswith("resources/strings/bad.xml: ")
    assert "id" in problems[0]


def test_main_needs_the_schema(tmp_path: Path) -> None:
    assert cr.main([str(tmp_path), "--xsd", str(tmp_path / "missing.xsd")]) == cr.EXIT_NO_SCHEMA


def test_cross_check_accepts_a_consistent_form(tmp_path: Path) -> None:
    properties = write(
        tmp_path, "properties.xml", '<properties><property id="kind" type="number">0</property></properties>'
    )
    settings = write(
        tmp_path,
        "settings.xml",
        '<settings><setting propertyKey="@Properties.kind" title="@Strings.Kind"><settingConfig type="list">'
        '<listEntry value="0">@Strings.Zero</listEntry></settingConfig></setting></settings>',
    )
    assert cr.cross_check(settings, properties, [write(tmp_path, "strings.xml", STRINGS)]) == []