testbase.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. # Copyright (c) 2009 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by the Free
  7. # Software Foundation; version 2 of the License
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. # for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc., 59
  16. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. SCRIPTS = """
  18. specify -N -n -o output.spec testpkg.yaml 1>output.1.o 2>output.2.o
  19. mv output.spec output.orig.spec
  20. patch -s < input.p
  21. specify -N -n -o output.spec testpkg.yaml 1>ouput.1 2>output.2
  22. if [ ! -f output.no -a ! -f output.spec ]; then
  23. mv output.2 output.error
  24. exit 1
  25. fi
  26. if [ -f output.p ]; then
  27. diff -upN output.orig.spec output.spec > newoutput.p
  28. mv output.spec output.new.spec
  29. patch < output.p >/dev/null
  30. fi
  31. if [ -f output.1p ]; then
  32. diff -upN output.1.o output.1 > newoutput.1p
  33. mv output.1 output.1.n
  34. patch < output.1p >/dev/null
  35. fi
  36. if [ -f output.2p ]; then
  37. diff -upN output.2.o output.2 > newoutput.2p
  38. mv output.2 output.2.n
  39. patch < output.2p >/dev/null
  40. fi
  41. """
  42. import os,sys
  43. import glob
  44. import shutil
  45. import filecmp
  46. BLUE='\033[%dm' % 34
  47. RED='\033[%dm' % 31
  48. RESET = '\033[0m'
  49. def prep_working_env(cases_dir, case, dst_dir):
  50. shutil.copy(os.path.join(cases_dir, 'base', 'testpkg.yaml'), dst_dir)
  51. for out in glob.glob(os.path.join(cases_dir, 'test-'+case, '*')):
  52. try:
  53. if not os.path.isdir(out):
  54. shutil.copy(out, dst_dir)
  55. except:
  56. pass # ignore if file missing
  57. def cleanup(work_dir):
  58. shutil.rmtree(work_dir)
  59. def run_and_check(work_dir):
  60. result = True
  61. cwd = os.getcwd()
  62. os.chdir(work_dir)
  63. os.system(SCRIPTS)
  64. if os.path.exists('output.error'):
  65. # something wrong with tested tools
  66. print >> sys.stderr, file(os.path.join(work_dir, 'output.error')).read()
  67. result = False
  68. os.chdir(cwd)
  69. return result
  70. def compare_outfile(work_dir):
  71. all_equ = True
  72. #print glob.glob(os.path.join(work_dir, '*'))
  73. desc = {'output.p': '*.spec',
  74. 'output.1p': 'STDOUT',
  75. 'output.2p': 'STDERR'}
  76. orig = {'output.p': 'output.orig.spec',
  77. 'output.1p': 'output.1.o',
  78. 'output.2p': 'output.2.o'}
  79. new = {'output.p': 'output.new.spec',
  80. 'output.1p': 'output.1.n',
  81. 'output.2p': 'output.2.n'}
  82. for out in ('output.p', 'output.1p', 'output.2p'):
  83. fp = os.path.join(work_dir, out)
  84. if os.path.exists(fp):
  85. if not filecmp.cmp(os.path.join(work_dir, orig[out]),\
  86. os.path.join(work_dir, new[out])):
  87. all_equ = False
  88. exp_output_diff = file(fp).read().strip()
  89. output_diff = file(os.path.join(work_dir, 'new'+out)).read().strip()
  90. if not output_diff:
  91. output_diff = '<EMPTY>'
  92. if not exp_output_diff:
  93. exp_output_diff = '<EMPTY>'
  94. print "%sExpected changes of %s:" % (BLUE,desc[out])
  95. print '----------------------------------------------------------------------%s' % RESET
  96. print exp_output_diff
  97. print '%s----------------------------------------------------------------------%s' % (BLUE,RESET)
  98. print "%sActual:" % RED
  99. print '----------------------------------------------------------------------%s' % RESET
  100. print output_diff
  101. print '%s----------------------------------------------------------------------%s' % (RED,RESET)
  102. return all_equ