mirror of https://github.com/Chizi123/Arch-autobuild-repo.git

Joel Grunbaum
2 days ago d68e2f2d0be6ac49e547775ece8655a720f6292f
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
"""Tests for configuration loading and validation."""
 
import pytest
from pathlib import Path
from tempfile import NamedTemporaryFile
 
from archbuild.config import (
    Config,
    load_config,
    migrate_vars_sh,
    save_config,
    BuildingConfig,
    RepositoryConfig,
)
 
 
class TestConfig:
    """Tests for Config model."""
 
    def test_minimal_config(self):
        """Test minimal valid configuration."""
        config = Config(
            repository=RepositoryConfig(
                name="test",
                path=Path("/repo"),
                build_dir=Path("/build"),
            )
        )
        assert config.repository.name == "test"
        assert config.building.parallel is True  # default
        assert config.building.max_workers == 4  # default
 
    def test_building_config_defaults(self):
        """Test BuildingConfig defaults."""
        config = BuildingConfig()
        assert config.parallel is True
        assert config.max_workers == 4
        assert config.clean is True
        assert config.retry_attempts == 3
 
    def test_max_workers_validation(self):
        """Test max_workers bounds."""
        with pytest.raises(ValueError):
            BuildingConfig(max_workers=0)
        with pytest.raises(ValueError):
            BuildingConfig(max_workers=100)
 
    def test_log_level_validation(self):
        """Test log level validation."""
        config = Config(
            repository=RepositoryConfig(
                name="test",
                path=Path("/repo"),
                build_dir=Path("/build"),
            ),
            log_level="debug",
        )
        assert config.log_level == "DEBUG"
 
        with pytest.raises(ValueError):
            Config(
                repository=RepositoryConfig(
                    name="test",
                    path=Path("/repo"),
                    build_dir=Path("/build"),
                ),
                log_level="invalid",
            )
 
 
class TestLoadConfig:
    """Tests for config file loading."""
 
    def test_load_yaml(self, tmp_path):
        """Test loading YAML config file."""
        config_file = tmp_path / "config.yaml"
        config_file.write_text("""
repository:
  name: myrepo
  path: /repo/x86_64
  build_dir: /repo/build
building:
  max_workers: 8
""")
        config = load_config(config_file)
        assert config.repository.name == "myrepo"
        assert config.building.max_workers == 8
 
    def test_file_not_found(self):
        """Test error on missing config file."""
        with pytest.raises(FileNotFoundError):
            load_config(Path("/nonexistent/config.yaml"))
 
 
class TestMigrateVarsSh:
    """Tests for vars.sh migration."""
 
    def test_migrate_basic(self, tmp_path):
        """Test basic vars.sh migration."""
        vars_file = tmp_path / "vars.sh"
        vars_file.write_text("""
REPODIR=/repo/x86_64
BUILDDIR=/repo/build
REPONAME=myrepo
PARALLEL=Y
SIGN=N
NUM_OLD=5
""")
        data = migrate_vars_sh(vars_file)
 
        assert data["repository"]["name"] == "myrepo"
        assert data["repository"]["path"] == "/repo/x86_64"
        assert data["building"]["parallel"] is True
        assert data["signing"]["enabled"] is False
        assert data["retention"]["keep_versions"] == 5
 
    def test_migrate_with_export(self, tmp_path):
        """Test migration handles export statements."""
        vars_file = tmp_path / "vars.sh"
        vars_file.write_text("""
export REPONAME="testrepo"
export REPODIR="/test/repo"
export BUILDDIR="/test/build"
""")
        data = migrate_vars_sh(vars_file)
 
        assert data["repository"]["name"] == "testrepo"
 
 
class TestSaveConfig:
    """Tests for config saving."""
 
    def test_round_trip(self, tmp_path):
        """Test config save/load round trip."""
        config = Config(
            repository=RepositoryConfig(
                name="test",
                path=Path("/repo"),
                build_dir=Path("/build"),
            ),
            building=BuildingConfig(max_workers=6),
        )
 
        config_file = tmp_path / "config.yaml"
        save_config(config, config_file)
 
        loaded = load_config(config_file)
        assert loaded.repository.name == "test"
        assert loaded.building.max_workers == 6