Previous 199869 Revisions Next

r18903 Thursday 8th November, 2012 at 08:14:32 UTC by Miodrag Milanović
Converted jedtest script to Python [Andrew Gardner]
[src/regtests/jedutil]jedtest.py*

trunk/src/regtests/jedutil/jedtest.py
r0r18903
1import os
2import sys
3import shutil
4import filecmp
5import subprocess
6
7VERBOSE = False
8
9
10class JedTest:
11    """
12    A class containing the information necessary to run a test.
13    """
14    def __init__(self):
15        self.name = ""
16        self.jedFile = ""
17        self.baselineFile = ""
18        self.outputFile = ""
19   
20    def __repr__(self):
21        return "Name: %s\nJedFile: %s\nBaselineFile: %s\nOutputFile: %s" % (self.name,
22                                                                            self.jedFile,
23                                                                            self.baselineFile,
24                                                                            self.outputFile)
25
26
27def findJedTests(jedPath, baselinePath, outputPath):
28    """
29    Given a path to some jed files, returns a list of JedTest objects.
30    """
31    jedTestList = list()
32    for (path, dirs, files) in os.walk(jedPath):
33        if '.svn' in path:
34            continue
35
36        for filename in files:
37            jedPathFull = os.path.join(path, filename)
38            if os.path.splitext(jedPathFull)[1] != '.jed':
39                continue
40            palName = os.path.splitext(os.path.basename(filename))[0]
41            subpathName = path[len(jedPath):].strip(os.sep)
42
43            test = JedTest()
44            test.name = palName
45            test.jedFile = jedPathFull
46            test.baselineFile = os.path.join(baselinePath, subpathName, test.name+".txt")
47            test.outputFile = os.path.join(outputPath, subpathName, test.name+".txt")
48            jedTestList.append(test)
49
50    return jedTestList
51
52
53def runViewJedTests(tests, jedUtilApp):
54    """
55    Given a list of JedTests and a command line program, run them & save the
56    results to the files specified in the JedTest objects.
57    """
58    for test in tests:
59        command = [jedUtilApp, "-view", test.jedFile, test.name]
60        if VERBOSE:
61            print "Viewing the JED file: %s" % (test.jedFile)
62            print "Command: %s" % ((" ").join(command))
63
64        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
65        (stdout,stderr) = process.communicate()
66       
67        if stderr:
68            print "Error: JED test named %s failed during viewing." % test.name
69
70        fp = open(test.outputFile, "wb")
71        fp.write(stdout)
72        fp.close()
73   
74
75# MAIN
76def main():
77    # Some path initializations
78    currentDirectory = os.path.dirname(os.path.realpath(__file__))
79    jedsPath = os.path.join(currentDirectory, 'jeds')
80    baselinePath = os.path.join(currentDirectory, "baseline")
81    outputPath = os.path.join(currentDirectory, "output")
82    if os.name == 'nt':
83        jedUtilApp = os.path.normpath(os.path.join(currentDirectory, "..", "..", "..", "jedutil.exe"))
84    else:     
85        jedUtilApp = os.path.normpath(os.path.join(currentDirectory, "..", "..", "..", "jedutil"))
86
87    if (VERBOSE):
88        print "JED Path:      %s" % jedsPath
89        print "Baseline Path: %s" % baselinePath
90        print "Output Path:   %s" % outputPath
91        print "jedutil App:   %s" % jedUtilApp
92        print
93
94
95    # Insure everything exists
96    if (not os.path.exists(currentDirectory) or
97        not os.path.exists(jedsPath) or
98        not os.path.exists(baselinePath) or
99        not os.path.exists(jedUtilApp)):
100        print "One of the above paths does not exist.  Aborting." % jedUtilApp
101        return 3
102
103
104    # Gather the tests
105    tests = findJedTests(jedsPath, baselinePath, outputPath)
106    if not len(tests):
107        print "No tests found!"
108        return 2
109
110
111    # Setup the output paths
112    if (VERBOSE):
113        print "Cleaning the output directory"
114        print
115    if os.path.exists(outputPath):
116        shutil.rmtree(outputPath)
117    os.makedirs(outputPath)
118    for test in tests:
119        testOutputPath = os.path.dirname(test.outputFile)
120        if not os.path.exists(testOutputPath):
121            os.makedirs(testOutputPath)
122
123
124    # Run the commands to create the sample output
125    runViewJedTests(tests, jedUtilApp)
126
127
128    # Insure there are no errors
129    success = True
130    for test in tests:
131        if VERBOSE:
132            print "Diffing the output from viewing the JED file: %s" % test.name
133        if not filecmp.cmp(test.outputFile, test.baselineFile):
134            success = False
135            print "Test %s failed" % test.name
136
137
138    # Report
139    if success:
140        print "All tests ran successfully."
141        return 0
142
143
144    return 1
145
146
147# Main stub - returns error code properly
148if __name__ == "__main__":
149    sys.exit(main())

Previous 199869 Revisions Next


© 1997-2024 The MAME Team