File: test_refspec.py

package info (click to toggle)
kas 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,268 kB
  • sloc: python: 5,745; sh: 1,095; makefile: 210
file content (278 lines) | stat: -rw-r--r-- 10,247 bytes parent folder | download | duplicates (2)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# kas - setup tool for bitbake based projects
#
# Copyright (c) Siemens AG, 2019
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest
import shutil
import yaml
import subprocess
import os
from pathlib import Path
from kas import kas
from kas.repos import RepoRefError, Repo


def run_cmd(cmd, cwd=None, fail=True):
    """
        Run a command and return the return code and output.
        Replacement for kas internal run_cmd function which
        cannot be used outside of kas as there is no event loop.
    """
    kas_wd = Path(os.environ.get('KAS_WORK_DIR', '.'))
    _cwd = Path(cwd or '.')
    if not _cwd.is_absolute():
        _cwd = kas_wd / _cwd
    try:
        output = subprocess.check_output(cmd, cwd=_cwd)
        return (0, output.decode('utf-8'))
    except subprocess.CalledProcessError as e:
        if fail:
            raise e
        return (e.returncode, e.output.decode('utf-8'))


@pytest.mark.online
def test_refspec_switch(monkeykas, tmpdir):
    """
        Test that the local git clone is correctly updated when switching
        between a commit hash refspec and a branch refspec.
    """
    tdir = str(tmpdir / 'test_refspec_switch')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    kas.kas(['shell', 'test.yml', '-c', 'true'])
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas',
                           fail=False)
    assert rc != 0
    assert output.strip() == ''
    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas',
                           fail=False)
    assert rc == 0
    assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas2',
                           fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'], cwd='kas3',
                           fail=False)
    assert rc == 0
    assert output.strip() == '3.0.1'

    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas',
                           fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas2',
                           fail=False)
    assert rc != 0
    assert output.strip() == ''
    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas2',
                           fail=False)
    assert rc == 0
    assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas3',
                           fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'], cwd='kas4',
                           fail=False)
    assert rc == 0
    assert output.strip() == '2.6.3'


@pytest.mark.online
def test_refspec_absolute(monkeykas, tmpdir):
    """
        Test that the local git clone works when a absolute refspec
        is given.
    """
    tdir = str(tmpdir / 'test_refspec_absolute')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    kas.kas(['shell', 'test3.yml', '-c', 'true'])
    (rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'],
                           cwd='kas_abs', fail=False)
    assert rc == 0
    assert output.strip() == 'refs/heads/master'
    (rc, output_kas_abs) = run_cmd(['git', 'rev-parse', 'HEAD'],
                                   cwd='kas_abs', fail=False)
    assert rc == 0
    (rc, output_kas_rel) = run_cmd(['git', 'rev-parse', 'HEAD'],
                                   cwd='kas_rel', fail=False)
    assert rc == 0
    assert output_kas_abs.strip() == output_kas_rel.strip()
    (rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'],
                           cwd='kas_tag_abs', fail=False)
    assert rc == 0
    assert output.strip() == '3.0.1'


def test_url_no_refspec(monkeykas, tmpdir):
    """
        Test that a repository with url but no refspec raises an error.
    """
    tdir = str(tmpdir / 'test_url_no_refspec')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test4.yml', '-c', 'true'])


def test_commit_refspec_mix(monkeykas, tmpdir):
    """
        Test that mixing legacy refspec with commit/branch raises errors.
    """
    tdir = str(tmpdir / 'test_commit_refspec_mix')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test5.yml', '-c', 'true'])
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test6.yml', '-c', 'true'])
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test7.yml', '-c', 'true'])


@pytest.mark.online
def test_tag_commit_do_not_match(monkeykas, tmpdir):
    """
        Test that giving tag and commit that do not match raises an error.
    """
    tdir = str(tmpdir / 'test_tag_commit_do_not_match')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with pytest.raises(RepoRefError):
        kas.kas(['shell', 'test8.yml', '-c', 'true'])


