from os import listdir, system
from os.path import isfile, join
import filecmp

mypath = "tests"
srcType = ".txt"
testType = ".m"
outputType = ".out"

onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath,f)) ]
srcFiles = [ f for f in onlyfiles if f.rfind(srcType) > 0 ]
testFiles = [ f for f in onlyfiles if f.rfind(testType) > 0 ]
testCount = 0
failCount = 0
passCount = 0
missingGenCount = 0
missingOutputCount = 0
filterTypes = ['#']

# Delete Test files
for f in testFiles:
    cmd = "rm " + join(mypath,f)
    print cmd
    system(cmd)

# Run Tests
for f in srcFiles:
    # Filter Bad Files
    skip = False
    for ft in filterTypes:
        if f.rfind(ft) > - 1:
            skip = True
    if skip:
        continue
    # Run Test
    print "*****************************"
    print "Test: %s" % (f)
    name = f.rstrip(srcType)
    genFile = name + testType
    execFile = name
    outputFile = name + outputType
    # Compile
    cmd = "./start < " + join(mypath,f)
    system(cmd)
    # Gen Exec
    cmd2 = "gcc -o " + name  + " output.c libcsv.o"
    system(cmd2)
    # Run Exec
    cmd3 = "./" + name + " > " + join(mypath,genFile)
    system(cmd3)
    onlyfiles.append(genFile)
    if (genFile in onlyfiles) == False:
        print "result: Missing Gen file"
        failCount = failCount + 1
        print "******************************"
        missingGenCount = missingGenCount + 1
        continue
    if (outputFile in onlyfiles) == False:
        print "Result: Missing Output file"
        failCount = failCount + 1
        print "******************************"
        missingOutputCount = missingOutputCount + 1
        continue
    genFilePath = join(mypath,genFile)
    outputFilePath = join(mypath,outputFile)
    print genFilePath + " " + outputFilePath
    result = filecmp.cmp( genFilePath, outputFilePath )
    testCount = testCount + 1
    if result == True:
        passCount = passCount + 1
        print "Result: Passed"
    else:
        failCount = failCount + 1
        print "Result: Failed"
    print "*****************************"
print "*****************************"
print "R E S U L T "
print "*****************************"
print "Total %d" % (testCount)
print "Passed %d" % (passCount)
print "Failed %d" % (failCount)
print "Missing Gen File %d" % (missingGenCount)
print "Missing Output File %d" % (missingOutputCount)

        
