From ce25e98dc974525daa0177f20eceb5f09482ae69 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Feb 04 2016 17:03:56 +0000 Subject: Moved unit test for git_get_tags_objects to tests/test_pagure_lib_git_get_tags_objects.py and fixing pep8 errors --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 949bf0c..9cc1988 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1210,19 +1210,19 @@ def get_git_tags_objects(project): tags_sort = {} for tag in tags: - #Split the git tag name using any non-alphanumeric character - #and store each element returned in a tuple. + # Split the git tag name using any non-alphanumeric character + # and store each element returned in a tuple. splitted_tag = re.split('[^a-zA-Z0-9]+', tag.name) sorting_tuple = () for item in splitted_tag: - #Cast the result to an integer when possible + # Cast the result to an integer when possible if item.isdigit(): sorting_tuple += (int(item),) else: sorting_tuple += (item,) tags_sort[sorting_tuple] = tag - for tag in sorted(tags_sort, reverse = True): + for tag in sorted(tags_sort, reverse=True): sorted_tags.append(tags_sort[tag]) return sorted_tags diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 0ccf964..25e9020 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -724,7 +724,6 @@ def view_tags(repo, username=None): flask.abort(404, 'Project not found') tags = pagure.lib.git.get_git_tags_objects(repo) - return flask.render_template( 'releases.html', select='tags', diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index 096c366..2615f6e 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -1313,115 +1313,6 @@ index 0000000..60f7480 os.path.join(tests.HERE, 'forks', 'pingou', 'foo.test.git')) self.assertEqual(repo_name, 'pingou') - def test_get_git_tags_objects(self): - """ Test the get_git_tags_objects method of pagure.lib.git. """ - tests.create_projects(self.session) - tests.create_projects_git(os.path.join(tests.HERE, 'repos'), bare=True) - project = pagure.lib.get_project(self.session, 'test') - - def get_tag_name(tags): - """ Return a list of the tag names """ - output = [] - for tag in tags: - output.append(tag.name) - return output - - #Case 1 - project does not contains tags - exp = [] - tags = pagure.lib.git.get_git_tags_objects(project) - self.assertEqual(exp,get_tag_name(tags)) - - #Case 2 - Simple sort - exp = ['0.1.0', '0.0.21', '0.0.12', '0.0.11', '0.0.3', '0.0.2', '0.0.1'] - - tests.add_readme_git_repo(os.path.join(os.path.join(tests.HERE, 'repos'), 'test.git')) - repo = pygit2.Repository(os.path.join(os.path.join(tests.HERE, 'repos'), 'test.git')) - first_commit = repo.revparse_single('HEAD') - tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) - repo.create_tag( - "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.1") - repo.create_tag( - "0.1.0", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.1.0") - repo.create_tag( - "0.0.2", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.2") - repo.create_tag( - "0.0.3", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.3") - repo.create_tag( - "0.0.11", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.11") - repo.create_tag( - "0.0.12", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.12") - repo.create_tag( - "0.0.21", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.21") - - tags = pagure.lib.git.get_git_tags_objects(project) - self.assertEqual(exp,get_tag_name(tags)) - - #Case 3 Sorting with alpha and beta - exp = ['0.1.0', '0.0.21', '0.0.12-beta', '0.0.12-alpha', '0.0.12', '0.0.11', '0.0.3', '0.0.2', '0.0.1'] - - repo.create_tag( - "0.0.12-alpha", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.12") - repo.create_tag( - "0.0.12-beta", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0.12") - - tags = pagure.lib.git.get_git_tags_objects(project) - self.assertEqual(exp,get_tag_name(tags)) - - #Case 4 - Sorting with different splitting characters - project = pagure.lib.get_project(self.session, 'test2') - exp = ['15.1-0_0', '1.0-0_14', '1.0-0_2', '1.0-0_1', '0.1-1_0', '0.1-0_0', '0.0-2_0', '0.0-1_34', '0.0-1_11', '0.0-1_3', '0.0-1_2', '0.0-1_1'] - tests.add_readme_git_repo(os.path.join(os.path.join(tests.HERE, 'repos'), 'test2.git')) - repo = pygit2.Repository(os.path.join(os.path.join(tests.HERE, 'repos'), 'test2.git')) - first_commit = repo.revparse_single('HEAD') - tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) - repo.create_tag( - "0.0-1_1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0-1_1") - repo.create_tag( - "0.0-1_3", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0-1_3") - repo.create_tag( - "0.0-1_11", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0-1_11") - repo.create_tag( - "0.0-1_2", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0-1_2") - repo.create_tag( - "0.1-0_0", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.1-0_0") - repo.create_tag( - "0.1-1_0", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.1-1_0") - repo.create_tag( - "0.0-1_34", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0-1_34") - repo.create_tag( - "0.0-2_0", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 0.0-2_0") - repo.create_tag( - "1.0-0_1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 1.0-0_1") - repo.create_tag( - "1.0-0_14", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 1.0-0_14") - repo.create_tag( - "1.0-0_2", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 1.0-0_2") - repo.create_tag( - "15.1-0_0", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, - "Release 15.1-0_0") - - tags = pagure.lib.git.get_git_tags_objects(project) - self.assertEqual(exp,get_tag_name(tags)) if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibGittests) diff --git a/tests/test_pagure_lib_git_get_tags_objects.py b/tests/test_pagure_lib_git_get_tags_objects.py new file mode 100644 index 0000000..1563523 --- /dev/null +++ b/tests/test_pagure_lib_git_get_tags_objects.py @@ -0,0 +1,109 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2016 - Copyright Red Hat Inc + + Authors: + Clement Verna + +""" + +import unittest +import sys +import os + +import pygit2 + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure.lib.git +import tests + + +def get_tag_name(tags): + """ Return a list of the tag names """ + output = [] + for tag in tags: + output.append(tag.name) + return output + + +def add_repo_tag(repo, tags): + """ Use a list to create multiple tags on a git repo """ + first_commit = repo.revparse_single('HEAD') + tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) + for tag in tags: + repo.create_tag( + tag, first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, + "Release " + tag) + + +class PagureLibGitGetTagstests(tests.Modeltests): + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureLibGitGetTagstests, self).setUp() + + pagure.lib.git.SESSION = self.session + pagure.APP.config['GIT_FOLDER'] = os.path.join( + tests.HERE, 'repos') + pagure.APP.config['FORK_FOLDER'] = os.path.join( + tests.HERE, 'forks') + pagure.APP.config['TICKETS_FOLDER'] = os.path.join( + tests.HERE, 'tickets') + pagure.APP.config['DOCS_FOLDER'] = os.path.join( + tests.HERE, 'docs') + pagure.APP.config['REQUESTS_FOLDER'] = os.path.join( + tests.HERE, 'requests') + + def test_get_git_tags_objects(self): + """ Test the get_git_tags_objects method of pagure.lib.git. """ + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(tests.HERE, 'repos'), bare=True) + project = pagure.lib.get_project(self.session, 'test') + + # Case 1 - project does not contains tags + exp = [] + tags = pagure.lib.git.get_git_tags_objects(project) + self.assertEqual(exp, get_tag_name(tags)) + + # Case 2 - Simple sort + tests.add_readme_git_repo(os.path.join(os.path.join( + tests.HERE, 'repos'), 'test.git')) + repo = pygit2.Repository(os.path.join(os.path.join( + tests.HERE, 'repos'), 'test.git')) + + exp = ['0.1.0', '0.0.21', '0.0.12', '0.0.11', '0.0.3', '0.0.2', + '0.0.1'] + add_repo_tag(repo, exp) + tags = pagure.lib.git.get_git_tags_objects(project) + self.assertEqual(exp, get_tag_name(tags)) + + # Case 3 Sorting with alphas and betas + exp = ['0.1.0', '0.0.21', '0.0.12-beta', '0.0.12-alpha', '0.0.12', + '0.0.11', '0.0.3', '0.0.2', '0.0.1'] + add_repo_tag(repo, ['0.0.12-beta', '0.0.12-alpha']) + tags = pagure.lib.git.get_git_tags_objects(project) + self.assertEqual(exp, get_tag_name(tags)) + + # Case 4 - Sorting with different splitting characters + project = pagure.lib.get_project(self.session, 'test2') + tests.add_readme_git_repo(os.path.join(os.path.join( + tests.HERE, 'repos'), 'test2.git')) + repo = pygit2.Repository(os.path.join(os.path.join( + tests.HERE, 'repos'), 'test2.git')) + + exp = ['15.1-0_0', '1.0-0_14', '1.0-0_2', '1.0-0_1', '0.1-1_0', + '0.1-0_0', '0.0-2_0', '0.0-1_34', '0.0-1_11', '0.0-1_3', + '0.0-1_2', '0.0-1_1'] + add_repo_tag(repo, exp) + + tags = pagure.lib.git.get_git_tags_objects(project) + self.assertEqual(exp, get_tag_name(tags)) + + +if __name__ == '__main__': + SUITE = unittest.TestLoader().loadTestsFromTestCase( + PagureLibGitGetTagstests) + unittest.TextTestRunner(verbosity=2).run(SUITE)