@pytest.mark.online
def test_unsafe_tag_warning(capsys, monkeykas, tmpdir):
    """
        Test that using tag without commit issues a warning, but only once.
    """
    tdir = str(tmpdir / 'test_unsafe_tag_warning')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    # needs to be reset in case other tests ran before
    Repo.__no_commit_tag_warned__ = []
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    assert capsys.readouterr().err.count(
        'Using tag without commit for repository "kas4" is unsafe as tags '
        'are mutable.') == 1


@pytest.mark.online
def test_tag_branch_same_name(capsys, monkeykas, tmpdir):
    """
        Test that kas uses the tag if a branch has the same name as the tag.
    """
    tdir = str(tmpdir / 'test_tag_branch_same_name')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)

    # Checkout the repositories
    kas.kas(['shell', 'test.yml', '-c', 'true'])

    # In kas3: create a branch named "3.0.1" on master HEAD
    # A tag named "3.0.1" already exists on an old commit from 2022
    (rc, output) = run_cmd(['git', 'switch', 'master'], cwd='kas3',
                           fail=False)
    assert rc == 0
    (rc, output) = run_cmd(['git', 'branch', '3.0.1'], cwd='kas3',
                           fail=False)
    assert rc == 0

    # In kas4: create a tag named "master" on existing 2.6.3 tag
    (rc, output) = run_cmd(['git', 'checkout', '2.6.3'], cwd='kas4',
                           fail=False)
    assert rc == 0
    (rc, output) = run_cmd(['git', 'tag', 'master'], cwd='kas4',
                           fail=False)
    assert rc == 0

    # Checkout the repositories again
    kas.kas(['shell', 'test.yml', '-c', 'true'])

    # Check the commit hashes
    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas3',
                           fail=False)
    assert rc == 0
    assert output.strip() == '229310958b17dc2b505b789c1cc1d0e2fddccc44'

    (rc, output) = run_cmd(['git', 'rev-parse', 'HEAD'], cwd='kas4',
                           fail=False)
    assert rc == 0

    (rc, output2) = run_cmd(['git', 'rev-parse', 'refs/heads/master'],
                            cwd='kas4', fail=False)
    assert rc == 0
    assert output.strip() == output2.strip()


@pytest.mark.online
def test_refspec_warning(capsys, monkeykas, tmpdir):
    """
        Test that using legacy refspec issues a warning, but only once.
    """
    tdir = str(tmpdir / 'test_refspec_warning')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    # needs to be reset in case other tests ran before
    Repo.__legacy_refspec_warned__ = []
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    assert capsys.readouterr().err.count(
        'Using deprecated refspec for repository "kas2".') == 1


@pytest.mark.online
def test_branch_and_tag(monkeykas, tmpdir, mercurial):
    """
        Test if error is raised when branch and tag are set.
    """
    tdir = str(tmpdir / 'test_branch_and_tag')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    with mercurial(tmpdir, 'evolve', branch='stable'):
        with pytest.raises(RepoRefError):
            kas.kas(['checkout', 'test9.yml'])

    with pytest.raises(RepoRefError):
        kas.kas(['checkout', 'test10.yml'])

    with pytest.raises(RepoRefError):
        kas.kas(['checkout', 'test11.yml'])


@pytest.mark.online
def test_commit_expand(monkeykas, tmpdir, capsys):
    """
        Test if an abbreviated commit hash is expanded to the full hash.
    """
    tdir = str(tmpdir / 'test_commit_expand')
    shutil.copytree('tests/test_refspec', tdir)
    monkeykas.chdir(tdir)
    kas.kas(['dump', '--resolve-refs', 'test12.yml'])
    rawspec = yaml.safe_load(capsys.readouterr().out)
    assert rawspec['repos']['kas']['commit'] == \
        'abd109469d17b7ff4d958b5aa5ab5f5511cc4d43'