Skip to content

Commit

Permalink
Add tests for tag property of repo
Browse files Browse the repository at this point in the history
* test_refspec_switch
* test mix of refspec and tag
* test if kas fails if commit and tag do not match
* test if kas warns about tag without commit being unsafe
* test if kas does not mixes up tag and branch with the same name

Signed-off-by: Félix Piédallu <[email protected]>
[Jan: style fix]
Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
Salamandar authored and jan-kiszka committed Oct 18, 2023
1 parent 1c403a3 commit 75f4767
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/test_refspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def test_refspec_switch(changedir, tmpdir):
fail=False, liveupdate=False)
assert rc == 0
assert output.strip() == 'refs/heads/master'
(rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'], cwd='kas3',
fail=False, liveupdate=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',
Expand All @@ -64,6 +68,14 @@ def test_refspec_switch(changedir, tmpdir):
fail=False, liveupdate=False)
assert rc == 0
assert output.strip() == '907816a5c4094b59a36aec12226e71c461c05b77'
(rc, output) = run_cmd(['git', 'symbolic-ref', '-q', 'HEAD'], cwd='kas3',
fail=False, liveupdate=False)
assert rc == 0
assert output.strip() == 'refs/heads/master'
(rc, output) = run_cmd(['git', 'tag', '--points-at', 'HEAD'], cwd='kas4',
fail=False, liveupdate=False)
assert rc == 0
assert output.strip() == '2.6.3'


def test_refspec_absolute(changedir, tmpdir):
Expand All @@ -87,6 +99,10 @@ def test_refspec_absolute(changedir, tmpdir):
cwd='kas_rel', fail=False, liveupdate=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, liveupdate=False)
assert rc == 0
assert output.strip() == '3.0.1'


def test_url_no_refspec(changedir, tmpdir):
Expand All @@ -111,6 +127,81 @@ def test_commit_refspec_mix(changedir, tmpdir):
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'])


def test_tag_commit_do_not_match(changedir, 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)
os.chdir(tdir)
with pytest.raises(RepoRefError):
kas.kas(['shell', 'test8.yml', '-c', 'true'])


def test_unsafe_tag_warning(capsys, changedir, 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)
os.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


def test_tag_branch_same_name(capsys, changedir, 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)
os.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, liveupdate=False)
assert rc == 0
(rc, output) = run_cmd(['git', 'branch', '3.0.1'], cwd='kas3',
fail=False, liveupdate=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, liveupdate=False)
assert rc == 0
(rc, output) = run_cmd(['git', 'tag', 'master'], cwd='kas4',
fail=False, liveupdate=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, liveupdate=False)
assert rc == 0
assert output.strip() == '229310958b17dc2b505b789c1cc1d0e2fddccc44'

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

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


def test_refspec_warning(capsys, changedir, tmpdir):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_refspec/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ repos:
kas2:
url: https://github.com/siemens/kas.git
branch: master

kas3:
url: https://github.com/siemens/kas.git
tag: 3.0.1
commit: 229310958b17dc2b505b789c1cc1d0e2fddccc44

kas4:
url: https://github.com/siemens/kas.git
branch: master
8 changes: 8 additions & 0 deletions tests/test_refspec/test2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ repos:
url: https://github.com/siemens/kas.git
# keep legacy refspec here for testing purposes
refspec: 907816a5c4094b59a36aec12226e71c461c05b77

kas3:
url: https://github.com/siemens/kas.git
branch: master

kas4:
url: https://github.com/siemens/kas.git
tag: 2.6.3
4 changes: 4 additions & 0 deletions tests/test_refspec/test3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ repos:
kas_rel:
url: https://github.com/siemens/kas.git
branch: master

kas_tag_abs:
url: https://github.com/siemens/kas.git
tag: refs/tags/3.0.1
10 changes: 10 additions & 0 deletions tests/test_refspec/test7.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
header:
version: 14

repos:
this:

kas:
url: https://github.com/siemens/kas.git
refspec: dc44638cd87c4d0045ea2ca441e682f3525d8b91
tag: 3.0.1
10 changes: 10 additions & 0 deletions tests/test_refspec/test8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
header:
version: 14

repos:
this:

kas:
url: https://github.com/siemens/kas.git
tag: 3.0.1
commit: de4dcafe

0 comments on commit 75f4767

Please sign in to comment.