Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ed65e60b3a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 150 lines (115 sloc) 5.439 kb
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
# Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of versiontools.
#
# versiontools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# versiontools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with versiontools. If not, see <http://www.gnu.org/licenses/>.
import sys

from distutils.dist import Distribution
from distutils.errors import DistutilsSetupError

from unittest import TestCase

from versiontools import Version
from versiontools.setuptools_hooks import version as handle_version


class VersionFormattingTests(TestCase):

    def setUp(self):
        # Inhibit Version.vcs from working
        self._real_vcs = Version.vcs
        Version.vcs = property(lambda self: None)

    def tearDown(self):
        Version.vcs = self._real_vcs

    def test_defaults(self):
        self.assertEqual(Version(1, 0), (1, 0, 0, "final", 0))

    def test_serial_cannot_be_zero_for_certain_releaselevel(self):
        self.assertRaises(ValueError, Version, 1, 2, 3, "alpha", 0)
        self.assertRaises(ValueError, Version, 1, 2, 3, "beta", 0)
        self.assertRaises(ValueError, Version, 1, 2, 3, "candidate", 0)

    def test_serial_can_be_zero_for_certain_releaselevel(self):
        self.assertEqual(Version(1, 2, 3, "final", 0).serial, 0)
        self.assertEqual(Version(1, 2, 3, "dev", 0).serial, 0)

    def test_releaselevel_values(self):
        self.assertRaises(ValueError, Version, 1, 2, 3, "foobar", 0)

    def test_accessors(self):
        version = Version(1, 2, 3, "dev", 4)
        self.assertEqual(version.major, 1)
        self.assertEqual(version.minor, 2)
        self.assertEqual(version.micro, 3)
        self.assertEqual(version.releaselevel, "dev")
        self.assertEqual(version.serial, 4)

    def test_positional_accessors(self):
        version = Version(1, 2, 3, "dev", 4)
        self.assertEqual(version[0], 1)
        self.assertEqual(version[1], 2)
        self.assertEqual(version[2], 3)
        self.assertEqual(version[3], "dev")
        self.assertEqual(version[4], 4)

    def test_formatting_zero_micro_discarded(self):
        self.assertEqual(str(Version(1, 0)), "1.0")
        self.assertEqual(str(Version(1, 0, 0)), "1.0")

    def test_formatting_nonzero_micro_retained(self):
        self.assertEqual(str(Version(1, 0, 1)), "1.0.1")

    def test_formatting_serial_not_used_for_development(self):
        self.assertEqual(str(Version(1, 2, 3, "dev", 4)), "1.2.3.dev")

    def test_formatting_serial_not_used_for_final(self):
        self.assertEqual(str(Version(1, 2, 3, "final", 4)), "1.2.3")

    def test_formatting_serial_used_for_alpha_beta_and_candidate(self):
        self.assertEqual(str(Version(1, 2, 3, "alpha", 4)), "1.2.3a4")
        self.assertEqual(str(Version(1, 2, 3, "beta", 4)), "1.2.3b4")
        self.assertEqual(str(Version(1, 2, 3, "candidate", 4)), "1.2.3c4")


class MockedVCS(object):

    def __init__(self, revno):
        self.revno = revno


class VersionFormattingTestsWithMockedVCS(TestCase):

    def setUp(self):
        # Inhibit Version.vcs from working
        self._real_vcs = Version.vcs
        self.mocked_vcs = None
        Version.vcs = property(lambda x: self.mocked_vcs)

    def mock_vcs_revno(self, revno):
        self.mocked_vcs = MockedVCS(revno)

    def tearDown(self):
        Version.vcs = self._real_vcs

    def test_formatting_without_vcs(self):
        version = Version(1, 2, 3, "dev", 4)
        self.assertEqual(str(version), "1.2.3.dev")

    def test_formatting_with_vcs_and_revno(self):
        self.mock_vcs_revno(5)
        version = Version(1, 2, 3, "dev", 4)
        self.assertEqual(str(version), "1.2.3.dev5")

    def test_formatting_no_dev_suffix_for_alpha_beta_and_candidate(self):
        self.mock_vcs_revno(5)
        self.assertEqual(str(Version(1, 2, 3, "alpha", 4)), "1.2.3a4")
        self.assertEqual(str(Version(1, 2, 3, "beta", 4)), "1.2.3b4")
        self.assertEqual(str(Version(1, 2, 3, "candidate", 4)), "1.2.3c4")


class HandleVersionTests(TestCase):

    def setUp(self):
        self.dist = Distribution()

    def test_cant_import(self):
        version = ':versiontools:nonexisting:'
        try:
            handle_version(self.dist, None, version)
        except Exception:
            e = sys.exc_info()[1]
            self.assertTrue(isinstance(e, DistutilsSetupError))
            self.assertEqual(str(e), "Unable to import 'nonexisting': "
                                      "No module named nonexisting")

    def test_not_found(self):
        version = ':versiontools:versiontools:__nonexisting__'
        try:
            handle_version(self.dist, None, version)
        except Exception:
            e = sys.exc_info()[1]
            self.assertTrue(isinstance(e, DistutilsSetupError))
            self.assertEqual(str(e), "Unable to access '__nonexisting__' in "
                                      "'versiontools': 'module' object has "
                                      "no attribute '__nonexisting__'")
Something went wrong with that request. Please try again.