1 # Copyright (c) 2013 New Dream Network, LLC (DreamHost)
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Copyright (C) 2013 Association of Universities for Research in Astronomy
19 # Redistribution and use in source and binary forms, with or without
20 # modification, are permitted provided that the following conditions are met:
22 # 1. Redistributions of source code must retain the above copyright
23 # notice, this list of conditions and the following disclaimer.
25 # 2. Redistributions in binary form must reproduce the above
26 # copyright notice, this list of conditions and the following
27 # disclaimer in the documentation and/or other materials provided
28 # with the distribution.
30 # 3. The name of AURA and its representatives may not be used to
31 # endorse or promote products derived from this software without
32 # specific prior written permission.
34 # THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED
35 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 # DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,
38 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
52 from testtools import matchers
55 from pbr import packaging
56 from pbr.tests import base
59 class TestRepo(fixtures.Fixture):
60 """A git repo for testing with.
62 Use of TempHomeDir with this fixture is strongly recommended as due to the
63 lack of config --local in older gits, it will write to the users global
64 configuration without TempHomeDir.
67 def __init__(self, basedir):
68 super(TestRepo, self).__init__()
69 self._basedir = basedir
72 super(TestRepo, self).setUp()
73 base._run_cmd(['git', 'init', '.'], self._basedir)
75 base._run_cmd(['git', 'add', '.'], self._basedir)
77 def commit(self, message_content='test commit'):
78 files = len(os.listdir(self._basedir))
79 path = self._basedir + '/%d' % files
80 open(path, 'wt').close()
81 base._run_cmd(['git', 'add', path], self._basedir)
82 base._run_cmd(['git', 'commit', '-m', message_content], self._basedir)
85 base._run_cmd(['git', 'reset', '--hard', 'HEAD^'], self._basedir)
87 def tag(self, version):
89 ['git', 'tag', '-sm', 'test tag', version], self._basedir)
92 class GPGKeyFixture(fixtures.Fixture):
93 """Creates a GPG key for testing.
95 It's recommended that this be used in concert with a unique home
100 super(GPGKeyFixture, self).setUp()
101 tempdir = self.useFixture(fixtures.TempDir())
102 gnupg_version_re = re.compile('^gpg\s.*\s([\d+])\.([\d+])\.([\d+])')
103 gnupg_version = base._run_cmd(['gpg', '--version'], tempdir.path)
104 for line in gnupg_version[0].split('\n'):
105 gnupg_version = gnupg_version_re.match(line)
107 gnupg_version = (int(gnupg_version.group(1)),
108 int(gnupg_version.group(2)),
109 int(gnupg_version.group(3)))
112 if gnupg_version is None:
113 gnupg_version = (0, 0, 0)
114 config_file = tempdir.path + '/key-config'
115 f = open(config_file, 'wt')
117 if gnupg_version[0] == 2 and gnupg_version[1] >= 1:
125 Name-Real: Example Key
127 Name-Email: example@example.com
129 Preferences: (setpref)
134 # Note that --quick-random (--debug-quick-random in GnuPG 2.x)
135 # does not have a corresponding preferences file setting and
136 # must be passed explicitly on the command line instead
137 if gnupg_version[0] == 1:
138 gnupg_random = '--quick-random'
139 elif gnupg_version[0] >= 2:
140 gnupg_random = '--debug-quick-random'
144 ['gpg', '--gen-key', '--batch', gnupg_random, config_file],
148 class TestPackagingInGitRepoWithCommit(base.BaseTestCase):
151 ('preversioned', dict(preversioned=True)),
152 ('postversioned', dict(preversioned=False)),
156 super(TestPackagingInGitRepoWithCommit, self).setUp()
157 repo = self.useFixture(TestRepo(self.package_dir))
160 def test_authors(self):
161 self.run_setup('sdist', allow_fail=False)
162 # One commit, something should be in the authors list
163 with open(os.path.join(self.package_dir, 'AUTHORS'), 'r') as f:
165 self.assertNotEqual(body, '')
167 def test_changelog(self):
168 self.run_setup('sdist', allow_fail=False)
169 with open(os.path.join(self.package_dir, 'ChangeLog'), 'r') as f:
171 # One commit, something should be in the ChangeLog list
172 self.assertNotEqual(body, '')
174 def test_manifest_exclude_honoured(self):
175 self.run_setup('sdist', allow_fail=False)
176 with open(os.path.join(
178 'pbr_testpackage.egg-info/SOURCES.txt'), 'r') as f:
181 body, matchers.Not(matchers.Contains('pbr_testpackage/extra.py')))
182 self.assertThat(body, matchers.Contains('pbr_testpackage/__init__.py'))
184 def test_install_writes_changelog(self):
185 stdout, _, _ = self.run_setup(
186 'install', '--root', self.temp_dir + 'installed',
188 self.expectThat(stdout, matchers.Contains('Generating ChangeLog'))
191 class TestPackagingInGitRepoWithoutCommit(base.BaseTestCase):
194 super(TestPackagingInGitRepoWithoutCommit, self).setUp()
195 self.useFixture(TestRepo(self.package_dir))
196 self.run_setup('sdist', allow_fail=False)
198 def test_authors(self):
199 # No commits, no authors in list
200 with open(os.path.join(self.package_dir, 'AUTHORS'), 'r') as f:
202 self.assertEqual(body, '\n')
204 def test_changelog(self):
205 # No commits, nothing should be in the ChangeLog list
206 with open(os.path.join(self.package_dir, 'ChangeLog'), 'r') as f:
208 self.assertEqual(body, 'CHANGES\n=======\n\n')
211 class TestPackagingInPlainDirectory(base.BaseTestCase):
214 super(TestPackagingInPlainDirectory, self).setUp()
216 def test_authors(self):
217 self.run_setup('sdist', allow_fail=False)
218 # Not a git repo, no AUTHORS file created
219 filename = os.path.join(self.package_dir, 'AUTHORS')
220 self.assertFalse(os.path.exists(filename))
222 def test_changelog(self):
223 self.run_setup('sdist', allow_fail=False)
224 # Not a git repo, no ChangeLog created
225 filename = os.path.join(self.package_dir, 'ChangeLog')
226 self.assertFalse(os.path.exists(filename))
228 def test_install_no_ChangeLog(self):
229 stdout, _, _ = self.run_setup(
230 'install', '--root', self.temp_dir + 'installed',
233 stdout, matchers.Not(matchers.Contains('Generating ChangeLog')))
236 class TestPresenceOfGit(base.BaseTestCase):
238 def testGitIsInstalled(self):
239 with mock.patch.object(git,
240 '_run_shell_command') as _command:
241 _command.return_value = 'git version 1.8.4.1'
242 self.assertEqual(True, git._git_is_installed())
244 def testGitIsNotInstalled(self):
245 with mock.patch.object(git,
246 '_run_shell_command') as _command:
247 _command.side_effect = OSError
248 self.assertEqual(False, git._git_is_installed())
251 class TestNestedRequirements(base.BaseTestCase):
253 def test_nested_requirement(self):
254 tempdir = tempfile.mkdtemp()
255 requirements = os.path.join(tempdir, 'requirements.txt')
256 nested = os.path.join(tempdir, 'nested.txt')
257 with open(requirements, 'w') as f:
258 f.write('-r ' + nested)
259 with open(nested, 'w') as f:
261 result = packaging.parse_requirements([requirements])
262 self.assertEqual(result, ['pbr'])
265 class TestVersions(base.BaseTestCase):
268 ('preversioned', dict(preversioned=True)),
269 ('postversioned', dict(preversioned=False)),
273 super(TestVersions, self).setUp()
274 self.repo = self.useFixture(TestRepo(self.package_dir))
275 self.useFixture(GPGKeyFixture())
276 self.useFixture(base.DiveDir(self.package_dir))
278 def test_capitalized_headers(self):
280 self.repo.tag('1.2.3')
281 self.repo.commit('Sem-Ver: api-break')
282 version = packaging._get_version_from_git()
283 self.assertThat(version, matchers.StartsWith('2.0.0.dev1'))
285 def test_capitalized_headers_partial(self):
287 self.repo.tag('1.2.3')
288 self.repo.commit('Sem-ver: api-break')
289 version = packaging._get_version_from_git()
290 self.assertThat(version, matchers.StartsWith('2.0.0.dev1'))
292 def test_tagged_version_has_tag_version(self):
294 self.repo.tag('1.2.3')
295 version = packaging._get_version_from_git('1.2.3')
296 self.assertEqual('1.2.3', version)
298 def test_untagged_version_has_dev_version_postversion(self):
300 self.repo.tag('1.2.3')
302 version = packaging._get_version_from_git()
303 self.assertThat(version, matchers.StartsWith('1.2.4.dev1'))
305 def test_untagged_pre_release_has_pre_dev_version_postversion(self):
307 self.repo.tag('1.2.3.0a1')
309 version = packaging._get_version_from_git()
310 self.assertThat(version, matchers.StartsWith('1.2.3.0a2.dev1'))
312 def test_untagged_version_minor_bump(self):
314 self.repo.tag('1.2.3')
315 self.repo.commit('sem-ver: deprecation')
316 version = packaging._get_version_from_git()
317 self.assertThat(version, matchers.StartsWith('1.3.0.dev1'))
319 def test_untagged_version_major_bump(self):
321 self.repo.tag('1.2.3')
322 self.repo.commit('sem-ver: api-break')
323 version = packaging._get_version_from_git()
324 self.assertThat(version, matchers.StartsWith('2.0.0.dev1'))
326 def test_untagged_version_has_dev_version_preversion(self):
328 self.repo.tag('1.2.3')
330 version = packaging._get_version_from_git('1.2.5')
331 self.assertThat(version, matchers.StartsWith('1.2.5.dev1'))
333 def test_untagged_version_after_pre_has_dev_version_preversion(self):
335 self.repo.tag('1.2.3.0a1')
337 version = packaging._get_version_from_git('1.2.5')
338 self.assertThat(version, matchers.StartsWith('1.2.5.dev1'))
340 def test_untagged_version_after_rc_has_dev_version_preversion(self):
342 self.repo.tag('1.2.3.0a1')
344 version = packaging._get_version_from_git('1.2.3')
345 self.assertThat(version, matchers.StartsWith('1.2.3.0a2.dev1'))
347 def test_preversion_too_low_simple(self):
348 # That is, the target version is either already released or not high
349 # enough for the semver requirements given api breaks etc.
351 self.repo.tag('1.2.3')
353 # Note that we can't target 1.2.3 anymore - with 1.2.3 released we
354 # need to be working on 1.2.4.
355 err = self.assertRaises(
356 ValueError, packaging._get_version_from_git, '1.2.3')
357 self.assertThat(err.args[0], matchers.StartsWith('git history'))
359 def test_preversion_too_low_semver_headers(self):
360 # That is, the target version is either already released or not high
361 # enough for the semver requirements given api breaks etc.
363 self.repo.tag('1.2.3')
364 self.repo.commit('sem-ver: feature')
365 # Note that we can't target 1.2.4, the feature header means we need
366 # to be working on 1.3.0 or above.
367 err = self.assertRaises(
368 ValueError, packaging._get_version_from_git, '1.2.4')
369 self.assertThat(err.args[0], matchers.StartsWith('git history'))
371 def test_get_kwargs_corner_cases(self):
373 git_dir = self.repo._basedir + '/.git'
374 get_kwargs = lambda tag: packaging._get_increment_kwargs(git_dir, tag)
376 def _check_combinations(tag):
378 self.assertEqual(dict(), get_kwargs(tag))
379 self.repo.commit('sem-ver: bugfix')
380 self.assertEqual(dict(), get_kwargs(tag))
381 self.repo.commit('sem-ver: feature')
382 self.assertEqual(dict(minor=True), get_kwargs(tag))
384 self.repo.commit('sem-ver: deprecation')
385 self.assertEqual(dict(minor=True), get_kwargs(tag))
387 self.repo.commit('sem-ver: api-break')
388 self.assertEqual(dict(major=True), get_kwargs(tag))
389 self.repo.commit('sem-ver: deprecation')
390 self.assertEqual(dict(major=True, minor=True), get_kwargs(tag))
391 _check_combinations('')
392 self.repo.tag('1.2.3')
393 _check_combinations('1.2.3')
395 def test_invalid_tag_ignored(self):
396 # Fix for bug 1356784 - we treated any tag as a version, not just those
397 # that are valid versions.
401 # when the tree is tagged and its wrong:
402 self.repo.tag('badver')
403 version = packaging._get_version_from_git()
404 self.assertThat(version, matchers.StartsWith('1.0.1.dev1'))
405 # When the tree isn't tagged, we also fall through.
407 version = packaging._get_version_from_git()
408 self.assertThat(version, matchers.StartsWith('1.0.1.dev2'))
409 # We don't fall through x.y versions
413 self.repo.tag('badver2')
414 version = packaging._get_version_from_git()
415 self.assertThat(version, matchers.StartsWith('1.2.1.dev1'))
418 self.repo.tag('1.2.3')
420 self.repo.tag('badver3')
421 version = packaging._get_version_from_git()
422 self.assertThat(version, matchers.StartsWith('1.2.4.dev1'))
423 # Or alpha/beta/pre versions
425 self.repo.tag('1.2.4.0a1')
427 self.repo.tag('badver4')
428 version = packaging._get_version_from_git()
429 self.assertThat(version, matchers.StartsWith('1.2.4.0a2.dev1'))
430 # Non-release related tags are ignored.
434 self.repo.tag('non-release-tag/2014.12.16-1')
435 version = packaging._get_version_from_git()
436 self.assertThat(version, matchers.StartsWith('2.0.1.dev1'))
438 def test_valid_tag_honoured(self):
439 # Fix for bug 1370608 - we converted any target into a 'dev version'
440 # even if there was a distance of 0 - indicating that we were on the
443 self.repo.tag('1.3.0.0a1')
444 version = packaging._get_version_from_git()
445 self.assertEqual('1.3.0.0a1', version)
447 def test_skip_write_git_changelog(self):
448 # Fix for bug 1467440
450 self.repo.tag('1.2.3')
451 os.environ['SKIP_WRITE_GIT_CHANGELOG'] = '1'
452 version = packaging._get_version_from_git('1.2.3')
453 self.assertEqual('1.2.3', version)
456 super(TestVersions, self).tearDown()
457 os.environ.pop('SKIP_WRITE_GIT_CHANGELOG', None)
460 class TestRequirementParsing(base.BaseTestCase):
462 def test_requirement_parsing(self):
463 tempdir = self.useFixture(fixtures.TempDir()).path
464 requirements = os.path.join(tempdir, 'requirements.txt')
465 with open(requirements, 'wt') as f:
466 f.write(textwrap.dedent(six.u("""\
468 quux<1.0; python_version=='2.6'
469 requests-aws>=0.1.4 # BSD License (3 clause)
470 Routes>=1.12.3,!=2.0,!=2.1;python_version=='2.7'
471 requests-kerberos>=0.6;python_version=='2.7' # MIT
473 setup_cfg = os.path.join(tempdir, 'setup.cfg')
474 with open(setup_cfg, 'wt') as f:
475 f.write(textwrap.dedent(six.u("""\
482 baz>3.2 :python_version=='2.7' # MIT
483 bar>3.3 :python_version=='2.7' # MIT # Apache
485 # pkg_resources.split_sections uses None as the title of an
486 # anonymous section instead of the empty string. Weird.
487 expected_requirements = {
488 None: ['bar', 'requests-aws>=0.1.4'],
489 ":(python_version=='2.6')": ['quux<1.0'],
490 ":(python_version=='2.7')": ['Routes>=1.12.3,!=2.0,!=2.1',
491 'requests-kerberos>=0.6'],
493 "test:(python_version=='2.7')": ['baz>3.2', 'bar>3.3']
496 setup_py = os.path.join(tempdir, 'setup.py')
497 with open(setup_py, 'wt') as f:
498 f.write(textwrap.dedent(six.u("""\
499 #!/usr/bin/env python
502 setup_requires=['pbr'],
507 self._run_cmd(sys.executable, (setup_py, 'egg_info'),
508 allow_fail=False, cwd=tempdir)
509 egg_info = os.path.join(tempdir, 'test_reqparse.egg-info')
511 requires_txt = os.path.join(egg_info, 'requires.txt')
512 with open(requires_txt, 'rt') as requires:
513 generated_requirements = dict(
514 pkg_resources.split_sections(requires))
516 self.assertEqual(expected_requirements, generated_requirements)
519 def load_tests(loader, in_tests, pattern):
520 return testscenarios.load_tests_apply_scenarios(loader, in_tests, pattern)