/test/includeconf/run.py

http://txt2tags.googlecode.com/ · Python · 153 lines · 107 code · 23 blank · 23 comment · 17 complexity · 1370bffa72d4444e9024135035c8cfc3 MD5 · raw file

  1. #
  2. # txt2tags %!includeconf command tester (http://txt2tags.org)
  3. # See also: ../run.py ../lib.py
  4. #
  5. import os, sys, glob
  6. sys.path.insert(0, '..')
  7. import lib
  8. del sys.path[0]
  9. # sux
  10. lib.OK = lib.FAILED = 0
  11. lib.ERROR_FILES = []
  12. # Tests for the command line option -C
  13. # Note: --config-file is also tested automatically from these tests
  14. tests = [
  15. {
  16. 'name' : 'C',
  17. 'cmdline': ["-C _config.inc"],
  18. 'not-numbered': True,
  19. }, {
  20. 'name' : 'C-C',
  21. 'cmdline': ["-C _config.inc -C _config.inc"],
  22. 'not-numbered': True,
  23. }, {
  24. 'name' : 'C-C2',
  25. 'cmdline': ["-C _config.inc -C _config2.inc"],
  26. }, {
  27. 'name' : 'C-default',
  28. 'cmdline': ["-t html -C _config2.inc"],
  29. }, {
  30. 'name' : 'C-empty',
  31. 'cmdline': ["-t html -C _empty.inc"],
  32. 'not-numbered': True,
  33. }, {
  34. 'name' : 'C-not-found',
  35. 'cmdline': ["-t html -C XXX.inc"],
  36. 'not-numbered': True,
  37. }, {
  38. 'name' : 'C-text',
  39. 'cmdline': ["-t html -C _text.inc"],
  40. 'not-numbered': True,
  41. }, {
  42. 'name' : 'C-targeted-inside',
  43. 'cmdline': ["-t html -C _targeted.inc"],
  44. }, {
  45. 'name' : 'C-nesting',
  46. 'cmdline': ["-C _sub_include.inc"],
  47. }, {
  48. 'name' : 'C-nesting-folder',
  49. 'cmdline': ["-C folder/_folder.inc"],
  50. }, {
  51. 'name' : 'C-nesting-folder-back',
  52. 'cmdline': ["-C folder/subfolder/_folder-back.inc"],
  53. # This checking is never made because the infile may not be known without
  54. # reading the config file. In other words, you can set %!options: -i foo.t2t
  55. # inside the config file and just call: txt2tags -C config.t2t
  56. # Because of this feature, we can't compare config file and infile names
  57. # before reading the full config. But it will not loop, since the first body
  58. # line of the infile will raise a config error.
  59. # }, {
  60. # 'name' : 'C-itself', # t2t -C foo.t2t -i foo.t2t
  61. # 'cmdline': ["-C body-only"],
  62. }
  63. ]
  64. def run():
  65. ### First test the %!includeconf command
  66. errors = [
  67. 'includeconf-itself',
  68. 'includeconf-not-found',
  69. 'includeconf-targeted',
  70. 'includeconf-text',
  71. ]
  72. unnumbered = [
  73. 'includeconf-empty',
  74. ]
  75. # test all t2t files found
  76. for infile in glob.glob("includeconf-*.t2t"):
  77. basename = infile.replace('.t2t', '')
  78. outfile = basename + '.html'
  79. if basename in unnumbered:
  80. okfile = 'ok/not-numbered.html'
  81. else:
  82. okfile = 'ok/numbered.html'
  83. if basename in errors:
  84. outfile = basename + '.out'
  85. okfile = 'ok/' + outfile
  86. cmdline = ['-H', '-i', infile, '-o- >', outfile, '2>&1']
  87. else:
  88. cmdline = ['-H', '-i', infile, '-o', outfile]
  89. if lib.initTest(basename, infile, outfile, okfile):
  90. lib.convert(cmdline)
  91. lib.diff(outfile, okfile)
  92. lib.convert(cmdline, True)
  93. lib.diff(outfile, okfile)
  94. ### Now test -C and --config-file command line options
  95. errors = ['C-not-found', 'C-text']
  96. default_cmdline = ['-H -i body-only.t2t']
  97. infile = 'body-only.t2t'
  98. for test in tests:
  99. # --enum-title is used by this test?
  100. if test.get('not-numbered'):
  101. okfile = 'ok/not-numbered.html'
  102. else:
  103. okfile = 'ok/numbered.html'
  104. # 1st turn (-C), 2nd turn (--config-file)
  105. for i in (1, 2):
  106. if i == 1:
  107. name = test['name']
  108. cmdline = test['cmdline']
  109. else:
  110. name = test['name'].replace('C', 'config-file')
  111. cmdline = map(lambda x: x.replace('-C', '--config-file'), test['cmdline'])
  112. outfile = name + '.html'
  113. if test['name'] in errors:
  114. outfile = name + '.out'
  115. okfile = 'ok/' + outfile
  116. cmdline = default_cmdline + cmdline + ['-o- >', outfile, '2>&1']
  117. else:
  118. cmdline = default_cmdline + cmdline + ['-o', outfile]
  119. # convert and check results
  120. if lib.initTest(name, infile, outfile, okfile):
  121. lib.convert(cmdline)
  122. lib.diff(outfile, okfile)
  123. lib.convert(cmdline, True)
  124. lib.diff(outfile, okfile)
  125. # clean up
  126. if os.path.isfile(lib.CONFIG_FILE):
  127. os.remove(lib.CONFIG_FILE)
  128. return lib.OK, lib.FAILED, lib.ERROR_FILES
  129. if __name__ == '__main__':
  130. print lib.MSG_RUN_ALONE