summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rwxr-xr-xassert89
-rw-r--r--assert.134
3 files changed, 124 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 6d74fce..29c55cb 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ mandir = $(sharedir)/man
man1dir = $(mandir)/man1
progs = corrupt isascii
-scripts = do-until errno minimify onerror
+scripts = assert do-until errno minimify onerror
CFLAGS = -Wall -O2 --std=gnu99
diff --git a/assert b/assert
new file mode 100755
index 0000000..4b31b5d
--- /dev/null
+++ b/assert
@@ -0,0 +1,89 @@
+#!/usr/bin/python
+
+
+import gnomevfs
+import optparse
+import os
+import subprocess
+import sys
+
+
+def setup_option_parser():
+ parser = optparse.OptionParser()
+
+ parser.add_option("--file", metavar="FILE",
+ help="Report FILE as being failing the assertion.")
+
+ return parser
+
+
+def equal(options, args):
+ return args[0] == args[1], "%s != %s" % (repr(args[0]), repr(args[1]))
+
+
+def mime_type_is(options, args):
+ wanted_type = args[0]
+ args = args[1:]
+ for arg in args:
+ uri = gnomevfs.get_uri_from_local_path(os.path.abspath(arg))
+ actual_type = gnomevfs.get_mime_type(uri)
+ if actual_type != wanted_type:
+ options.file = arg
+ return False, "%s != %s" % (wanted_type, actual_type)
+ return True, None
+
+
+def file_type_is(options, args):
+ wanted_type = args[0]
+ filenames = args[1:]
+ for filename in filenames:
+ p = subprocess.Popen(["file", "-0", filename], stdout=subprocess.PIPE)
+ stdout, stderr = p.communicate()
+ if p.returncode == 0:
+ filename, actual_type = stdout.split("\0")
+ actual_type = actual_type.strip()
+ if actual_type != wanted_type:
+ return False, "%s != %s" % (wanted_type, actual_type)
+ return True, None
+
+
+def main():
+ parser = setup_option_parser()
+ options, args = parser.parse_args()
+
+ opers = {
+ "equal": (2, 2, equal),
+ "file-type-is": (2, None, file_type_is),
+ "mime-type-is": (2, None, mime_type_is),
+ }
+
+ if not args:
+ raise Exception("No operation on command line.")
+ elif args[0] in opers:
+ name = args[0]
+ args = args[1:]
+ min_count, max_count, func = opers[name]
+ if (len(args) < min_count or
+ (max_count is not None and len(args) > max_count)):
+ raise Exception("Operation %s must have %d..%d "
+ "arguments (got %d)." %
+ (name, min_count, max_count or "infinity",
+ len(args)))
+ ok, msg = func(options, args)
+ if not ok:
+ parts = []
+ if options.file:
+ parts.append("%s: " % options.file)
+ parts.append("Assertion failed: ")
+ parts.append(msg or " ".join(sys.argv[1:]))
+ raise Exception("".join(parts))
+ else:
+ raise Exception("Unknown operation '%s'." % args[0])
+
+
+if __name__ == "__main__":
+ try:
+ main()
+ except Exception, e:
+ sys.stderr.write("%s\n" % str(e))
+ sys.exit(1)
diff --git a/assert.1 b/assert.1
new file mode 100644
index 0000000..c555482
--- /dev/null
+++ b/assert.1
@@ -0,0 +1,34 @@
+.\" assert.1 - manual page for the assert command
+.\" Copyright (C) 2009 Lars Wirzenius
+.\"
+.\" This program is free software: you can redistribute it and/or modify
+.\" it under the terms of the GNU General Public License as published by
+.\" the Free Software Foundation, either version 3 of the License, or
+.\" (at your option) any later version.
+.\"
+.\" This program is distributed in the hope that it will be useful,
+.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+.\" GNU General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License
+.\" along with this program. If not, see <http://www.gnu.org/licenses/>.
+.\"
+.TH ASSERT 1
+.SH NAME
+assert \- make sure given conditions are true
+.SH SYNOPSIS
+.B assert
+.RI [ --file =\fIFILE\fR]
+.SH DESCRIPTION
+.B assert
+checks that specific conditions are true.
+At the moment, it can check that a file has a given type (MIME or file(1)).
+.SH EXAMPLE
+The following command checks that the file NEWS has the MIME type text/plain.
+.sp 1
+.ti +4
+.nf
+assert mime-type-is text/plain NEWS
+.fi
+