summaryrefslogtreecommitdiffstatshomepage
path: root/src/regtests/jedutil/jedtest.py
blob: de9e0b53de2f0f2a97dc2b3cd6b6054c4d9100c0 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import sys
import shutil
import filecmp
import subprocess

VERBOSE = False


class JedTest:
    """
    A class containing the information necessary to run a test.
    """
    def __init__(self):
        self.name = ""
        self.jedFile = ""
        self.baselineFile = ""
        self.outputFile = ""
    
    def __repr__(self):
        return "Name: %s\nJedFile: %s\nBaselineFile: %s\nOutputFile: %s" % (self.name, 
                                                                            self.jedFile, 
                                                                            self.baselineFile, 
                                                                            self.outputFile)


def findJedTests(jedPath, baselinePath, outputPath):
    """
    Given a path to some jed files, returns a list of JedTest objects.
    """
    jedTestList = list()
    for (path, dirs, files) in os.walk(jedPath):
        if '.svn' in path:
            continue

        for filename in files:
            jedPathFull = os.path.join(path, filename)
            if os.path.splitext(jedPathFull)[1] != '.jed':
                continue
            palName = os.path.splitext(os.path.basename(filename))[0]
            subpathName = path[len(jedPath):].strip(os.sep)

            test = JedTest()
            test.name = palName
            test.jedFile = jedPathFull
            test.baselineFile = os.path.join(baselinePath, subpathName, test.name+".txt")
            test.outputFile = os.path.join(outputPath, subpathName, test.name+".txt")
            jedTestList.append(test)

    return jedTestList


def runViewJedTests(tests, jedUtilApp):
    """
    Given a list of JedTests and a command line program, run them & save the
    results to the files specified in the JedTest objects.
    """
    for test in tests:
        command = [jedUtilApp, "-view", test.jedFile, test.name]
        if VERBOSE:
            print "Viewing the JED file: %s" % (test.jedFile)
            print "Command: %s" % ((" ").join(command))

        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (stdout,stderr) = process.communicate()
        
        if stderr:
            print "Error: JED test named %s failed during viewing." % test.name

        fp = open(test.outputFile, "wb")
        fp.write(stdout)
        fp.close()
    

# MAIN
def main():
    # Some path initializations
    currentDirectory = os.path.dirname(os.path.realpath(__file__))
    jedsPath = os.path.join(currentDirectory, 'jeds')
    baselinePath = os.path.join(currentDirectory, "baseline")
    outputPath = os.path.join(currentDirectory, "output")
    if os.name == 'nt':
        jedUtilApp = os.path.normpath(os.path.join(currentDirectory, "..", "..", "..", "jedutil.exe"))
    else:		
        jedUtilApp = os.path.normpath(os.path.join(currentDirectory, "..", "..", "..", "jedutil"))

    if (VERBOSE):
        print "JED Path:      %s" % jedsPath
        print "Baseline Path: %s" % baselinePath
        print "Output Path:   %s" % outputPath
        print "jedutil App:   %s" % jedUtilApp
        print


    # Insure everything exists
    if (not os.path.exists(currentDirectory) or
        not os.path.exists(jedsPath) or
        not os.path.exists(baselinePath) or
        not os.path.exists(jedUtilApp)):
        print "One of the above paths does not exist.  Aborting." % jedUtilApp
        return 3


    # Gather the tests
    tests = findJedTests(jedsPath, baselinePath, outputPath)
    if not len(tests):
        print "No tests found!"
        return 2


    # Setup the output paths
    if (VERBOSE):
        print "Cleaning the output directory"
        print
    if os.path.exists(outputPath):
        shutil.rmtree(outputPath)
    os.makedirs(outputPath)
    for test in tests:
        testOutputPath = os.path.dirname(test.outputFile)
        if not os.path.exists(testOutputPath):
            os.makedirs(testOutputPath)


    # Run the commands to create the sample output
    runViewJedTests(tests, jedUtilApp)


    # Insure there are no errors
    success = True
    for test in tests:
        if VERBOSE:
            print "Diffing the output from viewing the JED file: %s" % test.name
        if not filecmp.cmp(test.outputFile, test.baselineFile):
            success = False
            print "Test %s failed" % test.name


    # Report
    if success:
        print "All tests ran successfully."
        return 0


    return 1


# Main stub - returns error code properly
if __name__ == "__main__":
    sys.exit(main())