# Copyright (c) 2015, NVIDIA CORPORATION.  All rights reserved.

import tempfile
import itertools

from . import generate_docs as _
from digits.webapp import app, _doc as doc

def check_doc_file(generator, doc_filename):
    """
    Checks that the output generated by generator matches the contents of doc_filename
    """
    with tempfile.NamedTemporaryFile(suffix='.md') as tmp_file:
        generator.generate(tmp_file.name)
        tmp_file.seek(0)
        with open(doc_filename) as doc_file:
            # memory friendly
            for doc_line, tmp_line in itertools.izip(doc_file, tmp_file):
                doc_line = doc_line.strip()
                tmp_line = tmp_line.strip()
                if doc_line.startswith('*Generated') and \
                        tmp_line.startswith('*Generated'):
                    # If the date is different, that's not a problem
                    pass
                else:
                    assert doc_line == tmp_line, '%s needs to be regenerated. Use scripts/generate_docs.py' % doc_filename

def test_api_md():
    """API.md out-of-date"""
    with app.app_context():
        generator = _.ApiDocGenerator(doc)
        check_doc_file(generator, 'docs/API.md')

def test_flask_routes_md():
    """FlaskRoutes.md out-of-date"""
    with app.app_context():
        generator = _.FlaskRoutesDocGenerator(doc)
        check_doc_file(generator, 'docs/FlaskRoutes.md')



@@ -69,67 +69,404 @@
 ort 
-tempfile%0Aimport itertools%0A%0Afrom . import generate_docs as _
+os.path%0Aimport sys%0Aimport tempfile%0Aimport itertools%0Aimport unittest%0A%0Atry:%0A    import flask.ext.autodoc%0Aexcept ImportError as e:%0A    raise unittest.SkipTest('Flask-Autodoc not installed')%0A%0Atry:%0A    import digits%0Aexcept ImportError:%0A    # Add path for DIGITS package%0A    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))%0A%0Aimport digits.config; digits.config.load_config()
 %0Afro
@@ -504,16 +504,49 @@
 c as doc
+%0Afrom . import generate_docs as _
 %0A%0Adef ch
@@ -1285,17 +1285,38 @@
       el
-s
+if doc_line != tmp_lin
 e:%0A     
@@ -1334,37 +1334,125 @@
     
-assert doc_line == tmp_line, 
+print '(Previous)', doc_line%0A                    print '(New)     ', tmp_line%0A                    raise RuntimeError(
 '%25s 
@@ -1520,16 +1520,17 @@
 filename
+)
 %0A%0Adef te
@@ -1679,23 +1679,99 @@
 tor,
+%0A                os.path.join(os.path.dirname(os.path.dirname(__file__)),
  'docs
-/
+', '
 API.md')
 %0A%0Ade
@@ -1766,16 +1766,17 @@
 API.md')
+)
 %0A%0Adef te
@@ -1950,15 +1950,91 @@
 tor,
+%0A                os.path.join(os.path.dirname(os.path.dirname(__file__)),
  'docs
-/
+', '
 Flas
@@ -2045,11 +2045,12 @@
 tes.md')
+)
 %0A%0A%0A

Fix failing Travis test