"""
@brief      test log(time=2s)
"""
import sys
import unittest
import textwrap

from pyquickhelper.pycode import ExtTestCase, get_temp_folder
from pyquickhelper.loghelper.process_script import execute_script, execute_script_get_local_variables
from pyquickhelper.loghelper.process_script import dictionary_as_class


class TestRunScript(ExtTestCase):

    def test_run_script(self):
        code = textwrap.dedent("""
            import os
            res = dict(a = os.getcwd())
        """)
        exe = execute_script(code)
        self.assertIsInstance(exe, dict)
        self.assertIn('res', exe)

    def test_run_script_error(self):
        code = textwrap.dedent("""
            import os
            res = dict('a' = os.getcwd())
        """)
        exe = execute_script(code)
        self.assertIsInstance(exe, dict)
        self.assertIn('ERROR', exe)

    def test_run_script_error2(self):
        code = textwrap.dedent("""
            import os
            res = dict(a = os.getcwd() + 3)
        """)
        exe = execute_script(code)
        self.assertIsInstance(exe, dict)
        self.assertIn('ERROR', exe)

    def test_run_script_process(self):
        code = textwrap.dedent("""
            import os
            res = dict(a = os.getcwd())
        """)
        exe = execute_script_get_local_variables(code)
        self.assertIsInstance(exe, dict)
        self.assertIn('res', exe)

    def test_run_script_process_check(self):
        code = textwrap.dedent("""
            import os
            res = dict(a = os.getcwd())
            import sys
            sys.path.append("-azerty-")
        """)
        exe = execute_script_get_local_variables(code)
        self.assertIsInstance(exe, dict)
        self.assertIn('res', exe)
        self.assertNotIn("-azerty-", sys.path)
        du = dictionary_as_class(exe)

    def test_dummy_class(self):
        cl = dictionary_as_class(dict(d1="e", r=4))
        st = str(cl)
        self.assertEqual(st, "{'d1': 'e', 'r': 4}")

    def test_run_script_popen(self):
        temp = get_temp_folder(__file__, "temp_run_script_popen")
        code = textwrap.dedent("""
            import os
            res = dict(a = os.getcwd())
        """)
        exe = execute_script(code, folder=temp)
        self.assertIsInstance(exe, dict)
        self.assertIn('res', exe)
        self.assertIn('__file__', exe)


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

@@ -136,17 +136,34 @@
 p_folder
+, skipif_appveyor
 %0A
-
 from pyq
@@ -364,16 +364,50 @@
 Case):%0A%0A
+    @skipif_appveyor(%22job stuck%22)%0A
     def 
@@ -642,32 +642,66 @@
 In('res', exe)%0A%0A
+    @skipif_appveyor(%22job stuck%22)%0A
     def test_run
@@ -938,32 +938,66 @@
 ('ERROR', exe)%0A%0A
+    @skipif_appveyor(%22job stuck%22)%0A
     def test_run
@@ -1237,32 +1237,66 @@
 ('ERROR', exe)%0A%0A
+    @skipif_appveyor(%22job stuck%22)%0A
     def test_run
@@ -1555,24 +1555,58 @@
 res', exe)%0A%0A
+    @skipif_appveyor(%22job stuck%22)%0A
     def test
@@ -2185,16 +2185,50 @@
 : 4%7D%22)%0A%0A
+    @skipif_appveyor(%22job stuck%22)%0A
     def 

disable unit test on appveyor