test_pagure_lib_git_get_tags_objects.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Clement Verna <cverna@tutanota.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import time
  12. import pygit2
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.git
  17. import pagure.lib.query
  18. import tests
  19. def get_tag_name(tags):
  20. """ Return a list of the tag names """
  21. output = []
  22. for tag in tags:
  23. output.append(tag["tagname"])
  24. return output
  25. def add_repo_tag(git_dir, repo, tags, repo_name):
  26. """ Use a list to create multiple tags on a git repo """
  27. for tag in reversed(tags):
  28. time.sleep(1)
  29. tests.add_commit_git_repo(
  30. os.path.join(git_dir, "repos", repo_name), ncommits=1
  31. )
  32. first_commit = repo.revparse_single("HEAD")
  33. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  34. repo.create_tag(
  35. tag,
  36. first_commit.oid.hex,
  37. pygit2.GIT_OBJ_COMMIT,
  38. tagger,
  39. "Release " + tag,
  40. )
  41. class PagureLibGitGetTagstests(tests.Modeltests):
  42. def test_get_git_tags_objects(self):
  43. """ Test the get_git_tags_objects method of pagure.lib.git. """
  44. tests.create_projects(self.session)
  45. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  46. project = pagure.lib.query._get_project(self.session, "test")
  47. # Case 1 - Empty repo with no tags
  48. exp = []
  49. tags = pagure.lib.git.get_git_tags_objects(project)
  50. self.assertEqual(exp, get_tag_name(tags))
  51. tests.add_readme_git_repo(
  52. os.path.join(os.path.join(self.path, "repos"), "test.git")
  53. )
  54. repo = pygit2.Repository(
  55. os.path.join(os.path.join(self.path, "repos"), "test.git")
  56. )
  57. # Case 2 - Repo with one commit and no tags
  58. exp = []
  59. tags = pagure.lib.git.get_git_tags_objects(project)
  60. self.assertEqual(exp, get_tag_name(tags))
  61. # Case 3 - Simple sort
  62. exp = [
  63. "0.1.0",
  64. "test-0.0.21",
  65. "0.0.12-beta",
  66. "0.0.12-alpha",
  67. "0.0.12",
  68. "0.0.11",
  69. "0.0.3",
  70. "foo-0.0.2",
  71. "0.0.1",
  72. ]
  73. add_repo_tag(self.path, repo, exp, "test.git")
  74. tags = pagure.lib.git.get_git_tags_objects(project)
  75. self.assertEqual(exp, get_tag_name(tags))
  76. # Case 4 - Sorting with different splitting characters
  77. project = pagure.lib.query.get_authorized_project(
  78. self.session, "test2"
  79. )
  80. tests.add_readme_git_repo(
  81. os.path.join(os.path.join(self.path, "repos"), "test2.git")
  82. )
  83. repo = pygit2.Repository(
  84. os.path.join(os.path.join(self.path, "repos"), "test2.git")
  85. )
  86. exp = [
  87. "1.0-0_2",
  88. "1.0-0_1",
  89. "0.1-1_0",
  90. "0.1-0_0",
  91. "0.0-2_0",
  92. "0.0-1_34",
  93. "0.0-1_11",
  94. "0.0-1_3",
  95. "0.0-1_2",
  96. "0.0-1_1",
  97. ]
  98. add_repo_tag(self.path, repo, exp, "test2.git")
  99. tags = pagure.lib.git.get_git_tags_objects(project)
  100. self.assertEqual(exp, get_tag_name(tags))
  101. if __name__ == "__main__":
  102. unittest.main(verbosity=2)