"""Tests for flow.core.config_parse.""" import pytest from flow.core.config import TargetConfig from flow.core.config_parse import as_bool, parse_target_shorthand, parse_targets from flow.core.errors import ConfigError class TestAsBool: @pytest.mark.parametrize("value", [True, "true", "True", "YES", "1", "on", "y"]) def test_truthy(self, value): assert as_bool(value) is True @pytest.mark.parametrize("value", [False, "false", "False", "NO", "0", "off", "n"]) def test_falsy(self, value): assert as_bool(value) is False def test_invalid_raises(self): with pytest.raises(ConfigError, match="Expected boolean"): as_bool("maybe") class TestParseTargetShorthand: def test_at_key(self): t = parse_target_shorthand("personal@orb", "personal.orb") assert t == TargetConfig(namespace="personal", platform="orb", host="personal.orb") def test_at_key_with_identity(self): t = parse_target_shorthand("work@ec2", "work.ec2 ~/.ssh/id_work") assert t.identity == "~/.ssh/id_work" def test_plain_key(self): t = parse_target_shorthand("personal", "orb personal.orb ~/.ssh/id") assert t == TargetConfig(namespace="personal", platform="orb", host="personal.orb", identity="~/.ssh/id") def test_empty_value_raises(self): with pytest.raises(ConfigError, match="must define a host"): parse_target_shorthand("x@y", "") def test_plain_key_too_few_parts_raises(self): with pytest.raises(ConfigError, match="expected 'platform host"): parse_target_shorthand("personal", "onlyone") class TestParseTargets: def test_none_returns_empty(self): assert parse_targets(None) == [] def test_dict_shorthand(self): targets = parse_targets({"personal@orb": "personal.orb"}) assert len(targets) == 1 assert targets[0].host == "personal.orb" def test_dict_mapping(self): targets = parse_targets({ "work@ec2": {"host": "work.ec2", "identity": "~/.ssh/id"}, }) assert targets[0].host == "work.ec2" assert targets[0].identity == "~/.ssh/id" def test_list_format(self): targets = parse_targets([ {"namespace": "a", "platform": "b", "host": "a.b"}, ]) assert len(targets) == 1 assert targets[0].namespace == "a" def test_invalid_type_raises(self): with pytest.raises(ConfigError, match="mapping or list"): parse_targets("invalid")