import os
import tempfile
import textwrap

import pytest
from conans import load

from conan.tools import CONAN_TOOLCHAIN_ARGS_FILE, CONAN_TOOLCHAIN_ARGS_SECTION
from conan.tools.files import load_toolchain_args, save_toolchain_args
from conans.errors import ConanException
from conans.util.files import save, remove


def test_load_empty_toolchain_args_in_default_dir():
    save(CONAN_TOOLCHAIN_ARGS_FILE, "[%s]" % CONAN_TOOLCHAIN_ARGS_SECTION)
    try:
        config = load_toolchain_args()
        assert not config
    finally:
        remove(CONAN_TOOLCHAIN_ARGS_FILE)


def test_load_toolchain_args_if_it_does_not_exist():
    with pytest.raises(ConanException):
        load_toolchain_args()


def test_toolchain_args_with_content_full():
    temp_folder = tempfile.mkdtemp()
    content = textwrap.dedent(r"""
    [%s]
    win_path=my\win\path
    command=conan --option "My Option"
    my_regex=([A-Z])\w+
    """ % CONAN_TOOLCHAIN_ARGS_SECTION)
    save(os.path.join(temp_folder,  CONAN_TOOLCHAIN_ARGS_FILE), content)
    args = load_toolchain_args(generators_folder=temp_folder)
    assert args["win_path"] == r'my\win\path'
    assert args["command"] == r'conan --option "My Option"'
    assert args["my_regex"] == r'([A-Z])\w+'


def test_save_toolchain_args_empty():
    temp_folder = tempfile.mkdtemp()
    content = {}
    save_toolchain_args(content, generators_folder=temp_folder)
    args = load(os.path.join(temp_folder, CONAN_TOOLCHAIN_ARGS_FILE))
    assert "[%s]" % CONAN_TOOLCHAIN_ARGS_SECTION in args


def test_save_toolchain_args_full():
    temp_folder = tempfile.mkdtemp()
    content = {
        'win_path': r'my\win\path',
        'command': r'conan --option "My Option"',
        'my_regex': r'([A-Z])\w+'
    }
    save_toolchain_args(content, generators_folder=temp_folder)
    args = load(os.path.join(temp_folder, CONAN_TOOLCHAIN_ARGS_FILE))
    assert "[%s]" % CONAN_TOOLCHAIN_ARGS_SECTION in args
    assert r'win_path = my\win\path' in args

@@ -7,24 +7,8 @@
  os%0A
-import tempfile%0A
 impo
@@ -251,16 +251,69 @@
 ception%0A
+from conans.test.utils.test_files import temp_folder%0A
 from con
@@ -398,24 +398,72 @@
 ault_dir():%0A
+    folder = temp_folder()%0A    os.chdir(folder)%0A
     save(CON
@@ -705,24 +705,72 @@
 ot_exist():%0A
+    folder = temp_folder()%0A    os.chdir(folder)%0A
     with pyt
@@ -869,32 +869,41 @@
 tent_full():%0A   
+ folder =
  temp_folder = t
@@ -898,35 +898,16 @@
 p_folder
- = tempfile.mkdtemp
 ()%0A    c
@@ -1087,37 +1087,32 @@
 ve(os.path.join(
-temp_
 folder,  CONAN_T
@@ -1182,37 +1182,32 @@
 nerators_folder=
-temp_
 folder)%0A    asse
@@ -1385,37 +1385,32 @@
 gs_empty():%0A    
-temp_
 folder = tempfil
@@ -1402,36 +1402,31 @@
 older = temp
-file.mkdtemp
+_folder
 ()%0A    conte
@@ -1476,37 +1476,32 @@
 nerators_folder=
-temp_
 folder)%0A    args
@@ -1513,37 +1513,32 @@
 ad(os.path.join(
-temp_
 folder, CONAN_TO
@@ -1656,16 +1656,25 @@
 l():%0A   
+ folder =
  temp_fo
@@ -1681,27 +1681,8 @@
 lder
- = tempfile.mkdtemp
 ()%0A 
@@ -1873,21 +1873,16 @@
 _folder=
-temp_
 folder)%0A
@@ -1910,21 +1910,16 @@
 th.join(
-temp_
 folder, 

fix concurrent test