<commit_before>"""Make sure we don't hammer the API."""

import re
import time
from subprocess import check_output, STDOUT

from tests.helpers import change_doc, init_sample_docs

COMMAND = ['sphinx-build', '-n', '-N', '-W', '.', '_build/html']


def test_first_run(tmpdir_module):
    """First run."""
    init_sample_docs(tmpdir_module)
    check_output(COMMAND, cwd=str(tmpdir_module), stderr=STDOUT)

    outdir = tmpdir_module.join('_build', 'html')
    out_doc1_html = outdir.join('doc1.html').read()
    out_doc2_html = outdir.join('doc2.html').read()
    assert 'The title is: Title.' in out_doc1_html
    assert 'And the description: Desc' in out_doc1_html
    assert 'The title is: Title2.' in out_doc2_html


def test_remove_imgur_api_test_response(tmpdir_module):
    """Remove imgur_api_test_response from conf.py. Extension should only use cached data."""
    conf_py = tmpdir_module.join('conf.py').read()
    new_conf_py = re.sub(r'\nimgur_api_test_response [^}]+\n}', '', conf_py).strip()
    tmpdir_module.join('conf.py').write(new_conf_py + '\n')
    assert 'imgur_api_test_response' not in new_conf_py

    time.sleep(2)
    change_doc(tmpdir_module)
    check_output(COMMAND, cwd=str(tmpdir_module), stderr=STDOUT)

    outdir = tmpdir_module.join('_build', 'html')
    out_doc1_html = outdir.join('doc1.html').read()
    out_doc2_html = outdir.join('doc2.html').read()
    assert 'The title is still: Title.' in out_doc1_html
    assert 'And the description: Desc' in out_doc1_html
    assert 'The title is: Title2.' in out_doc2_html<commit_msg>Testing API kindness with additional file.<commit_after>"""Make sure we don't hammer the API."""

import re
import time
from subprocess import check_output, STDOUT
from textwrap import dedent

from tests.helpers import change_doc, init_sample_docs

COMMAND = ['sphinx-build', '-n', '-N', '-W', '.', '_build/html']


def test_first_run(tmpdir_module):
    """First run."""
    init_sample_docs(tmpdir_module)
    check_output(COMMAND, cwd=str(tmpdir_module), stderr=STDOUT)

    outdir = tmpdir_module.join('_build', 'html')
    out_doc1_html = outdir.join('doc1.html').read()
    out_doc2_html = outdir.join('doc2.html').read()
    assert 'The title is: Title.' in out_doc1_html
    assert 'And the description: Desc' in out_doc1_html
    assert 'The title is: Title2.' in out_doc2_html


def test_remove_imgur_api_test_response(tmpdir_module):
    """Remove imgur_api_test_response from conf.py. Extension should only use cached data."""
    conf_py = tmpdir_module.join('conf.py').read()
    new_conf_py = re.sub(r'\nimgur_api_test_response [^}]+\n}', '', conf_py).strip()
    tmpdir_module.join('conf.py').write(new_conf_py + '\n')
    assert 'imgur_api_test_response' not in new_conf_py

    time.sleep(2)
    change_doc(tmpdir_module)
    check_output(COMMAND, cwd=str(tmpdir_module), stderr=STDOUT)

    outdir = tmpdir_module.join('_build', 'html')
    out_doc1_html = outdir.join('doc1.html').read()
    out_doc2_html = outdir.join('doc2.html').read()
    assert 'The title is still: Title.' in out_doc1_html
    assert 'And the description: Desc' in out_doc1_html
    assert 'The title is: Title2.' in out_doc2_html


def test_add_new_rst_file(tmpdir_module):
    """Add a new document but use a pre-existing Imgur ID."""
    tmpdir_module.join('doc3.rst').write(dedent("""\
        .. _doc3:

        More Images
        ===========

        | Test title: :imgur-title:`1234abc`.
        """))
    index_rst = tmpdir_module.join('index.rst').read().strip().splitlines()
    index_rst.extend(['    doc3', ''])
    tmpdir_module.join('index.rst').write('\n'.join(index_rst))

    time.sleep(2)
    change_doc(tmpdir_module)
    check_output(COMMAND, cwd=str(tmpdir_module), stderr=STDOUT)

    outdir = tmpdir_module.join('_build', 'html')
    out_doc1_html = outdir.join('doc1.html').read()
    out_doc2_html = outdir.join('doc2.html').read()
    out_doc3_html = outdir.join('doc3.html').read()
    assert 'The title is still: Title.' in out_doc1_html
    assert 'And the description: Desc' in out_doc1_html
    assert 'The title is: Title2.' in out_doc2_html
    assert 'Test title: Title2.' in out_doc3_html
{'fraction_non_alphanumeric': 0.13906810035842293, 'fraction_numerical': 0.012903225806451613, 'mean_word_length': 4.305449936628644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4302885', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1463, 'n_words': 318}