diff --git a/tests/test_refspec.py b/tests/test_refspec.py index c1dfcde7..457a26b8 100644 --- a/tests/test_refspec.py +++ b/tests/test_refspec.py @@ -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', @@ -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): @@ -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): @@ -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): diff --git a/tests/test_refspec/test.yml b/tests/test_refspec/test.yml index c15d27da..196e7e2a 100644 --- a/tests/test_refspec/test.yml +++ b/tests/test_refspec/test.yml @@ -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 diff --git a/tests/test_refspec/test2.yml b/tests/test_refspec/test2.yml index ae6fb4ef..e68cf527 100644 --- a/tests/test_refspec/test2.yml +++ b/tests/test_refspec/test2.yml @@ -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 diff --git a/tests/test_refspec/test3.yml b/tests/test_refspec/test3.yml index 41e3859c..7f8b4cee 100644 --- a/tests/test_refspec/test3.yml +++ b/tests/test_refspec/test3.yml @@ -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 diff --git a/tests/test_refspec/test7.yml b/tests/test_refspec/test7.yml new file mode 100644 index 00000000..76447c93 --- /dev/null +++ b/tests/test_refspec/test7.yml @@ -0,0 +1,10 @@ +header: + version: 14 + +repos: + this: + + kas: + url: https://github.com/siemens/kas.git + refspec: dc44638cd87c4d0045ea2ca441e682f3525d8b91 + tag: 3.0.1 diff --git a/tests/test_refspec/test8.yml b/tests/test_refspec/test8.yml new file mode 100644 index 00000000..eb312cde --- /dev/null +++ b/tests/test_refspec/test8.yml @@ -0,0 +1,10 @@ +header: + version: 14 + +repos: + this: + + kas: + url: https://github.com/siemens/kas.git + tag: 3.0.1 + commit: de4dcafe