0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Support wildcard matching of output in tests

This commit is contained in:
Kitson Kelly 2018-08-10 10:38:32 -07:00 committed by Ryan Dahl
parent e28d7abc1c
commit c4cafcecb1
4 changed files with 80 additions and 5 deletions

View file

@ -7,6 +7,7 @@
import os
import sys
import subprocess
from util import pattern_match
root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
tests_path = os.path.join(root_path, "tests")
@ -26,15 +27,25 @@ def check_output_test(deno_exe_filename):
with open(out_abs, 'r') as f:
expected_out = f.read()
cmd = [deno_exe_filename, script_abs]
should_succeed = "error" not in script
print " ".join(cmd)
err = False
try:
actual_out = subprocess.check_output(cmd, universal_newlines=True)
except subprocess.CalledProcessError as e:
print "Got non-zero exit code. Output:"
print e.output
err = True
actual_out = e.output
if should_succeed == True:
print "Expected success but got error. Output:"
print actual_out
sys.exit(1)
if should_succeed == False and err == False:
print "Expected an error but succeeded. Output:"
print actual_out
sys.exit(1)
if expected_out != actual_out:
if pattern_match(expected_out, actual_out) != True:
print "Expected output does not match actual."
print "Expected: " + expected_out
print "Actual: " + actual_out

View file

@ -5,6 +5,7 @@ import os
import sys
from check_output_test import check_output_test
from util import executable_suffix, run, build_path
from util_test import util_test
def check_exists(filename):
@ -23,6 +24,9 @@ def main(argv):
print "Usage: tools/test.py [build_dir]"
sys.exit(1)
# Internal tools testing
util_test()
test_cc = os.path.join(build_dir, "test_cc" + executable_suffix)
check_exists(test_cc)
run([test_cc])

View file

@ -1,5 +1,4 @@
# Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
# All rights reserved. MIT License.
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
import os
import shutil
import stat
@ -133,3 +132,33 @@ def build_path():
return os.environ["DENO_BUILD_PATH"]
else:
return os.path.join(root_path, "out", build_mode())
# Returns True if the expected matches the actual output, allowing variation
# from actual where expected has the wildcard (e.g. matches /.*/)
def pattern_match(pattern, string, wildcard="[WILDCARD]"):
if len(pattern) == 0:
return string == 0
if pattern == wildcard:
return True
parts = str.split(pattern, wildcard)
if len(parts) == 1:
return pattern == string
if string.startswith(parts[0]):
string = string[len(parts[0]):]
else:
return False
for i in range(1, len(parts)):
if i == (len(parts) - 1):
if parts[i] == "" or parts[i] == "\n":
return True
found = string.find(parts[i])
if found < 0:
return False
string = string[(found + len(parts[i])):]
return len(string) == 0

31
tools/util_test.py Normal file
View file

@ -0,0 +1,31 @@
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
from util import pattern_match
def pattern_match_test():
print "Testing util.pattern_match()..."
# yapf: disable
fixtures = [("foobarbaz", "foobarbaz", True),
("[WILDCARD]", "foobarbaz", True),
("foobar", "foobarbaz", False),
("foo[WILDCARD]baz", "foobarbaz", True),
("foo[WILDCARD]baz", "foobazbar", False),
("foo[WILDCARD]baz[WILDCARD]qux", "foobarbazqatqux", True),
("foo[WILDCARD]", "foobar", True),
("foo[WILDCARD]baz[WILDCARD]", "foobarbazqat", True)]
# yapf: enable
# Iterate through the fixture lists, testing each one
for (pattern, string, expected) in fixtures:
actual = pattern_match(pattern, string)
assert expected == actual, "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % (
expected, pattern, string)
assert pattern_match("foo[BAR]baz", "foobarbaz",
"[BAR]") == True, "expected wildcard to be set"
assert pattern_match("foo[BAR]baz", "foobazbar",
"[BAR]") == False, "expected wildcard to be set"
def util_test():
pattern_match_test()