File: pytests.py

package info (click to toggle)
abinit 8.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 285,072 kB
  • sloc: f90: 490,206; python: 44,892; xml: 30,323; perl: 7,298; sh: 5,533; ansic: 5,309; fortran: 557; objc: 323; cpp: 284; makefile: 49; csh: 42; pascal: 31
file content (171 lines) | stat: -rwxr-xr-x 4,716 bytes parent folder | download | duplicates (5)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
from __future__ import print_function, division, absolute_import #, unicode_literals

import os
import sys
import imp

from os.path import join as pj, abspath as absp, exists as pexists, basename, splitext, isfile

#try:
#  import tests as abitests
#except ImportError:
# Add the directory [...]/abinit/tests to $PYTHONPATH
pack_dir, x = os.path.split(absp(__file__))
pack_dir, x = os.path.split(pack_dir)
sys.path.insert(0,pack_dir)
import tests as abitests
del pack_dir, x

def tests_from_pymod(pymod_path, abenv):

  # Import the module
  mod_name = basename(pymod_path).split(".py")[0] 
  module = imp.load_source(mod_name, pymod_path)

  # Inspect the module and build the list of tests.
  tests = list()

  test_gen = getattr(module,"abinit_test_generator", None)
  if test_gen is not None:
    tests.append( PythonTest(abenv, test_gen()) )

  suite_gen = getattr(module, "abinit_suite_generator", None)
  if suite_gen is not None:
    for test_gen in suite_gen(): 
       tests.append( PythonTest(abenv, test_gen) )

  assert tests
  return tests

class PythonTest(object):
  _attrbs = [
    "test_func",
    #"pyscript"
    #"name",
  ]

  def __init__(self, abenv, dictionary):
    self.abenv = abenv

    for k in PythonTest._attrbs:
       self.__dict__[k] = dictionary.get(k, None)

    assert self.test_func is not None
    self.info = self.test_func.__doc__
    assert self.info is not None

    # TODO: Import conditions.
      
  def __str__(self):
    return "\n".join( [ str(k) + " : " + str(v) for k,v in self.__dict__.items()] )

  def run(self, workdir=None):
    if workdir is None: workdir = os.path.abspath(os.curdir) 
    self.workdir = workdir

    print(self.info +"...",)
    #stderr_fname = pj(workdir, self.name + ".stderr")
    #stdout_fname = pj(workdir, self.name + ".stdout")

    #self.stderr = open(stderr_fname, "w")
    #self.stdout = open(stdout_fname, "w")
    from StringIO import StringIO 
    self.stdout, self.stderr = StringIO(), StringIO()

    sys.stdout, sys.stderr = self.stdout, self.stderr

    try:
      exit_status = self.test_func(self.abenv)
    except:
      import traceback
      var = traceback.format_exc()
      #print(self.name + ": " + var)
      print(var)
      exit_status = 99

    self.stdout.seek(0)
    self.stderr.seek(0)

    #self.stderr.close()
    #self.stdin.close()

    # Reinstate stdout and stderr.
    sys.stdout, sys.stderr = sys.__stdout__ , sys.__stderr__ 

    #print("stdout:")
    #print(self.stdout.read())
    #print( "stderr:")
    #print( self.stderr.read())
    #print( "done")

    if exit_status != 0: # Check reference files.
      print(" (Comparing with reference files)")
      #FIXME: I need to change the name of the reference files.
      #print self.stdout.read()
      #print self.stderr.read()

      ref_dir = self.abenv.apath_of("tests/abirules/Refs")
      #for idx, ext in enumerate(["", ".stdout", ".stderr"]):
      #for idx, ext in enumerate([""]):
      for idx, ext in enumerate([]):
        #ref_file = pj(ref_dir, self.name + ext)
        ref_file = pj(ref_dir, "t01.out")
        if not isfile(ref_file): continue
        fh = open(ref_file, "r")
        ref_lines = fh.readlines()
        fh.close()
        if idx == 0: from_lines = self.stdout.readlines()
        if idx == 1: from_lines = self.stderr.readlines()
        import difflib
        diff = difflib.unified_diff(from_lines, ref_lines) #, fromfile, tofile, fromdate, todate, n=n)
        diff = list(diff)
        for l in diff: print(l)
        exit_status = 0
        if len(diff) != 0: exit_status = 1
        
    self.exit_status = exit_status

    if exit_status != 0: 
      print("FAILED")
    else:
      print("SUCCESS")

    return self.exit_status

def run_abirules_suite():
  abenv = abitests.abenv
  import abirules 
  script_dir = abenv.apath_of("abichecks/scripts/")
  scripts = [ absp(pj(script_dir,s)) for s in abirules.pyscripts ]

  exit_status = 0
  for script in scripts:
    tests = tests_from_pymod(script, abenv)
    for test in tests:
      retval = test.run()
      if retval != 0 : exit_status = retval
  return exit_status

def run_buildsys_suite():
  abenv = abitests.abenv

  import buildsys
  script_dir = abenv.apath_of("abichecks/scripts/")
  scripts = [ absp(pj(script_dir,s)) for s in buildsys.pyscripts ]

  exit_status = 0
  for script in scripts:
    tests = tests_from_pymod(script, abenv)
    for test in tests:
      retval = test.run()
      if retval != 0 : exit_status = retval
  return exit_status

if __name__ == "__main__":
 
  #print 30*"=" + " buildsys_tests " + 30*"="
  #run_buildsys_suite()

  print(30*"=" + " abirules_tests " + 30*"=")
  run_abirules_suite()