View
@@ -0,0 +1,65 @@
#!/bin/bash
#
# Usage:
# ./time-test.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
source test/common.sh
time-tool() {
$(dirname $0)/time.py "$@"
}
test-tsv() {
local out=_tmp/time.tsv
rm -f $out
for i in 1 2 3; do
time-tool --tsv -o $out -- sleep 0.0${i}
done
cat $out
}
test-cannot-serialize() {
local out=_tmp/time2.tsv
rm -f $out
set +o errexit
# Tab should fail
time-tool --tsv -o $out --field $'\n' -- sleep 0.001; status=$?
test $status = 1 || fail "Unexpected status $status"
# Newline should fail
time-tool --tsv -o $out --field $'\t' -- sleep 0.001; status=$?
test $status = 1 || fail "Unexpected status $status"
# Quote should fail
time-tool --tsv -o $out --field '"' -- sleep 0.001; status=$?
test $status = 1 || fail "Unexpected status $status"
# Backslash is OK
time-tool --tsv -o $out --field '\' -- sleep 0.001; status=$?
test $status = 0 || fail "Unexpected status $status"
# Space is OK, although canonical form would be " "
time-tool --tsv -o $out --field ' ' -- sleep 0.001; status=$?
test $status = 0 || fail "Unexpected status $status"
set -o errexit
cat $out
}
all-passing() {
test-tsv
test-cannot-serialize
echo
echo "All tests in $0 passed."
}
"$@"
View
@@ -27,6 +27,9 @@
def Options():
"""Returns an option parser instance."""
p = optparse.OptionParser('time.py [options] ARGV...')
p.add_option(
'--tsv', dest='tsv', default=False, action='store_true',
help='Write output in TSV format')
p.add_option(
'-o', '--output', dest='output', default=None,
help='Name of output file to write to')
@@ -45,7 +48,12 @@ def main(argv):
fields = tuple(opts.fields)
with open(opts.output, 'a') as f:
out = csv.writer(f)
if opts.tsv:
# TSV output.
out = csv.writer(f, delimiter='\t', doublequote=False,
quoting=csv.QUOTE_NONE)
else:
out = csv.writer(f)
row = (exit_code, '%.4f' % elapsed) + fields
out.writerow(row)
View
@@ -20,6 +20,11 @@ die() {
exit 1
}
fail() {
echo 'TEST FAILURE ' "$@"
exit 1
}
run-task-with-status() {
local out_file=$1
shift
View
@@ -7,15 +7,12 @@ set -o nounset
set -o pipefail
set -o errexit
source test/common.sh
# TODO: We need a common test framework for command-line syntax of bin/*. The
# spec tests are doing that now with $SH.
# osh2oil should be oilc translate.
fail() {
echo 'TEST FAILED'
exit 1
}
# Compare osh code on stdin (fd 0) and expected oil code on fd 3.
assert-deps() {
bin/oilc deps | diff -u /dev/fd/3 - || fail
View
@@ -7,15 +7,12 @@ set -o nounset
set -o pipefail
set -o errexit
source test/common.sh
osh-to-oil() {
bin/osh --fix "$@"
}
fail() {
echo 'TEST FAILED'
exit 1
}
# Compare osh code on stdin (fd 0) and expected oil code on fd 3.
osh0-oil3() {
osh-to-oil "$@" | diff -u /dev/fd/3 - || fail
View
@@ -10,7 +10,7 @@ def main(argv):
first_header = None
for path in argv[1:]:
with open(path) as f:
# Assume
# Assume there's no quoting or escaping.
header = f.readline()
if first_header is None:
sys.stdout.write(header)
View
@@ -262,9 +262,14 @@ def PrintTable(css_id, schema, col_names, rows):
print '</table>'
def ReadCsv(f):
def ReadFile(f, tsv=False):
"""Read the CSV file, returning the column names and rows."""
c = csv.reader(f)
if tsv:
c = csv.reader(f, delimiter='\t', doublequote=False,
quoting=csv.QUOTE_NONE)
else:
c = csv.reader(f)
# The first row of the CSV is assumed to be a header. The rest are data.
col_names = []
@@ -284,6 +289,9 @@ def CreateOptionsParser():
p.add_option(
'--schema', dest='schema', metavar="PATH", type='str',
help='Path to the schema.')
p.add_option(
'--tsv', dest='tsv', default=False, action='store_true',
help='Read input in TSV format')
return p
@@ -302,15 +310,26 @@ def main(argv):
except IOError as e:
raise RuntimeError('Error opening schema: %s' % e)
else:
schema_path = csv_path.replace('.csv', '.schema.csv')
if csv_path.endswith('.csv'):
schema_path = csv_path.replace('.csv', '.schema.csv')
elif csv_path.endswith('.tsv'):
schema_path = csv_path.replace('.tsv', '.schema.tsv')
else:
raise AssertionError(csv_path)
log('schema path %s', schema_path)
try:
schema_f = open(schema_path)
except IOError:
schema_f = None # allowed to have no schema
if schema_f:
r = csv.reader(schema_f)
if opts.tsv:
r = csv.reader(schema_f, delimiter='\t', doublequote=False,
quoting=csv.QUOTE_NONE)
else:
r = csv.reader(schema_f)
schema = Schema(list(r))
else:
schema = NullSchema()
@@ -319,7 +338,7 @@ def main(argv):
log('schema %s', schema)
with open(csv_path) as f:
col_names, rows = ReadCsv(f)
col_names, rows = ReadFile(f, opts.tsv)
schema.VerifyColumnNames(col_names)