test_build.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Test a build of the Jupyter Book"""
  2. import os
  3. import os.path as op
  4. import sys
  5. import subprocess
  6. import shutil as sh
  7. import pytest
  8. this_folder = op.dirname(op.abspath(__file__))
  9. sys.path.append(op.join(this_folder, '..'))
  10. from jupyterbook.utils import _split_yaml
  11. def is_in(lines, check):
  12. is_in = False
  13. for line in lines:
  14. if check in line:
  15. is_in = True
  16. return is_in
  17. def is_not_in(lines, check):
  18. is_in = True
  19. for line in lines:
  20. if check in line:
  21. is_in = False
  22. return is_in
  23. def replace_in_file(from_text, to_text, filename):
  24. with open(filename, "r") as sources:
  25. lines = sources.readlines()
  26. with open(filename, "w") as sources:
  27. for line in lines:
  28. sources.write(line.replace(from_text, to_text))
  29. ####################################################
  30. # Delete old build and create a new one
  31. curdir = op.dirname(op.abspath(__file__))
  32. if op.isdir(op.join(curdir, 'site', '_build')):
  33. sh.rmtree(op.join(curdir, 'site', '_build'))
  34. print("Building site for test suite...")
  35. cmd = ["python", op.join(curdir, "..", "generate_book.py"),
  36. "--site-root", op.join(curdir, "site"), "--path-template", op.join(curdir, "..", "templates", "jekyllmd.tpl")]
  37. out = subprocess.check_call(cmd)
  38. ####################################################
  39. # Check outputs
  40. def test_notebook():
  41. with open(op.join(curdir, 'site', '_build', 'tests', 'notebooks.md'), 'r') as ff:
  42. lines = ff.readlines()
  43. # Escaping characters get doubled
  44. assert is_in(lines, "\\\\$Escape \\\\$your \\\\$dollar signs!")
  45. # Notebook-converted images work
  46. assert is_in(lines, "../images/tests/notebooks_2_0.png")
  47. # Jekyll markdown classes are there
  48. assert is_in(lines, "{:.input_area}")
  49. # Cell hiding etc works
  50. assert is_not_in(lines, 'thisvariable = "none of this should show up in the textbook"')
  51. assert is_not_in(lines, '"this plot *will* show up in the textbook."')
  52. # Static files are copied over
  53. assert op.exists(op.join(curdir, 'site', '_build', 'tests', 'cool.jpg'))
  54. def test_split_yaml():
  55. with open(op.join(curdir, 'site', '_build', 'tests', 'features.md'), 'r') as ff:
  56. lines = ff.readlines()
  57. # Make sure the yaml remains in the file
  58. assert is_in(lines, "yaml_frontmatter: true")
  59. # Edgecases etc on the splitter function
  60. assert _split_yaml([]) == ([], [])
  61. assert _split_yaml(['foo\n', 'bar\n']) == ([], ['foo\n', 'bar\n'])
  62. assert _split_yaml(['---\n', 'foo\n', 'bar\n']) == ([], ['---\n', 'foo\n', 'bar\n'])
  63. exp = ['---\n', 'foo\n', '---\n']
  64. assert _split_yaml(exp) == (['foo\n'], [])
  65. assert (_split_yaml(['---\n', 'foo\n', '---\n', 'baz\n', 'barf\n']) ==
  66. (['foo\n'], ['baz\n', 'barf\n']))
  67. assert (_split_yaml(['---\n', 'foo\n', 'bar\n', '---\n', 'baz\n', 'barf\n']) ==
  68. (['foo\n', 'bar\n'], ['baz\n', 'barf\n']))
  69. assert (_split_yaml(['\n', '\n', '---\n', 'foo\n', '---\n', 'baz\n', 'barf\n']) ==
  70. (['foo\n'], ['baz\n', 'barf\n']))
  71. assert (_split_yaml([' \n', ' \n', '---\n', 'foo\n', '---\n', 'baz\n', 'barf\n']) ==
  72. (['foo\n'], ['baz\n', 'barf\n']))
  73. def test_notebook_update():
  74. source_file = op.join(curdir, 'site', 'content', 'tests', 'features.md')
  75. target_file = op.join(curdir, 'site', '_build', 'tests', 'features.md')
  76. source_text = 'https://'
  77. target_text = 'www.'
  78. # replace source_text with target_text in source_file
  79. assert is_not_in(open(target_file).readlines(), target_text)
  80. replace_in_file(source_text, target_text, source_file)
  81. out = subprocess.check_call(cmd)
  82. assert is_in(open(target_file).readlines(), target_text)
  83. replace_in_file(target_text, source_text, source_file)
  84. out = subprocess.check_call(cmd)
  85. assert is_not_in(open(target_file).readlines(), target_text)