"""
@brief      test log(time=1s)
"""

import sys
import os
import unittest
import warnings


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

try:
    import pyquickhelper as skip_
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..",
                "..",
                "pyquickhelper",
                "src")))
    if path not in sys.path:
        sys.path.append(path)
    if "PYQUICKHELPER" in os.environ and len(os.environ["PYQUICKHELPER"]) > 0:
        sys.path.append(os.environ["PYQUICKHELPER"])
    import pyquickhelper as skip_


from src.pymyinstall.installhelper.install_cmd_helper import run_cmd
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder
from pyquickhelper.pycode import is_travis_or_appveyor


class TestPyMyInstallCli(unittest.TestCase):

    def test_install_set_schedule(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        this = os.path.abspath(os.path.dirname(__file__))
        script = os.path.normpath(os.path.join(
            this, "..", "..", "src", "pymyinstall", "cli", "pymy_install.py"))
        cmd = "{0} {1} {2}".format(
            sys.executable, script, "--set=pyquickhelper --schedule")
        out, err = run_cmd(cmd, wait=True, fLOG=fLOG, communicate=False, timeout=20)
        if len(out) == 0:
            if is_travis_or_appveyor() == "appveyor":
                warnings.warn(
                    "CLI ISSUE cmd:\n{0}\nOUT:\n{1}\nERR\n{2}".format(cmd, out, err))
            else:
                raise Exception(
                    "cmd:\n{0}\nOUT:\n{1}\nERR\n{2}".format(cmd, out, err))

    def test_install_download(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_install_download")
        this = os.path.abspath(os.path.dirname(__file__))
        script = os.path.normpath(os.path.join(
            this, "..", "..", "src", "pymyinstall", "cli", "pymy_install.py"))
        cmd = "{0} {1} {2} --force --folder={3}".format(
            sys.executable, script, "colorama xlrd --download", temp)
        out, err = run_cmd(cmd, wait=True, fLOG=fLOG, communicate=False, timeout=20)
        fLOG("----", cmd)
        fLOG(out.replace("\r", "").replace("\n\n", "\n"))
        fLOG("-----")
        fLOG(err.replace("\r", "").replace("\n\n", "\n"))
        if len(out) == 0 and is_travis_or_appveyor() == "appveyor":
            warnings.warn(
                "CLI ISSUE cmd:\n{0}\nOUT:\n{1}\nERR\n{2}".format(cmd, out, err))
            return
        if "downloaded modules" not in out:
            raise Exception(
                "CMD: {0}\nOUT:\n{1}\nERR\n{2}".format(cmd, out, err))
        content = os.listdir(temp)
        if len(content) != 2:
            if is_travis_or_appveyor() == "appveyor":
                warnings.warn(
                    "CLI ISSUE cmd:\n{0}\nOUT:\n{1}\nERR\n{2}".format(cmd, out, err))
            else:
                raise Exception(
                    "cmd:\n{0}\nOUT:\n{1}\nERR\n{2}".format(cmd, out, err))


if __name__ == "__main__":
    unittest.main()

@@ -1320,32 +1320,53 @@
 == %22__main__%22)%0A%0A
+        flog = print%0A
         this = o
@@ -1399,32 +1399,32 @@
 name(__file__))%0A
-
         script =
@@ -1681,35 +1681,35 @@
 ait=True, fLOG=f
-LOG
+log
 , communicate=Fa
@@ -2198,32 +2198,53 @@
 == %22__main__%22)%0A%0A
+        flog = print%0A
         temp = g
@@ -2654,19 +2654,19 @@
 , fLOG=f
-LOG
+log
 , commun
@@ -2695,27 +2695,27 @@
 0)%0A        f
-LOG
+log
 (%22----%22, cmd
@@ -2725,19 +2725,19 @@
        f
-LOG
+log
 (out.rep
@@ -2783,19 +2783,19 @@
        f
-LOG
+log
 (%22-----%22
@@ -2805,19 +2805,19 @@
        f
-LOG
+log
 (err.rep

update unit test for travis