javaclass

Annotated test.py

146:1dc70ce13680
2005-01-23 Paul Boddie Added more tests.
paul@144 1
#!/usr/bin/env python
paul@144 2
paul@144 3
"Run the test suite."
paul@144 4
paul@144 5
import os, glob
paul@144 6
paul@144 7
def get_test_sources():
paul@144 8
    return glob.glob("*.java")
paul@144 9
paul@144 10
def get_test_classes():
paul@144 11
    # Use the original source files to decide which classes have main methods.
paul@144 12
    return [os.path.splitext(f)[0] for f in get_test_sources()]
paul@144 13
paul@144 14
def compile_test_files(javac):
paul@144 15
    for java_file in get_test_sources():
paul@144 16
        class_file = os.path.splitext(java_file)[0] + ".class"
paul@144 17
        if not os.path.exists(class_file) or os.path.getmtime(class_file) < os.path.getmtime(java_file):
paul@144 18
            print "Compiling", java_file
paul@144 19
            os.system(javac + " " + java_file)
paul@144 20
paul@144 21
def run_test_files(java):
paul@144 22
    for class_ in get_test_classes():
paul@144 23
        print "Running", class_
paul@144 24
        os.system(java + " " + class_)
paul@144 25
paul@144 26
if __name__ == "__main__":
paul@144 27
    import sys
paul@144 28
paul@144 29
    # Find the compiler.
paul@144 30
paul@144 31
    java_home = os.environ.get("JAVA_HOME")
paul@144 32
    if java_home:
paul@144 33
        javac = os.path.join(java_home, "bin", "javac")
paul@144 34
    elif len(sys.argv) > 1:
paul@144 35
        javac = sys.argv[1]
paul@144 36
    else:
paul@144 37
        print "Cannot find a Java compiler."
paul@144 38
        print "Please specify the full path as an argument to this program"
paul@144 39
        print "or set JAVA_HOME to the JDK installation."
paul@144 40
        sys.exit(1)
paul@144 41
paul@144 42
    if not os.path.exists(javac):
paul@144 43
        print "The suggested Java compiler cannot be found."
paul@144 44
        sys.exit(1)
paul@144 45
paul@144 46
    # Compile the programs.
paul@144 47
paul@144 48
    os.chdir("tests")
paul@144 49
    compile_test_files(javac)
paul@144 50
    run_test_files("runclass.py")
paul@144 51
paul@144 52
# vim: tabstop=4 expandtab shiftwidth=4