diff --git a/404main b/404main index 80a410f..eae6d26 100755 --- a/404main +++ b/404main @@ -104,15 +104,20 @@ def find_main(cache, pack): process_deps(cache, deps) +def usage(exit): + print 'Usage: %s []' % sys.argv[0] + sys.exit(exit) def main(): global packages, distro # Check if the amount of arguments is correct - if len(sys.argv) < 2 or len(sys.argv) > 3 or sys.argv[1] in ('help', '-h', '--help'): - print 'Usage: %s []' % sys.argv[0] - sys.exit(1) + if len(sys.argv) > 1 and sys.argv[1] in ('help', '-h', '--help'): + usage(0) + + if len(sys.argv) < 2 or len(sys.argv) > 3: + usage(1) cache = apt.cache.Cache() diff --git a/debian/changelog b/debian/changelog index f91b1b4..54b9563 100644 --- a/debian/changelog +++ b/debian/changelog @@ -15,6 +15,10 @@ ubuntu-dev-tools (0.109) UNRELEASED; urgency=low DEBEMAIL. (LP: #665202) * Add the beginnings of a test suite. (LP: #690386) - Switch to setuptools, to support setup.py test. + - Test for that every script can run --help and return 0. + - 404main, merge-changelog, pull-debian-debdiff, pull-debian-source, + pull-revu-source: + + Return 0 after showing help. * ubuntutools/common.py: Remove https_proxy unsetting code, working around LP: #94130. @@ -36,7 +40,7 @@ ubuntu-dev-tools (0.109) UNRELEASED; urgency=low * add "add-patch" that provides the non-interactive version of edit-patch - -- Benjamin Drung Wed, 22 Dec 2010 14:22:58 +0100 + -- Benjamin Drung Wed, 22 Dec 2010 19:06:23 +0100 ubuntu-dev-tools (0.108) experimental; urgency=low diff --git a/merge-changelog b/merge-changelog index 29e2d8a..5ec3c97 100755 --- a/merge-changelog +++ b/merge-changelog @@ -20,7 +20,7 @@ import sys, re -def usage(): +def usage(exit=1): print '''Usage: merge-changelog merge-changelog takes two changelogs that once shared a common source, @@ -28,7 +28,7 @@ merges them back together, and prints the merged result to stdout. This is useful if you need to manually merge a ubuntu package with a new Debian release of the package. ''' - sys.exit(1) + sys.exit(exit) ######################################################################## # Changelog Management @@ -253,8 +253,10 @@ def deb_cmp(x, y): if __name__ == '__main__': + if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'): + usage(exit=0) if len(sys.argv) != 3: - usage() + usage(exit=1) left_changelog = sys.argv[1] right_changelog = sys.argv[2] diff --git a/pull-debian-debdiff b/pull-debian-debdiff index bd5389f..e9e3e92 100755 --- a/pull-debian-debdiff +++ b/pull-debian-debdiff @@ -115,6 +115,13 @@ sub download_source return 1; } +sub usage +{ + my ($exit) = @_; + print "Usage: $0 PKG VERSION\n"; + exit $exit; +} + my $pkg = $ARGV[0]; @@ -123,8 +130,10 @@ my $just_fetch = ($ARGV[2] && $ARGV[2] eq "--fetch"); my $skip = $ARGV[2] || 1; $skip+=0; -if (!defined($pkg) || !defined($version)) { - die "Usage: $0 PKG VERSION\n"; +if (defined($pkg) && ($pkg eq '--help' || $pkg eq '-h')) { + usage(0); +} elsif (!defined($pkg) || !defined($version)) { + usage(2); } diff --git a/pull-debian-source b/pull-debian-source index aba22c8..297324f 100755 --- a/pull-debian-source +++ b/pull-debian-source @@ -27,10 +27,10 @@ use AptPkg::Version; die("Please install 'devscripts'\n") if(! grep -x "$_/dget", split(':',$ENV{'PATH'})); my($name)=basename($0); -my($package)=$ARGV[0] || &usage(); +my($package)=$ARGV[0] || &usage(2); my($help)=0; GetOptions('help' => \$help); -&usage() if($help); +&usage(0) if($help); my($release)=$ARGV[1] || 'unstable'; $release=&convertCodeName($release); &checkRelease($release); @@ -132,7 +132,9 @@ sub getDSC { } sub usage { - die("USAGE: $name [-h] [target release]\n"); + my($exit)=shift; + print("USAGE: $name [-h] [target release]\n"); + exit($exit); } sub invalidRelease { my($releases)=shift || die("Invalid Release!"); diff --git a/pull-revu-source b/pull-revu-source index 1c6bbd4..9bce2f7 100755 --- a/pull-revu-source +++ b/pull-revu-source @@ -20,6 +20,7 @@ use warnings; use strict; +use File::Basename; use LWP::Simple; use Getopt::Long; @@ -27,10 +28,10 @@ die("Please install 'devscripts'\n") if(! grep -x "$_/dget", split(':',$ENV{'PAT my $REVU = "revu.ubuntuwire.com"; -my($package) = lc($ARGV[0]) || usage(); +my($package) = lc($ARGV[0]) || usage(2); my($help)=0; GetOptions('help' => \$help); -usage() if($help); +usage(0) if($help); dget(getURL()); @@ -47,6 +48,8 @@ sub dget { } sub usage { + my($exit) = @_; my($name)=basename($0); - die("USAGE: $name [-h] \n"); + print("USAGE: $name [-h] \n"); + exit($exit); } diff --git a/setup.py b/setup.py index 938857e..fe1ae0f 100755 --- a/setup.py +++ b/setup.py @@ -13,57 +13,60 @@ if os.path.exists(changelog): if match: version = match.group(1) -setup(name='ubuntu-dev-tools', - version=version, - scripts=['404main', - 'backportpackage', - 'check-symbols', - 'dch-repeat', - 'dgetlp', - 'edit-patch', - 'errno', - 'get-branches', - 'get-build-deps', - 'grab-attachments', - 'grab-merge', - 'grep-merges', - 'hugdaylist', - 'import-bug-from-debian', - 'lp-list-bugs', - 'lp-project-upload', - 'lp-set-dup', - 'lp-shell', - 'manage-credentials', - 'massfile', - 'merge-changelog', - 'mk-sbuild', - 'pbuilder-dist', - 'pbuilder-dist-simple', - 'pull-debian-debdiff', - 'pull-debian-source', - 'pull-lp-source', - 'pull-revu-source', - 'requestsync', - 'reverse-build-depends', - 'setup-packaging-environment', - 'sponsor-patch', - 'submittodebian', - 'suspicious-source', - 'syncpackage', - 'ubuntu-build', - 'ubuntu-iso', - 'update-maintainer', - 'what-patch', - 'wrap-and-sort', - ], - packages=['ubuntutools', - 'ubuntutools/lp', - 'ubuntutools/requestsync', - 'ubuntutools/sponsor_patch', - 'ubuntutools/test', - ], - data_files=[('share/man/man1', glob.glob("doc/*.1")), - ('share/man/man5', glob.glob("doc/*.5")), - ], - test_suite='ubuntutools.test.discover', -) +scripts = ['404main', + 'backportpackage', + 'check-symbols', + 'dch-repeat', + 'dgetlp', + 'edit-patch', + 'errno', + 'get-branches', + 'get-build-deps', + 'grab-attachments', + 'grab-merge', + 'grep-merges', + 'hugdaylist', + 'import-bug-from-debian', + 'lp-list-bugs', + 'lp-project-upload', + 'lp-set-dup', + 'lp-shell', + 'manage-credentials', + 'massfile', + 'merge-changelog', + 'mk-sbuild', + 'pbuilder-dist', + 'pbuilder-dist-simple', + 'pull-debian-debdiff', + 'pull-debian-source', + 'pull-lp-source', + 'pull-revu-source', + 'requestsync', + 'reverse-build-depends', + 'setup-packaging-environment', + 'sponsor-patch', + 'submittodebian', + 'suspicious-source', + 'syncpackage', + 'ubuntu-build', + 'ubuntu-iso', + 'update-maintainer', + 'what-patch', + 'wrap-and-sort', + ] + +if __name__ == '__main__': + setup(name='ubuntu-dev-tools', + version=version, + scripts=scripts, + packages=['ubuntutools', + 'ubuntutools/lp', + 'ubuntutools/requestsync', + 'ubuntutools/sponsor_patch', + 'ubuntutools/test', + ], + data_files=[('share/man/man1', glob.glob("doc/*.1")), + ('share/man/man5', glob.glob("doc/*.5")), + ], + test_suite='ubuntutools.test.discover', + ) diff --git a/ubuntutools/test/test_help.py b/ubuntutools/test/test_help.py new file mode 100644 index 0000000..4b8e339 --- /dev/null +++ b/ubuntutools/test/test_help.py @@ -0,0 +1,90 @@ +# test_help.py - Ensure scripts can run --help. +# +# Copyright (C) 2010, Stefano Rivera +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +import fcntl +import os +import select +import signal +import subprocess +import time + +import setup +from ubuntutools.test import unittest + +BLACKLIST = { + 'check-symbols': 'No Help', + 'edit-patch': 'No Help', + 'get-build-deps': 'No Help, runs sudo', + 'grep-merges': 'No Help', + 'lp-project-upload': 'Returns non-zero after help. Leaving u-d-t in LP: #524680', + 'massfile': 'No Help. Leaves files in .', + 'mk-sbuild': 'Fires up apt-get before showing help', + 'pbuilder-dist-simple': 'No Help', + 'setup-packaging-environment': 'Throws Error', + 'submittodebian': 'No Help', + 'ubuntu-iso': 'No Help', +} +TIMEOUT = 5 + +def load_tests(loader, tests, pattern): + "Give HelpTestCase a chance to populate before loading its test cases" + suite = unittest.TestSuite() + HelpTestCase.populate() + suite.addTests(loader.loadTestsFromTestCase(HelpTestCase)) + return suite + +class HelpTestCase(unittest.TestCase): + @classmethod + def populate(cls): + for script in setup.scripts: + setattr(cls, 'test_' + script, cls.makeHelpTester(script)) + + @classmethod + def makeHelpTester(cls, script): + def tester(self): + if script in BLACKLIST: + raise unittest.SkipTest("Blacklisted: " + BLACKLIST[script]) + + null = open('/dev/null', 'r') + p = subprocess.Popen(['./' + script, '--help'], + close_fds=True, stdin=null, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + started = time.time() + out = [] + + fds = [p.stdout.fileno(), p.stderr.fileno()] + for fd in fds: + fcntl.fcntl(fd, fcntl.F_SETFL, + fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) + + while time.time() - started < TIMEOUT: + for fd in select.select(fds, [], fds, TIMEOUT)[0]: + out.append(os.read(fd, 1024)) + if p.poll() is not None: + break + + if p.poll() is None: + os.kill(p.pid, signal.SIGTERM) + time.sleep(1) + if p.poll() is None: + os.kill(p.pid, signal.SIGKILL) + null.close() + + self.assertEqual(p.poll(), 0, + "%s failed to return usage within %i seconds.\n" + "Output:\n%s" + % (script, TIMEOUT, ''.join(out))) + return tester