<commit_before>"""
@brief      test log(time=2s)
"""

import sys
import os
import unittest

if "temp_" in os.path.abspath(__file__):
    raise ImportError(
        "this file should not be imported in that location: " +
        os.path.abspath(__file__))

try:
    import src
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..")))
    if path not in sys.path:
        sys.path.append(path)
    import src

from src.pyquickhelper.loghelper import fLOG
from src.pyquickhelper.filehelper import FileTreeNode
from src.pyquickhelper.pycode import ExtTestCase
from src.pyquickhelper.helpgen.utils_sphinx_doc import filecontent_to_rst, replace_relative_import


class TestFileNodeTree(ExtTestCase):

    def test_src_import(self):
        """for pylint"""
        self.assertTrue(src is not None)

    def test_file_tree_node(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        this = os.path.abspath(os.path.dirname(__file__))
        folder = os.path.normpath(os.path.join(this, "..", "..", "src"))

        def filter(root, path, f, d):
            return "__pycache__" not in path and "__pycache__" not in f

        ftn = FileTreeNode(folder, fLOG=fLOG, log=True, log1=True,
                           filter=filter)
        if len(ftn) == 2:
            raise Exception("%d" % len(ftn))
        nb = 0
        nrst = 0
        for f in ftn:
            if f.isfile():
                hash = f.hash_md5_readfile()
                s = str(f)
                self.assertNotEmpty(s)
                self.assertNotEmpty(hash)
                nb += 1
                if nb > 15:
                    break

                if "__init__" not in f.name and ".py" in f.name and ".pyc" not in f.name:
                    content = f.get_content()
                    rst = filecontent_to_rst(f.fullname, content)
                    contr, doc = rst
                    nrst += 1
                    self.assertNotIn("no documentation", doc)
                    self.assertIn(".. _f-", contr)

                    cont2 = replace_relative_import(f.fullname)
                    lines = cont2.split("\n")
                    condition = "# replace # from ." in cont2
                    if not condition:
                        for line in lines:
                            if "from ." in line and "import" in line:
                                if sys.version_info[0] == 2:
                                    if "from __future__ " not in cont2:<commit_msg>fix failing unit test broken by #165<commit_after>"""
@brief      test log(time=2s)
"""

import sys
import os
import unittest

if "temp_" in os.path.abspath(__file__):
    raise ImportError(
        "this file should not be imported in that location: " +
        os.path.abspath(__file__))

try:
    import src
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..")))
    if path not in sys.path:
        sys.path.append(path)
    import src

from src.pyquickhelper.loghelper import fLOG
from src.pyquickhelper.filehelper import FileTreeNode
from src.pyquickhelper.pycode import ExtTestCase
from src.pyquickhelper.helpgen.utils_sphinx_doc import filecontent_to_rst, replace_relative_import_fct


class TestFileNodeTree(ExtTestCase):

    def test_src_import(self):
        """for pylint"""
        self.assertTrue(src is not None)

    def test_file_tree_node(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        this = os.path.abspath(os.path.dirname(__file__))
        folder = os.path.normpath(os.path.join(this, "..", "..", "src"))

        def filter(root, path, f, d):
            return "__pycache__" not in path and "__pycache__" not in f

        ftn = FileTreeNode(folder, fLOG=fLOG, log=True, log1=True,
                           filter=filter)
        if len(ftn) == 2:
            raise Exception("%d" % len(ftn))
        nb = 0
        nrst = 0
        for f in ftn:
            if f.isfile():
                hash = f.hash_md5_readfile()
                s = str(f)
                self.assertNotEmpty(s)
                self.assertNotEmpty(hash)
                nb += 1
                if nb > 15:
                    break

                if "__init__" not in f.name and ".py" in f.name and ".pyc" not in f.name:
                    content = f.get_content()
                    rst = filecontent_to_rst(f.fullname, content)
                    contr, doc = rst
                    nrst += 1
                    self.assertNotIn("no documentation", doc)
                    self.assertIn(".. _f-", contr)

                    cont2 = replace_relative_import_fct(f.fullname)
                    lines = cont2.split("\n")
                    condition = "# replace # from ." in cont2
                    if not condition:
                        for line in lines:
                            if "from ." in line and "import" in line:
                                if sys.version_info[0] == 2:
                                    if "from __future__ " not in cont2:
{'fraction_non_alphanumeric': 0.10016787912702854, 'fraction_numerical': 0.006901697444506622, 'mean_word_length': 1.50443717888837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2550994', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1523, 'n_words': 395}