# Copyright Kevin Deldycke <kevin@deldycke.com> and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test the command-line interface and its rendering helpers."""
from __future__ import annotations
import json
import sys
from operator import attrgetter
import pytest
from extra_platforms import (
ALL_TRAITS,
__version__,
current_agent,
current_architecture,
current_ci,
current_platform,
current_shell,
current_terminal,
)
from extra_platforms.__main__ import (
_SEPARATOR_WIDTH,
_column_widths,
_display_width,
_print_trait,
main,
)
CURRENT_FUNCS = {
"architecture": current_architecture,
"platform": current_platform,
"shell": current_shell,
"terminal": current_terminal,
"ci": current_ci,
"agent": current_agent,
}
"""The trait each section of the report covers, in report order."""
[docs]
def test_column_widths_of_no_rows():
"""An empty table needs no column width."""
assert _column_widths([]) == (0, 0, 0, 0)
[docs]
def test_main_text_report(monkeypatch, capsys):
"""The text report covers each trait category, then the two tables."""
monkeypatch.setattr(sys, "argv", ["extra-platforms"])
main()
lines = capsys.readouterr().out.splitlines()
assert lines[0] == f"extra-platforms {__version__}"
sections = [line[3:].split(" ββ")[0] for line in lines if line.startswith("ββ ")]
assert sections == [
"Architecture",
"Platform",
"Shell",
"Terminal",
"CI",
"Agent",
"Detected traits",
"Detected groups",
]
for current in CURRENT_FUNCS.values():
trait = current()
assert any(
f"ββ {trait.icon} {trait.name} ββ[{trait.symbol_id}]" in line
for line in lines
)
[docs]
def test_main_json_report(monkeypatch, capsys):
"""`--json` reports what the API detects, as a single JSON object."""
monkeypatch.setattr(sys, "argv", ["extra-platforms", "--json"])
main()
data = json.loads(capsys.readouterr().out)
assert list(data) == ["version", *CURRENT_FUNCS, "groups"]
assert data["version"] == __version__
for key, current in CURRENT_FUNCS.items():
trait = current()
assert data[key]["id"] == trait.id
assert data[key]["symbol"] == trait.symbol_id
assert data[key]["groups"] == sorted(g.symbol_id for g in trait.groups)
group_ids = [group["id"] for group in data["groups"]]
assert group_ids == sorted(group_ids)
assert all(
set(group) == {"id", "name", "icon", "symbol", "detection", "canonical"}
for group in data["groups"]
)