From 9b8016a2b1e75fda42fe7b7bf37475a8c64dc6b2 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Wed, 16 Feb 2022 17:47:22 +0000 Subject: Add pre-commit and woke configuration --- .pre-commit-config.yaml | 14 + .woke.yaml | 221 ++++++++++++ bin/git-build-recipe | 12 +- gitbuildrecipe/deb_util.py | 12 +- gitbuildrecipe/deb_version.py | 12 +- gitbuildrecipe/main.py | 12 +- gitbuildrecipe/recipe.py | 14 +- gitbuildrecipe/tests/__init__.py | 12 +- gitbuildrecipe/tests/test_blackbox.py | 581 ------------------------------- gitbuildrecipe/tests/test_deb_version.py | 12 +- gitbuildrecipe/tests/test_functional.py | 581 +++++++++++++++++++++++++++++++ gitbuildrecipe/tests/test_recipe.py | 12 +- setup.py | 12 +- tox.ini | 10 + 14 files changed, 881 insertions(+), 636 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 .woke.yaml delete mode 100644 gitbuildrecipe/tests/test_blackbox.py create mode 100644 gitbuildrecipe/tests/test_functional.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4737b06 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.1.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/get-woke/woke + rev: v0.17.1 + hooks: + - id: woke-from-source diff --git a/.woke.yaml b/.woke.yaml new file mode 100644 index 0000000..010ccff --- /dev/null +++ b/.woke.yaml @@ -0,0 +1,221 @@ + +# Based on Canonical's Guidelines +# https://docs.google.com/document/d/1mJUa1VJHOMWa723dmKmNjSKGW-nlBv9xktzGZZwacVo/edit#heading=h.5efudb237qdb +rules: + - name: whitelist + terms: + - whitelist + - white-list + - whitelisted + - white-listed + alternatives: + - allowlist + - inclusion list + severity: warning + note: "The underlying assumption of the whitelist/blacklist metaphor is that white = good and black = bad. Because colors in and of themselves have no predetermined meaning, any meaning we assign to them is cultural: for example, the color red in many Southeast Asian countries is lucky, and is often associated with events like marriages, whereas the color white carries the same connotations in many European countries. In the case of whitelist/blacklist, the terms originate in the publishing industry – one dominated by the USA and England, two countries which participated in slavery and which grapple with their racist legacies to this day." + + - name: blacklist + terms: + - blacklist + - black-list + - blacklisted + - black-listed + alternatives: + - denylist + - blocklist + - exclusion list + severity: warning + note: "The underlying assumption of the whitelist/blacklist metaphor is that white = good and black = bad. Because colors in and of themselves have no predetermined meaning, any meaning we assign to them is cultural: for example, the color red in many Southeast Asian countries is lucky, and is often associated with events like marriages, whereas the color white carries the same connotations in many European countries. In the case of whitelist/blacklist, the terms originate in the publishing industry – one dominated by the USA and England, two countries which participated in slavery and which grapple with their racist legacies to this day." + + - name: master-slave + terms: + - master-slave + - master/slave + alternatives: + - leader/follower + - main/replica + - primary/replica + - primary/standby + - primary/secondary + + - name: slave + terms: + - slave + alternatives: + - follower + - replica + - standby + + - name: grandfathered + terms: + - grandfathered + alternatives: + - legacy status + - legacied + - exempted + - carried + - brought forward + - rolled over + + - name: man-hours + terms: + - man hours + - man-hours + alternatives: + - person hours + - engineer hours + + - name: sanity + terms: + - sanity + alternatives: + - confidence + - quick check + - coherence check + + - name: dummy + terms: + - dummy + alternatives: + - placeholder + - sample + + - name: guys + terms: + - guys + alternatives: + - folks + - people + - you all + - y'all + - yinz + + - name: whitebox + terms: + - white-box + - whitebox + - white box + alternatives: + - open-box + + - name: blackbox + terms: + - black-box + - blackbox + - black box + alternatives: + - closed-box + + - name: blackhat + terms: + - blackhat + - black-hat + - black hat + alternatives: + - attacker + - malicious actor + + - name: whitehat + terms: + - whitehat + - white-hat + - white hat + alternatives: + - researcher + - security specialist + + - name: illegal characters + terms: + - illegal characters + alternatives: + - invalid characters + - unsupported characters + - special characters + + - name: native feature + terms: + - native feature + alternatives: + - core feature + - built-in feature + + - name: chairman/foreman + terms: + - chairman + - foreman + alternatives: + - chair + - foreperson + + - name: man in the middle + terms: + - man in the middle + - man-in-the-middle + alternatives: + - machine-in-the-middle + - person-in-the-middle + - system-in-the-middle + - intermediary attack + + - name: middleman + terms: + - middleman + alternatives: + - middleperson + - intermediary + + - name: manned + terms: + - manned + alternatives: + - crewed + - staffed + - monitored + - human operated + + - name: mom test / girlfriend test + terms: + - mom test + - girlfriend test + alternatives: + - user test + - user friendly + + - name: crazy + terms: + - crazy + alternatives: + - baffling + - unexplained + - errant + + - name: cripples + terms: + - cripples + alternatives: + - slows down + - hinders + - obstructs + + - name: crippling + terms: + - crippling + alternatives: + - attenuating + - incapacitating + + - name: stonith/stomith + terms: + - stonith + - stomith + alternatives: + - fence failed nodes + - machines + + - name: demilitarized zone + terms: + - demilitarized zone + - dmz + alternatives: + - perimeter network + - passthrough network diff --git a/bin/git-build-recipe b/bin/git-build-recipe index 3f4bd34..2e34b58 100755 --- a/bin/git-build-recipe +++ b/bin/git-build-recipe @@ -2,16 +2,16 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . """A Git command to construct trees based on recipes.""" diff --git a/gitbuildrecipe/deb_util.py b/gitbuildrecipe/deb_util.py index 56778e5..d0a6666 100644 --- a/gitbuildrecipe/deb_util.py +++ b/gitbuildrecipe/deb_util.py @@ -1,15 +1,15 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . import email.utils diff --git a/gitbuildrecipe/deb_version.py b/gitbuildrecipe/deb_version.py index 2576357..f70bc49 100644 --- a/gitbuildrecipe/deb_version.py +++ b/gitbuildrecipe/deb_version.py @@ -1,15 +1,15 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . """Handle deb-version and related substitutions.""" diff --git a/gitbuildrecipe/main.py b/gitbuildrecipe/main.py index 6294852..30e6bec 100644 --- a/gitbuildrecipe/main.py +++ b/gitbuildrecipe/main.py @@ -2,16 +2,16 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . """A Git command to construct trees based on recipes.""" diff --git a/gitbuildrecipe/recipe.py b/gitbuildrecipe/recipe.py index 0ff215f..f932ca7 100644 --- a/gitbuildrecipe/recipe.py +++ b/gitbuildrecipe/recipe.py @@ -1,15 +1,15 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . """Handle recipe parsing and substitution variables.""" @@ -845,7 +845,7 @@ class BaseRecipeBranch(RecipeBranch): manifest += self._add_child_branches_to_manifest( self.child_branches, 0) if validate: - # Sanity check. + # Ensure that the recipe can be parsed. # TODO: write a function that compares the result of this parse # with the branch that we built it from. RecipeParser(manifest).parse() diff --git a/gitbuildrecipe/tests/__init__.py b/gitbuildrecipe/tests/__init__.py index 6b41396..d968f5b 100644 --- a/gitbuildrecipe/tests/__init__.py +++ b/gitbuildrecipe/tests/__init__.py @@ -1,15 +1,15 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . import os.path diff --git a/gitbuildrecipe/tests/test_blackbox.py b/gitbuildrecipe/tests/test_blackbox.py deleted file mode 100644 index c951a19..0000000 --- a/gitbuildrecipe/tests/test_blackbox.py +++ /dev/null @@ -1,581 +0,0 @@ -# Copyright 2015 Canonical Ltd. - -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published -# by the Free Software Foundation. - -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR -# PURPOSE. See the GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License along -# with this program. If not, see . - -import hashlib -import os.path -import re -import shlex -import signal -import subprocess -import sys -from textwrap import dedent -import unittest - -try: - from debian import changelog -except ImportError: - from debian_bundle import changelog -from fixtures import EnvironmentVariable -from testtools.content import text_content -from testtools.matchers import ( - EndsWith, - FileContains, - MatchesRegex, - Not, - PathExists, - StartsWith, - ) - -from gitbuildrecipe.tests import ( - GitRepository, - GitTestCase, - ) - - -def skipIfFakeroot(): - return unittest.skipIf( - "FAKEROOTKEY" in os.environ, "running under fakeroot") - - -def requirePristineTar(): - path = "/usr/bin/pristine-tar" - return unittest.skipUnless(os.path.exists(path), "requires %s" % path) - - -def sha1_file_by_name(name): - s = hashlib.sha1() - with open(name, "rb") as f: - while True: - buf = f.read(1 << 16) - if not buf: - break - s.update(buf) - return s.hexdigest() - - -def make_pristine_tar_delta(dest, tarball_path): - """Create a pristine-tar delta for a tarball. - - :param dest: Directory to generate pristine tar delta for - :param tarball_path: Path to the tarball - :return: pristine-tarball - """ - def subprocess_setup(): - signal.signal(signal.SIGPIPE, signal.SIG_DFL) - # If tarball_path is relative, the cwd=dest parameter to Popen will make - # pristine-tar faaaail. pristine-tar doesn't use the VFS either, so we - # assume local paths. - tarball_path = os.path.abspath(tarball_path) - command = ["pristine-tar", "gendelta", tarball_path, "-"] - proc = subprocess.Popen(command, stdout=subprocess.PIPE, - cwd=dest, preexec_fn=subprocess_setup, - stderr=subprocess.PIPE) - stdout, stderr = proc.communicate() - if proc.returncode != 0: - raise Exception("Generating delta from tar failed: %s" % stderr) - return stdout - - -class BlackboxBuilderTests(GitTestCase): - - def setUp(self): - super().setUp() - self.use_temp_dir() - # Replace DEBEMAIL and DEBFULLNAME so that they are known values - # for the changelog checks. - self.useFixture(EnvironmentVariable("DEBEMAIL", "maint@maint.org")) - self.useFixture(EnvironmentVariable("DEBFULLNAME", "M. Maintainer")) - - def _get_file_contents(self, filename, mode="r"): - """Helper to read contents of a file - - Use FileContains instead to just assert the contents match.""" - self.assertThat(filename, PathExists()) - with open(filename, mode) as f: - return f.read() - - def run_recipe(self, args, retcode=0): - cmd = [sys.executable, "-m", "gitbuildrecipe.main"] - if isinstance(args, str): - cmd += shlex.split(args) - else: - cmd += list(args) - process = subprocess.Popen( - cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - stdout, stderr = process.communicate() - if stdout: - self.addDetail("stdout", text_content(stdout)) - if stderr: - self.addDetail("stderr", text_content(stderr)) - self.assertEqual(retcode, process.returncode) - return stdout, stderr - - @skipIfFakeroot() - def test_cmd_dailydeb(self): - #TODO: define a test feature for debuild and require it here. - source = GitRepository("source") - source.build_tree(["a", "debian/"]) - source.build_tree_contents([ - ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), - ("debian/control", - "Source: foo\nMaintainer: maint maint@maint.org\n\n" - "Package: foo\nArchitecture: all\n")]) - source.add(["a", "debian/rules", "debian/control"]) - commit = source.commit("one") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe("--manifest manifest test.recipe working") - self.assertThat("working/a", Not(PathExists())) - package_root = "working/test/" - self.assertThat(os.path.join(package_root, "a"), PathExists()) - self.assertThat( - os.path.join(package_root, "debian/git-build-recipe.manifest"), - PathExists()) - self.assertThat("manifest", PathExists()) - self.assertThat("manifest", FileContains( - "# git-build-recipe format 0.1 deb-version 1\n" - "source git-commit:%s\n" % commit)) - self.assertThat( - os.path.join(package_root, "debian/git-build-recipe.manifest"), - FileContains( - "# git-build-recipe format 0.1 deb-version 1\n" - "source git-commit:%s\n" % commit)) - self.assertThat( - os.path.join(package_root, "debian/changelog"), - FileContains(matcher=StartsWith( - "foo (1) unstable; urgency=low\n"))) - - @skipIfFakeroot() - def test_cmd_dailydeb_no_work_dir(self): - #TODO: define a test feature for debuild and require it here. - source = GitRepository("source") - source.build_tree(["a", "debian/"]) - source.build_tree_contents([ - ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), - ("debian/control", - "Source: foo\nMaintainer: maint maint@maint.org\n\n" - "Package: foo\nArchitecture: all\n")]) - source.add(["a", "debian/rules", "debian/control"]) - source.commit("one") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe("--manifest manifest test.recipe") - - @skipIfFakeroot() - def test_cmd_dailydeb_if_changed_from_non_existent(self): - #TODO: define a test feature for debuild and require it here. - source = GitRepository("source") - source.build_tree(["a", "debian/"]) - source.build_tree_contents([ - ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), - ("debian/control", - "Source: foo\nMaintainer: maint maint@maint.org\n" - "\nPackage: foo\nArchitecture: all\n")]) - source.add(["a", "debian/rules", "debian/control"]) - source.commit("one") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--manifest manifest --if-changed-from bar test.recipe") - - def make_upstream_version(self, source, package_name, version, contents, - pristine_tar_format=None): - source._git_call("checkout", "-q", "--orphan", "upstream") - source._git_call("rm", "-qrf", ".") - source.build_tree_contents(contents) - source.add(["."]) - commit = source.commit("import upstream %s" % version) - cmd = [] - if pristine_tar_format in (None, "gz"): - tarfile_path = "%s_%s.orig.tar.gz" % (package_name, version) - elif pristine_tar_format == "bz2": - tarfile_path = "%s_%s.orig.tar.bz2" % (package_name, version) - cmd.extend(["-c", "tar.tar.bz2.command=bzip2 -c"]) - else: - raise AssertionError( - "unknown pristine tar format %s" % pristine_tar_format) - cmd.extend([ - "archive", - "--prefix=upstream/", - "-o", os.path.abspath(tarfile_path), - "upstream", - ]) - source._git_call(*cmd) - subprocess.check_call( - ["pristine-tar", "commit", os.path.abspath(tarfile_path)], - stderr=subprocess.DEVNULL, cwd=source.path) - tarfile_sha1 = sha1_file_by_name(tarfile_path) - source.tag("upstream/%s" % version, commit) - source._git_call("checkout", "-q", "master") - return tarfile_sha1 - - def make_simple_package(self, path): - source = GitRepository(path) - source.build_tree(["a", "debian/"]) - cl_contents = ("package (0.1-1) unstable; urgency=low\n * foo\n" - " -- maint Tue, 04 Aug 2009 " - "10:03:10 +0100\n") - source.build_tree_contents([ - ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), - ("debian/control", - "Source: package\nMaintainer: maint maint@maint.org\n\n" - "Package: package\nArchitecture: all\n"), - ("debian/changelog", cl_contents) - ]) - source.add(["a", "debian/rules", "debian/control", "debian/changelog"]) - source.commit("one") - return source - - def test_cmd_dailydeb_no_build(self): - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--manifest manifest --no-build test.recipe working") - new_cl_contents = ( - "package (1) unstable; urgency=low\n\n" - " * Auto build.\n\n -- M. Maintainer ") - self.assertThat( - "working/test/debian/changelog", - FileContains(matcher=StartsWith(new_cl_contents))) - for fn in os.listdir("working"): - self.assertThat(fn, Not(EndsWith(".changes"))) - - @skipIfFakeroot() - def test_cmd_dailydeb_with_package_from_changelog(self): - #TODO: define a test feature for debuild and require it here. - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--manifest manifest --if-changed-from bar test.recipe working") - new_cl_contents = ( - "package (1) unstable; urgency=low\n\n" - " * Auto build.\n\n -- M. Maintainer ") - self.assertThat( - "working/test/debian/changelog", - FileContains(matcher=StartsWith(new_cl_contents))) - - @skipIfFakeroot() - def test_cmd_dailydeb_with_version_from_changelog(self): - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version {debversion}-2\n" - "source\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - cl = changelog.Changelog(self._get_file_contents( - "working/test/debian/changelog")) - self.assertEqual("0.1-1-2", str(cl._blocks[0].version)) - - @skipIfFakeroot() - def test_cmd_dailydeb_with_version_from_other_branch_changelog(self): - self.make_simple_package("source") - other = self.make_simple_package("other") - cl_contents = ("package (0.4-1) unstable; urgency=low\n * foo\n" - " -- maint Tue, 04 Aug 2009 " - "10:03:10 +0100\n") - other.build_tree_contents([("debian/changelog", cl_contents)]) - other.add(["debian/changelog"]) - other.commit("new changelog entry") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.4 " - "deb-version {debversion:other}.2\n" - "source\n" - "nest other other other\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - cl = changelog.Changelog(self._get_file_contents( - "working/test/debian/changelog")) - self.assertEqual("0.4-1.2", str(cl._blocks[0].version)) - - @skipIfFakeroot() - def test_cmd_dailydeb_with_upstream_version_from_changelog(self): - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version {debupstream}-2\n" - "source\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - new_cl_contents = ( - "package (0.1-2) unstable; urgency=low\n\n" - " * Auto build.\n\n -- M. Maintainer ") - self.assertThat( - "working/test/debian/changelog", - FileContains(matcher=StartsWith(new_cl_contents))) - - @skipIfFakeroot() - def test_cmd_dailydeb_with_append_version(self): - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--append-version ~ppa1 test.recipe working") - new_cl_contents = ( - "package (1~ppa1) unstable; urgency=low\n\n" - " * Auto build.\n\n -- M. Maintainer ") - self.assertThat( - "working/test/debian/changelog", - FileContains(matcher=StartsWith(new_cl_contents))) - - def test_cmd_dailydeb_with_nonascii_maintainer_in_changelog(self): - self.useFixture(EnvironmentVariable( - "DEBFULLNAME", u"Micha\u25c8 Sawicz")) - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version 1\nsource\n") - out, err = self.run_recipe("test.recipe working") - new_cl_contents = ( - "package (1) unstable; urgency=low\n\n" - " * Auto build.\n\n" - u" -- Micha\u25c8 Sawicz ") - self.assertThat( - "working/test/debian/changelog", - FileContains(matcher=StartsWith(new_cl_contents))) - - def test_cmd_dailydeb_with_invalid_version(self): - source = GitRepository("source") - source.build_tree(["a"]) - source.build_tree_contents([ - ("debian/", None), - ("debian/control", - "Source: foo\nMaintainer: maint maint@maint.org\n") - ]) - source.add(["a", "debian/control"]) - source.commit("one") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.1 deb-version $\nsource\n") - err = self.run_recipe("test.recipe working", retcode=1)[1] - self.assertThat(err, MatchesRegex( - r".*Invalid deb-version: \$: " - r"(Could not parse version: \$|Invalid version string '\$')\n", - flags=re.S)) - - def test_cmd_dailydeb_with_safe(self): - self.make_simple_package("source") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1\n" - "source\nrun something bad") - out, err = self.run_recipe("--safe test.recipe working", retcode=1) - self.assertIn("The 'run' instruction is forbidden.\n", err) - - def make_simple_quilt_package(self): - source = self.make_simple_package("source") - source.build_tree(["debian/source/"]) - source.build_tree_contents([ - ("debian/source/format", "3.0 (quilt)\n"), - ("debian/source/options", 'compression = "gzip"\n')]) - source.add(["debian/source/format", "debian/source/options"]) - source.commit("set source format") - return source - - def test_cmd_dailydeb_missing_orig_tarball(self): - self.make_simple_quilt_package() - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1-1\nsource\n") - out, err = self.run_recipe("test.recipe working", retcode=2) - self.assertIn( - 'error: Unable to find the upstream source. ' - 'Import it as tag upstream/1 or build with ' - '--allow-fallback-to-native.\n', err) - - @skipIfFakeroot() - def test_cmd_dailydeb_with_orig_tarball(self): - source = self.make_simple_package("source") - self.make_upstream_version( - source, "package", "0.1", [("file", "content\n")]) - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") - out, err = self.run_recipe("test.recipe working") - self.assertThat("working/package_0.1.orig.tar.gz", PathExists()) - self.assertThat("working/package_0.1-1.diff.gz", PathExists()) - - @skipIfFakeroot() - @requirePristineTar() - def test_cmd_dailydeb_with_pristine_orig_gz_tarball(self): - source = self.make_simple_package("source") - pristine_tar_sha1 = self.make_upstream_version( - source, "package", "0.1", [("file", "content\n")], - pristine_tar_format="gz") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") - out, err = self.run_recipe("test.recipe working") - self.assertThat("working/package_0.1.orig.tar.gz", PathExists()) - self.assertThat("working/package_0.1-1.diff.gz", PathExists()) - self.assertEqual( - sha1_file_by_name("working/package_0.1.orig.tar.gz"), - pristine_tar_sha1) - - @skipIfFakeroot() - @requirePristineTar() - def test_cmd_dailydeb_with_pristine_orig_bz2_tarball(self): - source = self.make_simple_quilt_package() - pristine_tar_sha1 = self.make_upstream_version( - source, "package", "0.1", - [("file", "content\n"), ("a", "contents of a\n")], - pristine_tar_format="bz2") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") - source.build_tree([("upstream/")]) - source.build_tree_contents([ - ("upstream/file", "content\n"), - ("upstream/a", "contents of a\n")]) - source.add(["upstream/file", "upstream/a"]) - out, err = self.run_recipe("test.recipe working") - self.assertThat("working/package_0.1.orig.tar.bz2", PathExists()) - self.assertThat("working/package_0.1-1.debian.tar.gz", PathExists()) - self.assertEqual( - sha1_file_by_name("working/package_0.1.orig.tar.bz2"), - pristine_tar_sha1) - - @requirePristineTar() - def test_cmd_dailydeb_allow_fallback_to_native_with_orig_tarball(self): - source = self.make_simple_quilt_package() - pristine_tar_sha1 = self.make_upstream_version( - source, "package", "0.1", - [("file", "content\n"), ("a", "contents of a\n")], - pristine_tar_format="bz2") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") - source.build_tree([("upstream/")]) - source.build_tree_contents([ - ("upstream/file", "content\n"), - ("upstream/a", "contents of a\n")]) - source.add(["upstream/file", "upstream/a"]) - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - self.assertThat("working/package_0.1.orig.tar.bz2", PathExists()) - self.assertThat("working/package_0.1-1.debian.tar.gz", PathExists()) - self.assertThat( - "source/debian/source/format", FileContains("3.0 (quilt)\n")) - self.assertEqual( - sha1_file_by_name("working/package_0.1.orig.tar.bz2"), - pristine_tar_sha1) - - @skipIfFakeroot() - def test_cmd_dailydeb_force_native(self): - self.make_simple_quilt_package() - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - self.assertThat( - "working/test/debian/source/format", - FileContains("3.0 (native)\n")) - - @skipIfFakeroot() - def test_cmd_dailydeb_force_native_empty_series(self): - source = self.make_simple_quilt_package() - source.build_tree(['debian/patches/']) - source.build_tree_contents([("debian/patches/series", "\n")]) - source.add(["debian/patches/series"]) - source.commit("add patches") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - self.assertThat( - "working/test/debian/source/format", - FileContains("3.0 (native)\n")) - self.assertThat("working/test/debian/patches", Not(PathExists())) - - @skipIfFakeroot() - def test_cmd_dailydeb_force_native_apply_quilt(self): - source = self.make_simple_quilt_package() - source.build_tree(["debian/patches/"]) - patch = dedent("""\ - diff -ur a/thefile b/thefile - --- a/thefile 2010-12-05 20:14:22.000000000 +0100 - +++ b/thefile 2010-12-05 20:14:26.000000000 +0100 - @@ -1 +1 @@ - -old-contents - +new-contents - """) - source.build_tree_contents([ - ("thefile", "old-contents\n"), - ("debian/patches/series", "01_foo.patch"), - ("debian/patches/01_foo.patch", patch)]) - source.add([ - "thefile", "debian/patches/series", - "debian/patches/01_foo.patch"]) - source.commit("add patch") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1\nsource\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working") - self.assertThat( - "working/test/debian/source/format", - FileContains("3.0 (native)\n")) - self.assertThat("working/test/thefile", FileContains("new-contents\n")) - self.assertThat("working/test/debian/patches", Not(PathExists())) - - def test_cmd_dailydeb_force_native_apply_quilt_failure(self): - source = self.make_simple_quilt_package() - source.build_tree(["debian/patches/"]) - patch = dedent("""\ - diff -ur a/thefile b/thefile - --- a/thefile 2010-12-05 20:14:22.000000000 +0100 - +++ b/thefile 2010-12-05 20:14:26.000000000 +0100 - @@ -1 +1 @@ - -old-contents - +new-contents - """) - source.build_tree_contents([ - ("thefile", "contents\n"), - ("debian/patches/series", "01_foo.patch"), - ("debian/patches/01_foo.patch", patch)]) - source.add([ - "thefile", "debian/patches/series", - "debian/patches/01_foo.patch"]) - source.commit("add patch") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1-1\nsource\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working", retcode=1) - self.assertIn("Failed to apply quilt patches\n", err) - - def test_unknown_source_format(self): - source = self.make_simple_package("source") - source.build_tree(["debian/source/"]) - source.build_tree_contents([("debian/source/format", "2.0\n")]) - source.add(["debian/source/format"]) - source.commit("set source format") - with open("test.recipe", "w") as recipe: - recipe.write( - "# git-build-recipe format 0.3 deb-version 1-1\nsource\n") - out, err = self.run_recipe( - "--allow-fallback-to-native test.recipe working", retcode=1) - self.assertIn("Unknown source format 2.0\n", err) diff --git a/gitbuildrecipe/tests/test_deb_version.py b/gitbuildrecipe/tests/test_deb_version.py index f58f56f..662e811 100644 --- a/gitbuildrecipe/tests/test_deb_version.py +++ b/gitbuildrecipe/tests/test_deb_version.py @@ -1,15 +1,15 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . import datetime diff --git a/gitbuildrecipe/tests/test_functional.py b/gitbuildrecipe/tests/test_functional.py new file mode 100644 index 0000000..56fd216 --- /dev/null +++ b/gitbuildrecipe/tests/test_functional.py @@ -0,0 +1,581 @@ +# Copyright 2015 Canonical Ltd. + +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published +# by the Free Software Foundation. + +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# PURPOSE. See the GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . + +import hashlib +import os.path +import re +import shlex +import signal +import subprocess +import sys +from textwrap import dedent +import unittest + +try: + from debian import changelog +except ImportError: + from debian_bundle import changelog +from fixtures import EnvironmentVariable +from testtools.content import text_content +from testtools.matchers import ( + EndsWith, + FileContains, + MatchesRegex, + Not, + PathExists, + StartsWith, + ) + +from gitbuildrecipe.tests import ( + GitRepository, + GitTestCase, + ) + + +def skipIfFakeroot(): + return unittest.skipIf( + "FAKEROOTKEY" in os.environ, "running under fakeroot") + + +def requirePristineTar(): + path = "/usr/bin/pristine-tar" + return unittest.skipUnless(os.path.exists(path), "requires %s" % path) + + +def sha1_file_by_name(name): + s = hashlib.sha1() + with open(name, "rb") as f: + while True: + buf = f.read(1 << 16) + if not buf: + break + s.update(buf) + return s.hexdigest() + + +def make_pristine_tar_delta(dest, tarball_path): + """Create a pristine-tar delta for a tarball. + + :param dest: Directory to generate pristine tar delta for + :param tarball_path: Path to the tarball + :return: pristine-tarball + """ + def subprocess_setup(): + signal.signal(signal.SIGPIPE, signal.SIG_DFL) + # If tarball_path is relative, the cwd=dest parameter to Popen will make + # pristine-tar faaaail. pristine-tar doesn't use the VFS either, so we + # assume local paths. + tarball_path = os.path.abspath(tarball_path) + command = ["pristine-tar", "gendelta", tarball_path, "-"] + proc = subprocess.Popen(command, stdout=subprocess.PIPE, + cwd=dest, preexec_fn=subprocess_setup, + stderr=subprocess.PIPE) + stdout, stderr = proc.communicate() + if proc.returncode != 0: + raise Exception("Generating delta from tar failed: %s" % stderr) + return stdout + + +class FunctionalBuilderTests(GitTestCase): + + def setUp(self): + super().setUp() + self.use_temp_dir() + # Replace DEBEMAIL and DEBFULLNAME so that they are known values + # for the changelog checks. + self.useFixture(EnvironmentVariable("DEBEMAIL", "maint@maint.org")) + self.useFixture(EnvironmentVariable("DEBFULLNAME", "M. Maintainer")) + + def _get_file_contents(self, filename, mode="r"): + """Helper to read contents of a file + + Use FileContains instead to just assert the contents match.""" + self.assertThat(filename, PathExists()) + with open(filename, mode) as f: + return f.read() + + def run_recipe(self, args, retcode=0): + cmd = [sys.executable, "-m", "gitbuildrecipe.main"] + if isinstance(args, str): + cmd += shlex.split(args) + else: + cmd += list(args) + process = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True) + stdout, stderr = process.communicate() + if stdout: + self.addDetail("stdout", text_content(stdout)) + if stderr: + self.addDetail("stderr", text_content(stderr)) + self.assertEqual(retcode, process.returncode) + return stdout, stderr + + @skipIfFakeroot() + def test_cmd_dailydeb(self): + #TODO: define a test feature for debuild and require it here. + source = GitRepository("source") + source.build_tree(["a", "debian/"]) + source.build_tree_contents([ + ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), + ("debian/control", + "Source: foo\nMaintainer: maint maint@maint.org\n\n" + "Package: foo\nArchitecture: all\n")]) + source.add(["a", "debian/rules", "debian/control"]) + commit = source.commit("one") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe("--manifest manifest test.recipe working") + self.assertThat("working/a", Not(PathExists())) + package_root = "working/test/" + self.assertThat(os.path.join(package_root, "a"), PathExists()) + self.assertThat( + os.path.join(package_root, "debian/git-build-recipe.manifest"), + PathExists()) + self.assertThat("manifest", PathExists()) + self.assertThat("manifest", FileContains( + "# git-build-recipe format 0.1 deb-version 1\n" + "source git-commit:%s\n" % commit)) + self.assertThat( + os.path.join(package_root, "debian/git-build-recipe.manifest"), + FileContains( + "# git-build-recipe format 0.1 deb-version 1\n" + "source git-commit:%s\n" % commit)) + self.assertThat( + os.path.join(package_root, "debian/changelog"), + FileContains(matcher=StartsWith( + "foo (1) unstable; urgency=low\n"))) + + @skipIfFakeroot() + def test_cmd_dailydeb_no_work_dir(self): + #TODO: define a test feature for debuild and require it here. + source = GitRepository("source") + source.build_tree(["a", "debian/"]) + source.build_tree_contents([ + ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), + ("debian/control", + "Source: foo\nMaintainer: maint maint@maint.org\n\n" + "Package: foo\nArchitecture: all\n")]) + source.add(["a", "debian/rules", "debian/control"]) + source.commit("one") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe("--manifest manifest test.recipe") + + @skipIfFakeroot() + def test_cmd_dailydeb_if_changed_from_non_existent(self): + #TODO: define a test feature for debuild and require it here. + source = GitRepository("source") + source.build_tree(["a", "debian/"]) + source.build_tree_contents([ + ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), + ("debian/control", + "Source: foo\nMaintainer: maint maint@maint.org\n" + "\nPackage: foo\nArchitecture: all\n")]) + source.add(["a", "debian/rules", "debian/control"]) + source.commit("one") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--manifest manifest --if-changed-from bar test.recipe") + + def make_upstream_version(self, source, package_name, version, contents, + pristine_tar_format=None): + source._git_call("checkout", "-q", "--orphan", "upstream") + source._git_call("rm", "-qrf", ".") + source.build_tree_contents(contents) + source.add(["."]) + commit = source.commit("import upstream %s" % version) + cmd = [] + if pristine_tar_format in (None, "gz"): + tarfile_path = "%s_%s.orig.tar.gz" % (package_name, version) + elif pristine_tar_format == "bz2": + tarfile_path = "%s_%s.orig.tar.bz2" % (package_name, version) + cmd.extend(["-c", "tar.tar.bz2.command=bzip2 -c"]) + else: + raise AssertionError( + "unknown pristine tar format %s" % pristine_tar_format) + cmd.extend([ + "archive", + "--prefix=upstream/", + "-o", os.path.abspath(tarfile_path), + "upstream", + ]) + source._git_call(*cmd) + subprocess.check_call( + ["pristine-tar", "commit", os.path.abspath(tarfile_path)], + stderr=subprocess.DEVNULL, cwd=source.path) + tarfile_sha1 = sha1_file_by_name(tarfile_path) + source.tag("upstream/%s" % version, commit) + source._git_call("checkout", "-q", "master") + return tarfile_sha1 + + def make_simple_package(self, path): + source = GitRepository(path) + source.build_tree(["a", "debian/"]) + cl_contents = ("package (0.1-1) unstable; urgency=low\n * foo\n" + " -- maint Tue, 04 Aug 2009 " + "10:03:10 +0100\n") + source.build_tree_contents([ + ("debian/rules", "#!/usr/bin/make -f\nclean:\n"), + ("debian/control", + "Source: package\nMaintainer: maint maint@maint.org\n\n" + "Package: package\nArchitecture: all\n"), + ("debian/changelog", cl_contents) + ]) + source.add(["a", "debian/rules", "debian/control", "debian/changelog"]) + source.commit("one") + return source + + def test_cmd_dailydeb_no_build(self): + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--manifest manifest --no-build test.recipe working") + new_cl_contents = ( + "package (1) unstable; urgency=low\n\n" + " * Auto build.\n\n -- M. Maintainer ") + self.assertThat( + "working/test/debian/changelog", + FileContains(matcher=StartsWith(new_cl_contents))) + for fn in os.listdir("working"): + self.assertThat(fn, Not(EndsWith(".changes"))) + + @skipIfFakeroot() + def test_cmd_dailydeb_with_package_from_changelog(self): + #TODO: define a test feature for debuild and require it here. + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--manifest manifest --if-changed-from bar test.recipe working") + new_cl_contents = ( + "package (1) unstable; urgency=low\n\n" + " * Auto build.\n\n -- M. Maintainer ") + self.assertThat( + "working/test/debian/changelog", + FileContains(matcher=StartsWith(new_cl_contents))) + + @skipIfFakeroot() + def test_cmd_dailydeb_with_version_from_changelog(self): + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version {debversion}-2\n" + "source\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + cl = changelog.Changelog(self._get_file_contents( + "working/test/debian/changelog")) + self.assertEqual("0.1-1-2", str(cl._blocks[0].version)) + + @skipIfFakeroot() + def test_cmd_dailydeb_with_version_from_other_branch_changelog(self): + self.make_simple_package("source") + other = self.make_simple_package("other") + cl_contents = ("package (0.4-1) unstable; urgency=low\n * foo\n" + " -- maint Tue, 04 Aug 2009 " + "10:03:10 +0100\n") + other.build_tree_contents([("debian/changelog", cl_contents)]) + other.add(["debian/changelog"]) + other.commit("new changelog entry") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.4 " + "deb-version {debversion:other}.2\n" + "source\n" + "nest other other other\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + cl = changelog.Changelog(self._get_file_contents( + "working/test/debian/changelog")) + self.assertEqual("0.4-1.2", str(cl._blocks[0].version)) + + @skipIfFakeroot() + def test_cmd_dailydeb_with_upstream_version_from_changelog(self): + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version {debupstream}-2\n" + "source\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + new_cl_contents = ( + "package (0.1-2) unstable; urgency=low\n\n" + " * Auto build.\n\n -- M. Maintainer ") + self.assertThat( + "working/test/debian/changelog", + FileContains(matcher=StartsWith(new_cl_contents))) + + @skipIfFakeroot() + def test_cmd_dailydeb_with_append_version(self): + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--append-version ~ppa1 test.recipe working") + new_cl_contents = ( + "package (1~ppa1) unstable; urgency=low\n\n" + " * Auto build.\n\n -- M. Maintainer ") + self.assertThat( + "working/test/debian/changelog", + FileContains(matcher=StartsWith(new_cl_contents))) + + def test_cmd_dailydeb_with_nonascii_maintainer_in_changelog(self): + self.useFixture(EnvironmentVariable( + "DEBFULLNAME", u"Micha\u25c8 Sawicz")) + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version 1\nsource\n") + out, err = self.run_recipe("test.recipe working") + new_cl_contents = ( + "package (1) unstable; urgency=low\n\n" + " * Auto build.\n\n" + u" -- Micha\u25c8 Sawicz ") + self.assertThat( + "working/test/debian/changelog", + FileContains(matcher=StartsWith(new_cl_contents))) + + def test_cmd_dailydeb_with_invalid_version(self): + source = GitRepository("source") + source.build_tree(["a"]) + source.build_tree_contents([ + ("debian/", None), + ("debian/control", + "Source: foo\nMaintainer: maint maint@maint.org\n") + ]) + source.add(["a", "debian/control"]) + source.commit("one") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.1 deb-version $\nsource\n") + err = self.run_recipe("test.recipe working", retcode=1)[1] + self.assertThat(err, MatchesRegex( + r".*Invalid deb-version: \$: " + r"(Could not parse version: \$|Invalid version string '\$')\n", + flags=re.S)) + + def test_cmd_dailydeb_with_safe(self): + self.make_simple_package("source") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1\n" + "source\nrun something bad") + out, err = self.run_recipe("--safe test.recipe working", retcode=1) + self.assertIn("The 'run' instruction is forbidden.\n", err) + + def make_simple_quilt_package(self): + source = self.make_simple_package("source") + source.build_tree(["debian/source/"]) + source.build_tree_contents([ + ("debian/source/format", "3.0 (quilt)\n"), + ("debian/source/options", 'compression = "gzip"\n')]) + source.add(["debian/source/format", "debian/source/options"]) + source.commit("set source format") + return source + + def test_cmd_dailydeb_missing_orig_tarball(self): + self.make_simple_quilt_package() + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1-1\nsource\n") + out, err = self.run_recipe("test.recipe working", retcode=2) + self.assertIn( + 'error: Unable to find the upstream source. ' + 'Import it as tag upstream/1 or build with ' + '--allow-fallback-to-native.\n', err) + + @skipIfFakeroot() + def test_cmd_dailydeb_with_orig_tarball(self): + source = self.make_simple_package("source") + self.make_upstream_version( + source, "package", "0.1", [("file", "content\n")]) + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") + out, err = self.run_recipe("test.recipe working") + self.assertThat("working/package_0.1.orig.tar.gz", PathExists()) + self.assertThat("working/package_0.1-1.diff.gz", PathExists()) + + @skipIfFakeroot() + @requirePristineTar() + def test_cmd_dailydeb_with_pristine_orig_gz_tarball(self): + source = self.make_simple_package("source") + pristine_tar_sha1 = self.make_upstream_version( + source, "package", "0.1", [("file", "content\n")], + pristine_tar_format="gz") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") + out, err = self.run_recipe("test.recipe working") + self.assertThat("working/package_0.1.orig.tar.gz", PathExists()) + self.assertThat("working/package_0.1-1.diff.gz", PathExists()) + self.assertEqual( + sha1_file_by_name("working/package_0.1.orig.tar.gz"), + pristine_tar_sha1) + + @skipIfFakeroot() + @requirePristineTar() + def test_cmd_dailydeb_with_pristine_orig_bz2_tarball(self): + source = self.make_simple_quilt_package() + pristine_tar_sha1 = self.make_upstream_version( + source, "package", "0.1", + [("file", "content\n"), ("a", "contents of a\n")], + pristine_tar_format="bz2") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") + source.build_tree([("upstream/")]) + source.build_tree_contents([ + ("upstream/file", "content\n"), + ("upstream/a", "contents of a\n")]) + source.add(["upstream/file", "upstream/a"]) + out, err = self.run_recipe("test.recipe working") + self.assertThat("working/package_0.1.orig.tar.bz2", PathExists()) + self.assertThat("working/package_0.1-1.debian.tar.gz", PathExists()) + self.assertEqual( + sha1_file_by_name("working/package_0.1.orig.tar.bz2"), + pristine_tar_sha1) + + @requirePristineTar() + def test_cmd_dailydeb_allow_fallback_to_native_with_orig_tarball(self): + source = self.make_simple_quilt_package() + pristine_tar_sha1 = self.make_upstream_version( + source, "package", "0.1", + [("file", "content\n"), ("a", "contents of a\n")], + pristine_tar_format="bz2") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 0.1-1\nsource\n") + source.build_tree([("upstream/")]) + source.build_tree_contents([ + ("upstream/file", "content\n"), + ("upstream/a", "contents of a\n")]) + source.add(["upstream/file", "upstream/a"]) + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + self.assertThat("working/package_0.1.orig.tar.bz2", PathExists()) + self.assertThat("working/package_0.1-1.debian.tar.gz", PathExists()) + self.assertThat( + "source/debian/source/format", FileContains("3.0 (quilt)\n")) + self.assertEqual( + sha1_file_by_name("working/package_0.1.orig.tar.bz2"), + pristine_tar_sha1) + + @skipIfFakeroot() + def test_cmd_dailydeb_force_native(self): + self.make_simple_quilt_package() + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + self.assertThat( + "working/test/debian/source/format", + FileContains("3.0 (native)\n")) + + @skipIfFakeroot() + def test_cmd_dailydeb_force_native_empty_series(self): + source = self.make_simple_quilt_package() + source.build_tree(['debian/patches/']) + source.build_tree_contents([("debian/patches/series", "\n")]) + source.add(["debian/patches/series"]) + source.commit("add patches") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + self.assertThat( + "working/test/debian/source/format", + FileContains("3.0 (native)\n")) + self.assertThat("working/test/debian/patches", Not(PathExists())) + + @skipIfFakeroot() + def test_cmd_dailydeb_force_native_apply_quilt(self): + source = self.make_simple_quilt_package() + source.build_tree(["debian/patches/"]) + patch = dedent("""\ + diff -ur a/thefile b/thefile + --- a/thefile 2010-12-05 20:14:22.000000000 +0100 + +++ b/thefile 2010-12-05 20:14:26.000000000 +0100 + @@ -1 +1 @@ + -old-contents + +new-contents + """) + source.build_tree_contents([ + ("thefile", "old-contents\n"), + ("debian/patches/series", "01_foo.patch"), + ("debian/patches/01_foo.patch", patch)]) + source.add([ + "thefile", "debian/patches/series", + "debian/patches/01_foo.patch"]) + source.commit("add patch") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1\nsource\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working") + self.assertThat( + "working/test/debian/source/format", + FileContains("3.0 (native)\n")) + self.assertThat("working/test/thefile", FileContains("new-contents\n")) + self.assertThat("working/test/debian/patches", Not(PathExists())) + + def test_cmd_dailydeb_force_native_apply_quilt_failure(self): + source = self.make_simple_quilt_package() + source.build_tree(["debian/patches/"]) + patch = dedent("""\ + diff -ur a/thefile b/thefile + --- a/thefile 2010-12-05 20:14:22.000000000 +0100 + +++ b/thefile 2010-12-05 20:14:26.000000000 +0100 + @@ -1 +1 @@ + -old-contents + +new-contents + """) + source.build_tree_contents([ + ("thefile", "contents\n"), + ("debian/patches/series", "01_foo.patch"), + ("debian/patches/01_foo.patch", patch)]) + source.add([ + "thefile", "debian/patches/series", + "debian/patches/01_foo.patch"]) + source.commit("add patch") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1-1\nsource\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working", retcode=1) + self.assertIn("Failed to apply quilt patches\n", err) + + def test_unknown_source_format(self): + source = self.make_simple_package("source") + source.build_tree(["debian/source/"]) + source.build_tree_contents([("debian/source/format", "2.0\n")]) + source.add(["debian/source/format"]) + source.commit("set source format") + with open("test.recipe", "w") as recipe: + recipe.write( + "# git-build-recipe format 0.3 deb-version 1-1\nsource\n") + out, err = self.run_recipe( + "--allow-fallback-to-native test.recipe working", retcode=1) + self.assertIn("Unknown source format 2.0\n", err) diff --git a/gitbuildrecipe/tests/test_recipe.py b/gitbuildrecipe/tests/test_recipe.py index 7628a45..bfc6452 100644 --- a/gitbuildrecipe/tests/test_recipe.py +++ b/gitbuildrecipe/tests/test_recipe.py @@ -1,15 +1,15 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . import os diff --git a/setup.py b/setup.py index a9e781a..930bde2 100755 --- a/setup.py +++ b/setup.py @@ -2,16 +2,16 @@ # Copyright 2015 Canonical Ltd. # -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 3, as published +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranties of -# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranties of +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along +# You should have received a copy of the GNU General Public License along # with this program. If not, see . from setuptools import setup diff --git a/tox.ini b/tox.ini index f990d65..c36a664 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,6 @@ [tox] envlist = + lint py36 py37 py38 @@ -14,3 +15,12 @@ deps = pytest commands = pytest {posargs} + +[testenv:lint] +basepython = + python3.10 +deps = + pre-commit +skip_install = true +commands = + pre-commit run -a -- cgit v1.2.3