|
| 1 | +#!/usr/bin/env python |
| 2 | +#------------------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (C) Microsoft. All rights reserved. |
| 4 | +# Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 5 | +#------------------------------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import subprocess as SP |
| 10 | + |
| 11 | +test_all = True |
| 12 | +test_root = os.path.dirname(os.path.realpath(__file__)) |
| 13 | + |
| 14 | +# ugly trick |
| 15 | +ch_path = os.path.join(os.path.dirname(test_root), "BuildLinux/ch") |
| 16 | + |
| 17 | +if not os.path.isfile(ch_path): |
| 18 | + print "BuildLinux/ch not found. Did you run ./build.sh already?" |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | +if len(sys.argv) > 1: |
| 22 | + if sys.argv[1] in ['-?', '--help']: |
| 23 | + print "ChakraCore *nix Test Script\n" |
| 24 | + |
| 25 | + print "Usage:" |
| 26 | + print "test.py <optional test path>\n" |
| 27 | + |
| 28 | + print "-?, --help : Show help\n" |
| 29 | + |
| 30 | + print "Samples:" |
| 31 | + print "test only Array:" |
| 32 | + print "\t./test.py Array\n" |
| 33 | + |
| 34 | + print "test a single file:" |
| 35 | + print "\t./test.py Basics/hello.js\n" |
| 36 | + |
| 37 | + print "test all folders:" |
| 38 | + print "\t./test.py" |
| 39 | + sys.exit(0) |
| 40 | + test_all = None |
| 41 | + |
| 42 | +test_dirs=[''] |
| 43 | +if test_all: |
| 44 | + test_dirs = os.listdir(test_root) |
| 45 | +else: |
| 46 | + test_dirs[0] = sys.argv[1] |
| 47 | + |
| 48 | +def show_failed(filename, output, exit_code, expected_output): |
| 49 | + print "\nFailed ->", filename |
| 50 | + if expected_output == None: |
| 51 | + print "\nOutput:" |
| 52 | + print "----------------------------" |
| 53 | + print output |
| 54 | + print "----------------------------" |
| 55 | + else: |
| 56 | + lst_output = output.split('\n') |
| 57 | + lst_expected = expected_output.split('\n') |
| 58 | + ln = min(len(lst_output), len(lst_expected)) |
| 59 | + for i in range(0, ln): |
| 60 | + if lst_output[i] != lst_expected[i]: |
| 61 | + print "Output: (at line " + str(i) + ")" |
| 62 | + print "----------------------------" |
| 63 | + print lst_output[i] |
| 64 | + print "----------------------------" |
| 65 | + print "Expected Output:" |
| 66 | + print "----------------------------" |
| 67 | + print lst_expected[i] |
| 68 | + print "----------------------------" |
| 69 | + break |
| 70 | + |
| 71 | + print "exit code:", exit_code |
| 72 | + print "\nFailed!" |
| 73 | + sys.exit(exit_code) |
| 74 | + |
| 75 | +def test_path(folder, is_file): |
| 76 | + files=[''] |
| 77 | + if is_file == False: |
| 78 | + print "Testing ->", os.path.basename(folder) |
| 79 | + files = os.listdir(folder) |
| 80 | + else: |
| 81 | + files[0] = folder |
| 82 | + |
| 83 | + for js_file in files: |
| 84 | + if is_file or os.path.splitext(js_file)[1] == '.js': |
| 85 | + js_file = os.path.join(folder, js_file) |
| 86 | + js_output = "" |
| 87 | + |
| 88 | + if not os.path.isfile(js_file): |
| 89 | + print "Javascript file doesn't exist (" + js_file + ")" |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + p = SP.Popen([ch_path, js_file], stdout=SP.PIPE, stderr=SP.STDOUT, close_fds=True) |
| 93 | + js_output = p.communicate()[0].replace('\r','') |
| 94 | + exit_code = p.wait() |
| 95 | + |
| 96 | + if exit_code != 0: |
| 97 | + show_failed(js_file, js_output, exit_code, None) |
| 98 | + else: #compare outputs |
| 99 | + baseline = os.path.splitext(js_file)[0] + '.baseline' |
| 100 | + baseline = os.path.join(folder, baseline) |
| 101 | + if os.path.isfile(baseline): |
| 102 | + expected_output = None |
| 103 | + with open(baseline, 'r') as bs_file: |
| 104 | + expected_output = bs_file.read().replace('\r', '') |
| 105 | + # todo: compare line by line and use/implement wild cards support |
| 106 | + # todo: by default we discard line endings (xplat), make this optional |
| 107 | + if expected_output.replace('\n', '') != js_output.replace('\n', ''): |
| 108 | + show_failed(js_file, js_output, exit_code, expected_output) |
| 109 | + |
| 110 | + if not is_file: |
| 111 | + print "\tPassed ->", os.path.basename(js_file) |
| 112 | + |
| 113 | +is_file = len(test_dirs) == 1 and os.path.splitext(test_dirs[0])[1] == '.js' |
| 114 | + |
| 115 | +for folder in test_dirs: |
| 116 | + full_path = os.path.join(test_root, folder) |
| 117 | + if os.path.isdir(full_path) or is_file: |
| 118 | + test_path(full_path, is_file) |
| 119 | + |
| 120 | +print 'Success!' |
0 commit comments