def test_increment(self): for (initial_version, args), result_version in INCREMENT_CASES.items(): with self.subTest(initial_version=initial_version, args=args, result_version=result_version): self.assertEqual( Version.from_str(initial_version).increment(*args), Version.from_str(result_version))
def test_hash(self): for version, equivalent_version in COMPARISON_CASES_EQUAL.items(): original = Version.from_str(version) equivalent = Version.from_str(equivalent_version) with self.subTest(version=version, equivalent_version=equivalent_version): self.assertEqual(hash(original), hash(equivalent)) self.assertDictEqual({original: equivalent}, {equivalent: original})
def test_from_version(self): for version_str, (args, kwargs) in INIT_CASES.items(): with self.subTest(args=args, kwargs=kwargs, version_str=version_str): version = Version.from_str(version_str) created_version = Version(*args, **dict(kwargs)) self.assertIsInstance(created_version, Version) self.assertEqual(version, created_version) version_copy = Version.from_version(created_version) self.assertEqual(version, version_copy) self.assertEqual(version_copy, created_version)
def test_init(self): for version_str, (args, kwargs) in INIT_CASES.items(): with self.subTest(args=args, kwargs=kwargs, version_str=version_str): version = Version(*args, **dict(kwargs)) self.assertIsInstance(version, Version) self.assertEqual(Version.from_str(version_str), version) self.assertIsInstance(version.release, tuple) if version.pre_release is not None: self.assertIsInstance(version.pre_release, list) if version.has_local: self.assertIsInstance(version.local, tuple)
def test_from_py_version(self): for version_str, (args, kwargs) in COMPATIBLE_STR_CASES.items(): version_tuple = case_to_version_tuple(args, kwargs) with self.subTest(version_str=version_str, version_tuple=version_tuple): py_version = packaging.version.Version(version_str) self.assertEqual( Version.from_py_version(py_version).to_tuple(), version_tuple, py_version) py_version_setuptools = pkg_resources.parse_version( version_str) self.assertEqual( Version.from_py_version(py_version_setuptools).to_tuple(), version_tuple, py_version_setuptools)
def test_from_str(self): for version_str, (args, kwargs) in STR_CASES.items(): version_tuple = case_to_version_tuple(args, kwargs) with self.subTest(version_str=version_str, version_tuple=version_tuple): py_version = \ pkg_resources.parse_version(version_str) # type: packaging.version.Version _LOG.debug('packaging parsed version string %s into %s: %s', repr(version_str), type(py_version), py_version) try: sem_version = semver.parse(version_str) # type: dict _LOG.debug('semver parsed version string %s into %s: %s', repr(version_str), type(sem_version), sem_version) sem_version_info = \ semver.parse_version_info(version_str) # type: semver.VersionInfo _LOG.debug('semver parsed version string %s into %s: %s', repr(version_str), type(sem_version_info), sem_version_info) except ValueError: _LOG.debug('semver could not parse version string %s', repr(version_str)) self.assertEqual( Version.from_str(version_str).to_tuple(), version_tuple)
def test_dirty_repo(self): version = Version.from_str('0.9.0') for new_commit, add in itertools.product((False, True), (False, True)): path = self.git_commit_new_file() self.repo.create_tag(f'v{version}') if new_commit: self.git_commit_new_file() self.git_modify_file(path, add=add) current_version = query_git_repo(self.repo_path) self.assertEqual(version, current_version) upcoming_version = predict_git_repo(self.repo_path) self.assertLess(version, upcoming_version) if new_commit: current_version.devel_increment(1) current_version.local = (self.repo_head_hexsha, ) self.assertTupleEqual(current_version.local, upcoming_version.local[:1]) local_prefix = f'+{self.repo_head_hexsha}.dirty' else: local_prefix = '+dirty' self.assertTrue( upcoming_version.local_to_str().startswith(local_prefix), msg=upcoming_version.local_to_str()) self.assertLess(current_version, upcoming_version) version.increment(VersionComponent.Patch) self.assertLess(upcoming_version, version)
def test_to_py_version(self): for version_str, (args, kwargs) in COMPATIBLE_STR_CASES.items(): version_tuple = case_to_version_tuple(args, kwargs) with self.subTest(version_str=version_str, version_tuple=version_tuple): version = Version.from_str(version_str) py_version = packaging.version.Version(version_str) self.assertEqual(version.to_py_version(), py_version, msg=(version.to_py_version(), py_version))
def test_inconsistent_tag_prefix(self): version = Version.from_str('1.0') for _ in range(5): for version_tag_prefix in ('v', 'ver', ''): self.git_commit_new_file() version.increment(VersionComponent.Minor) self.repo.create_tag(f'{version_tag_prefix}{version}') current_version = query_git_repo(self.repo_path) self.assertEqual(version, current_version) upcoming_version = predict_git_repo(self.repo_path) self.assertEqual(version, upcoming_version)
def test_to_sem_version(self): for version_str, (args, kwargs) in COMPATIBLE_STR_CASES.items(): version_tuple = case_to_version_tuple(args, kwargs) with self.subTest(version_str=version_str, version_tuple=version_tuple): try: sem_version = semver.parse(version_str) except ValueError: continue version = Version.from_str(version_str) self.assertEqual(version.to_sem_version(), sem_version)
def test_increment_bad(self): version = Version(1, 0, 0) with self.assertRaises(TypeError): version.increment(3) with self.assertRaises(TypeError): version.increment('dev') with self.assertRaises(TypeError): version.increment(VersionComponent.Minor, '5') with self.assertRaises(ValueError): version.increment(VersionComponent.Minor, -1) with self.assertRaises(ValueError): version.increment(VersionComponent.Local)
def test_from_sem_version(self): for version_str, (args, kwargs) in COMPATIBLE_STR_CASES.items(): version_tuple = case_to_version_tuple(args, kwargs) with self.subTest(version_str=version_str, version_tuple=version_tuple): try: sem_version = semver.parse(version_str) except ValueError: pass else: self.assertEqual( Version.from_sem_version(sem_version).to_tuple(), version_tuple, sem_version) try: sem_version_info = semver.parse_version_info(version_str) except ValueError: pass else: self.assertEqual( Version.from_sem_version(sem_version_info).to_tuple(), version_tuple, sem_version_info)
def test_nonversion_tags(self): version = Version.from_str('0.1.0') self.git_commit_new_file() self.repo.create_tag(f'v{version}') path = self.git_commit_new_file() self.git_modify_file(path, commit=True) self.repo.create_tag('release1') current_version = query_git_repo(self.repo_path) self.assertEqual(version, current_version) upcoming_version = predict_git_repo(self.repo_path) version.increment(VersionComponent.Patch, 1) version.increment(VersionComponent.DevPatch, 2) version.local = (self.repo_head_hexsha, ) self.assertEqual(version, upcoming_version)
def test_compare(self): for earlier_version, later_version in COMPARISON_CASES_LESS.items(): earlier = Version.from_str(earlier_version) later = Version.from_str(later_version) with self.subTest(earlier_version=earlier_version, later_version=later_version): self.assertLess(earlier, later) self.assertLessEqual(earlier, later) self.assertNotEqual(earlier, later) self.assertNotEqual(later, earlier) self.assertGreaterEqual(later, earlier) self.assertGreater(later, earlier) for version, equivalent_version in COMPARISON_CASES_EQUAL.items(): original = Version.from_str(version) equivalent = Version.from_str(equivalent_version) with self.subTest(version=version, equivalent_version=equivalent_version): self.assertLessEqual(original, equivalent) self.assertLessEqual(equivalent, original) self.assertEqual(original, equivalent) self.assertEqual(equivalent, original) self.assertGreaterEqual(original, equivalent) self.assertGreaterEqual(equivalent, original)
def test_init_bad(self): for (args, kwargs), exception in BAD_INIT_CASES.items(): with self.subTest(args=args, kwargs=kwargs, exception=exception): with self.assertRaises(exception): Version(*args, **dict(kwargs)) version = Version(1, 0) with self.assertRaises(TypeError): version.release = 2 with self.assertRaises(ValueError): version.release = 2, 0 with self.assertRaises(ValueError): version.release = 2, 0, 0, 1 with self.assertRaises(ValueError): version.local = '42', 'and', '43'
def test_to_str(self): for result, (args, kwargs) in STR_CASES.items(): with self.subTest(args=args, kwargs=kwargs, result=result): self.assertEqual(Version(*args, **kwargs).to_str(), result)
def test_from_str_bad(self): with self.assertRaises(ValueError): Version.from_str('hello world')
def test_compare_bad(self): with self.assertRaises(TypeError): assert Version(1, 0) < '2.0'