Files
StockSharp/scripts/test_console_utf8.py
mika@stocksharp.com 0234229f1b Validate connector coverage in CI and complete the connector tables
The connector README check prints the titles of the localized sections, and
Python encodes stdout with the locale code page: on the English Windows runner
that is cp1252, which has no glyph for Cyrillic or Chinese, so the step died
with UnicodeEncodeError and every later step of the validate job never ran. A
Cyrillic or Chinese locale hides this locally. Script output now goes through
UTF-8 via the shared console_utf8 helper, with stderr keeping a forgiving error
handler so that reporting a failure cannot fail in turn, and the tests
reproduce the runner by forcing cp1252 on a child process.

With the job running again, the READMEs turned out to list 292 connectors out
of the 393 that exist, plus 26 that exist nowhere in the code. Nothing checked
that: the only guard that looked past the READMEs, the --metadata-since mode,
matched commit subjects, so it saw only connectors added by a commit worded
that way, and CI never passed the flag. It is replaced by a check that reads
the connector sources and pairs a connector with its row through the
documentation page - a connector declares
[Doc("topics/api/connectors/<group>/<page>.html")] and the row links the same
path behind a language prefix, which identifies it without relying on the
display name or the logo, both of which are shared between connectors. Ten
connectors live in the applications repository, which the validate job does not
check out, so the pages they document are named in the check rather than
discovered.

The missing rows are generated from the connector itself: the logo from
[MediaIcon], the link from [Doc], the name from the localization resource named
by [Display]. The English name is used in all three files, as the common tables
have never translated connector names; only the Russian-market section, which
no other file mirrors, keeps its localized names and is also the only place the
connectors of that market are listed. Crypto connectors join the table that has
received every recent crypto connector, and the connectors documented under
europe/ and common/ join the stock table for want of a section of their own.
The rows of the withdrawn connectors are gone: sixteen crypto exchanges that
shut down years ago, eight data feeds and brokers dropped since, and two of the
Russian market. All of them kept their documentation pages and so looked
healthy to the link checker while promising connectors that cannot be used.

Also ignore __pycache__, which running the repository's own tests leaves behind.
2026-08-14 19:55:37 +03:00

100 lines
3.5 KiB
Python

from __future__ import annotations
import importlib.util
import os
import subprocess
import sys
import types
import unittest
from pathlib import Path
from console_utf8 import force_utf8_stdio
# A failing assertion here quotes the very text that an ANSI console cannot
# encode, so the test runner needs the same treatment as the scripts.
force_utf8_stdio()
SCRIPTS = Path(__file__).parent
ROOT = SCRIPTS.parent
# The Windows runner pipes script output through the ANSI code page, which on an
# English image is cp1252 and has no glyph for the localized connector names.
ANSI_CONSOLE = {"PYTHONIOENCODING": "cp1252", "PYTHONUTF8": "0"}
# "Russian market" and "Crypto exchanges": the alphabets the READMEs are written in.
SAMPLE_TEXT = (
"Российский рынок"
" / 加密货币交易所"
)
def child_source(*statements: str) -> str:
"""Build a program that configures the console and then prints."""
return "\n".join(
[
"import sys",
"sys.path.insert(0, sys.argv[1])",
"from console_utf8 import force_utf8_stdio",
"force_utf8_stdio()",
*statements,
]
)
def run_on_ansi_console(args: list[str]) -> subprocess.CompletedProcess[bytes]:
return subprocess.run(
[sys.executable, *args],
cwd=ROOT,
env=os.environ | ANSI_CONSOLE,
check=False,
capture_output=True,
)
def load_script(name: str) -> types.ModuleType:
spec = importlib.util.spec_from_file_location(name.replace("-", "_"), SCRIPTS / f"{name}.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
class ConsoleUtf8Tests(unittest.TestCase):
def test_stdout_and_stderr_survive_an_ansi_console(self) -> None:
child = child_source("print(sys.argv[2])", "print(sys.argv[2], file=sys.stderr)")
result = run_on_ansi_console(["-c", child, str(SCRIPTS), SAMPLE_TEXT])
self.assertEqual(0, result.returncode, result.stderr.decode("utf-8", "replace"))
self.assertEqual(SAMPLE_TEXT, result.stdout.decode("utf-8").strip())
self.assertEqual(SAMPLE_TEXT, result.stderr.decode("utf-8").strip())
def test_stderr_survives_text_that_utf8_cannot_encode(self) -> None:
child = child_source(r"print('\udcff', file=sys.stderr)")
result = run_on_ansi_console(["-c", child, str(SCRIPTS)])
self.assertEqual(0, result.returncode, result.stderr.decode("utf-8", "replace"))
self.assertIn(rb"\udcff", result.stderr)
def test_check_scripts_configure_their_console(self) -> None:
scripts = sorted(SCRIPTS.glob("check-*.py"))
self.assertTrue(scripts)
for script in scripts:
with self.subTest(script=script.name):
source = script.read_text(encoding="utf-8")
configured = (
"from console_utf8 import force_utf8_stdio" in source
and "force_utf8_stdio()" in source
)
self.assertTrue(configured, f"{script.name} must call force_utf8_stdio() before printing")
def test_readme_connectors_reports_localized_section_on_ansi_console(self) -> None:
result = run_on_ansi_console([str(SCRIPTS / "check-readme-connectors.py")])
title = load_script("check-readme-connectors").KNOWN_LOCAL_SECTIONS["ru"][0]
self.assertEqual("", result.stderr.decode("utf-8", "replace"))
self.assertIn(f"INFO local-only ru/{title}".encode(), result.stdout)
if __name__ == "__main__":
unittest.main()