summaryrefslogtreecommitdiff
path: root/python/edu/learn-python/resources/com/jetbrains/python/edu/user_tester.py
blob: c17e6cdd7479f36a120ed81968ffb8e49688ae84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import imp
import os
import subprocess

USER_TESTS = "userTests"

TEST_FAILED = "FAILED"

TEST_PASSED = "PASSED"

INPUT = "input"
OUTPUT = "output"


def get_index(logical_name, full_name):
    logical_name_len = len(logical_name)
    if full_name[:logical_name_len] == logical_name:
        return int(full_name[logical_name_len])
    return -1


def process_user_tests(file_path):
    user_tests = []
    imp.load_source('user_file', file_path)
    user_tests_dir_path = os.path.abspath(os.path.join(file_path, os.pardir, USER_TESTS))
    user_test_files = os.listdir(user_tests_dir_path)
    for user_file in user_test_files:
        index = get_index(INPUT, user_file)
        if index == -1:
            continue
        output = OUTPUT + str(index)
        if output in user_test_files:
            input_path = os.path.abspath(os.path.join(user_tests_dir_path, user_file))
            output_path = os.path.abspath(os.path.join(user_tests_dir_path, output))
            user_tests.append((input_path, output_path, index))
    return sorted(user_tests, key=(lambda x: x[2]))


def run_user_test(python, executable_path):
    user_tests = process_user_tests(executable_path)
    for test in user_tests:
        input, output, index = test
        test_output = subprocess.check_output([python, executable_path, input])
        expected_output = open(output).read()
        test_status = TEST_PASSED if test_output == expected_output else TEST_FAILED
        print "TEST" + str(index) + " " + test_status
        print "OUTPUT:"
        print test_output + "\n"
        if test_status == TEST_FAILED:
            print "EXPECTED OUTPUT:"
            print expected_output + "\n"


if __name__ == "__main__":
    python = sys.argv[1]
    executable_path = sys.argv[2]
    run_user_test(python , executable_path)