summaryrefslogtreecommitdiffstats
path: root/tests/test_git_config.py
blob: 3bc14052290ec3c9d8324f4057855be492fbf343 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright (C) 2009 The Android Open Source Project
#
# 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.

"""Unittests for the git_config.py module."""

from pathlib import Path
from typing import Any

import pytest
import utils_for_test

import git_config


@pytest.fixture
def readonly_config() -> git_config.GitConfig:
    """Create a GitConfig object using the test.gitconfig fixture."""
    config_fixture = utils_for_test.FIXTURES_DIR / "test.gitconfig"
    return git_config.GitConfig(config_fixture)


def test_get_string_with_empty_config_values(
    readonly_config: git_config.GitConfig,
) -> None:
    """Test config entries with no value.

    [section]
        empty

    """
    val = readonly_config.GetString("section.empty")
    assert val is None


def test_get_string_with_true_value(
    readonly_config: git_config.GitConfig,
) -> None:
    """Test config entries with a string value.

    [section]
        nonempty = true

    """
    val = readonly_config.GetString("section.nonempty")
    assert val == "true"


def test_get_string_from_missing_file() -> None:
    """Test missing config file."""
    config_fixture = utils_for_test.FIXTURES_DIR / "not.present.gitconfig"
    config = git_config.GitConfig(config_fixture)
    val = config.GetString("empty")
    assert val is None


def test_get_boolean_undefined(readonly_config: git_config.GitConfig) -> None:
    """Test GetBoolean on key that doesn't exist."""
    assert readonly_config.GetBoolean("section.missing") is None


def test_get_boolean_invalid(readonly_config: git_config.GitConfig) -> None:
    """Test GetBoolean on invalid boolean value."""
    assert readonly_config.GetBoolean("section.boolinvalid") is None


def test_get_boolean_true(readonly_config: git_config.GitConfig) -> None:
    """Test GetBoolean on valid true boolean."""
    assert readonly_config.GetBoolean("section.booltrue") is True


def test_get_boolean_false(readonly_config: git_config.GitConfig) -> None:
    """Test GetBoolean on valid false boolean."""
    assert readonly_config.GetBoolean("section.boolfalse") is False


def test_get_int_undefined(readonly_config: git_config.GitConfig) -> None:
    """Test GetInt on key that doesn't exist."""
    assert readonly_config.GetInt("section.missing") is None


def test_get_int_invalid(readonly_config: git_config.GitConfig) -> None:
    """Test GetInt on invalid integer value."""
    assert readonly_config.GetInt("section.intinvalid") is None


@pytest.mark.parametrize(
    "key, expected",
    (
        ("inthex", 16),
        ("inthexk", 16384),
        ("int", 10),
        ("intk", 10240),
        ("intm", 10485760),
        ("intg", 10737418240),
    ),
)
def test_get_int_valid(
    readonly_config: git_config.GitConfig, key: str, expected: int
) -> None:
    """Test GetInt on valid integers."""
    assert readonly_config.GetInt(f"section.{key}") == expected


@pytest.fixture
def rw_config_file(tmp_path: Path) -> Path:
    """Return a path to a temporary config file."""
    return tmp_path / "config"


def test_set_string(rw_config_file: Path) -> None:
    """Test SetString behavior."""
    config = git_config.GitConfig(str(rw_config_file))

    # Set a value.
    assert config.GetString("foo.bar") is None
    config.SetString("foo.bar", "val")
    assert config.GetString("foo.bar") == "val"

    # Make sure the value was actually written out.
    config2 = git_config.GitConfig(str(rw_config_file))
    assert config2.GetString("foo.bar") == "val"

    # Update the value.
    config.SetString("foo.bar", "valll")
    assert config.GetString("foo.bar") == "valll"
    config3 = git_config.GitConfig(str(rw_config_file))
    assert config3.GetString("foo.bar") == "valll"

    # Delete the value.
    config.SetString("foo.bar", None)
    assert config.GetString("foo.bar") is None
    config4 = git_config.GitConfig(str(rw_config_file))
    assert config4.GetString("foo.bar") is None


def test_set_boolean(rw_config_file: Path) -> None:
    """Test SetBoolean behavior."""
    config = git_config.GitConfig(str(rw_config_file))

    # Set a true value.
    assert config.GetBoolean("foo.bar") is None
    for val in (True, 1):
        config.SetBoolean("foo.bar", val)
        assert config.GetBoolean("foo.bar") is True

    # Make sure the value was actually written out.
    config2 = git_config.GitConfig(str(rw_config_file))
    assert config2.GetBoolean("foo.bar") is True
    assert config2.GetString("foo.bar") == "true"

    # Set a false value.
    for val in (False, 0):
        config.SetBoolean("foo.bar", val)
        assert config.GetBoolean("foo.bar") is False

    # Make sure the value was actually written out.
    config3 = git_config.GitConfig(str(rw_config_file))
    assert config3.GetBoolean("foo.bar") is False
    assert config3.GetString("foo.bar") == "false"

    # Delete the value.
    config.SetBoolean("foo.bar", None)
    assert config.GetBoolean("foo.bar") is None
    config4 = git_config.GitConfig(str(rw_config_file))
    assert config4.GetBoolean("foo.bar") is None


def test_set_int(rw_config_file: Path) -> None:
    """Test SetInt behavior."""
    config = git_config.GitConfig(str(rw_config_file))

    # Set a value.
    assert config.GetInt("foo.bar") is None
    config.SetInt("foo.bar", 10)
    assert config.GetInt("foo.bar") == 10

    # Make sure the value was actually written out.
    config2 = git_config.GitConfig(str(rw_config_file))
    assert config2.GetInt("foo.bar") == 10
    assert config2.GetString("foo.bar") == "10"

    # Update the value.
    config.SetInt("foo.bar", 20)
    assert config.GetInt("foo.bar") == 20
    config3 = git_config.GitConfig(str(rw_config_file))
    assert config3.GetInt("foo.bar") == 20

    # Delete the value.
    config.SetInt("foo.bar", None)
    assert config.GetInt("foo.bar") is None
    config4 = git_config.GitConfig(str(rw_config_file))
    assert config4.GetInt("foo.bar") is None


def test_get_sync_analysis_state_data(rw_config_file: Path) -> None:
    """Test config entries with a sync state analysis data."""
    config = git_config.GitConfig(str(rw_config_file))
    superproject_logging_data: dict[str, Any] = {"test": False}

    class Options:
        """Container for testing."""

    options = Options()
    options.verbose = "true"
    options.mp_update = "false"

    TESTS = (
        ("superproject.test", "false"),
        ("options.verbose", "true"),
        ("options.mpupdate", "false"),
        ("main.version", "1"),
    )
    config.UpdateSyncAnalysisState(options, superproject_logging_data)
    sync_data = config.GetSyncAnalysisStateData()
    for key, value in TESTS:
        assert sync_data[f"{git_config.SYNC_STATE_PREFIX}{key}"] == value
    assert sync_data[f"{git_config.SYNC_STATE_PREFIX}main.synctime"]


def test_remote_save_with_push_url_without_projectname(
    rw_config_file: Path,
) -> None:
    """Test saving a remote pushUrl when projectname is unset."""
    config = git_config.GitConfig(str(rw_config_file))
    remote = config.GetRemote("origin")
    remote.url = "https://example.com/repo"
    remote.pushUrl = "ssh://example.com"
    remote.projectname = None

    remote.Save()

    written_config = git_config.GitConfig(str(rw_config_file))
    assert (
        written_config.GetString("remote.origin.pushurl") == "ssh://example.com"
    )