@@ -650,54 +650,44 @@ class TestDetectOsFromSrc(unittest.TestCase):
650650 def test_python_detected_as_linux (self ):
651651 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
652652 import tempfile
653- tmp = tempfile .mkdtemp ()
654- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
655- f .write ('flask\n ' )
656- result = detect_os_from_src (tmp )
657- self .assertEqual (result , "Linux" )
658- import shutil
659- shutil .rmtree (tmp )
653+ with tempfile .TemporaryDirectory () as tmp :
654+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
655+ f .write ('flask\n ' )
656+ result = detect_os_from_src (tmp )
657+ self .assertEqual (result , "Linux" )
660658
661659 def test_node_detected_as_linux (self ):
662660 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
663661 import tempfile
664662 import json
665- tmp = tempfile .mkdtemp ()
666- with open (os .path .join (tmp , 'package.json' ), 'w' ) as f :
667- json .dump ({"name" : "test" , "version" : "1.0.0" }, f )
668- result = detect_os_from_src (tmp )
669- self .assertEqual (result , "Linux" )
670- import shutil
671- shutil .rmtree (tmp )
663+ with tempfile .TemporaryDirectory () as tmp :
664+ with open (os .path .join (tmp , 'package.json' ), 'w' ) as f :
665+ json .dump ({"name" : "test" , "version" : "1.0.0" }, f )
666+ result = detect_os_from_src (tmp )
667+ self .assertEqual (result , "Linux" )
672668
673669 def test_html_detected_as_linux (self ):
674670 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
675671 import tempfile
676- tmp = tempfile .mkdtemp ()
677- with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
678- f .write ('<html></html>' )
679- result = detect_os_from_src (tmp , html = True )
680- self .assertEqual (result , "Linux" )
681- import shutil
682- shutil .rmtree (tmp )
672+ with tempfile .TemporaryDirectory () as tmp :
673+ with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
674+ f .write ('<html></html>' )
675+ result = detect_os_from_src (tmp , html = True )
676+ self .assertEqual (result , "Linux" )
683677
684678 def test_runtime_override_python (self ):
685679 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
686680 import tempfile
687- tmp = tempfile .mkdtemp ()
688- result = detect_os_from_src (tmp , runtime = "python|3.11" )
689- self .assertEqual (result , "Linux" )
690- import shutil
691- shutil .rmtree (tmp )
681+ with tempfile .TemporaryDirectory () as tmp :
682+ result = detect_os_from_src (tmp , runtime = "python|3.11" )
683+ self .assertEqual (result , "Linux" )
692684
693685 def test_runtime_override_aspnet (self ):
694686 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
695687 import tempfile
696- tmp = tempfile .mkdtemp ()
697- result = detect_os_from_src (tmp , runtime = "aspnet|4.8" )
698- self .assertEqual (result , "Windows" )
699- import shutil
700- shutil .rmtree (tmp )
688+ with tempfile .TemporaryDirectory () as tmp :
689+ result = detect_os_from_src (tmp , runtime = "aspnet|4.8" )
690+ self .assertEqual (result , "Windows" )
701691
702692
703693class TestGetLangFromContent (unittest .TestCase ):
@@ -706,26 +696,22 @@ class TestGetLangFromContent(unittest.TestCase):
706696 def test_html_autodetected_without_flag (self ):
707697 from azure .cli .command_modules .appservice ._create_util import get_lang_from_content
708698 import tempfile
709- tmp = tempfile .mkdtemp ()
710- with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
711- f .write ('<html></html>' )
712- result = get_lang_from_content (tmp , html = False )
713- self .assertEqual (result ['language' ], 'static' )
714- import shutil
715- shutil .rmtree (tmp )
699+ with tempfile .TemporaryDirectory () as tmp :
700+ with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
701+ f .write ('<html></html>' )
702+ result = get_lang_from_content (tmp , html = False )
703+ self .assertEqual (result ['language' ], 'static' )
716704
717705 def test_python_takes_precedence_over_html (self ):
718706 from azure .cli .command_modules .appservice ._create_util import get_lang_from_content
719707 import tempfile
720- tmp = tempfile .mkdtemp ()
721- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
722- f .write ('flask\n ' )
723- with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
724- f .write ('<html></html>' )
725- result = get_lang_from_content (tmp , html = False )
726- self .assertEqual (result ['language' ], 'python' )
727- import shutil
728- shutil .rmtree (tmp )
708+ with tempfile .TemporaryDirectory () as tmp :
709+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
710+ f .write ('flask\n ' )
711+ with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
712+ f .write ('<html></html>' )
713+ result = get_lang_from_content (tmp , html = False )
714+ self .assertEqual (result ['language' ], 'python' )
729715
730716
731717class TestDetectPythonVersion (unittest .TestCase ):
@@ -734,54 +720,46 @@ class TestDetectPythonVersion(unittest.TestCase):
734720 def test_detect_from_runtime_txt (self ):
735721 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
736722 import tempfile
737- tmp = tempfile .mkdtemp ()
738- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
739- f .write ('flask\n ' )
740- with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
741- f .write ('python-3.11.4\n ' )
742- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
743- self .assertEqual (result , '3.11' )
744- import shutil
745- shutil .rmtree (tmp )
723+ with tempfile .TemporaryDirectory () as tmp :
724+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
725+ f .write ('flask\n ' )
726+ with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
727+ f .write ('python-3.11.4\n ' )
728+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
729+ self .assertEqual (result , '3.11' )
746730
747731 def test_detect_from_python_version_file (self ):
748732 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
749733 import tempfile
750- tmp = tempfile .mkdtemp ()
751- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
752- f .write ('flask\n ' )
753- with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
754- f .write ('3.10.2\n ' )
755- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
756- self .assertEqual (result , '3.10' )
757- import shutil
758- shutil .rmtree (tmp )
734+ with tempfile .TemporaryDirectory () as tmp :
735+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
736+ f .write ('flask\n ' )
737+ with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
738+ f .write ('3.10.2\n ' )
739+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
740+ self .assertEqual (result , '3.10' )
759741
760742 def test_no_version_file_returns_dash (self ):
761743 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
762744 import tempfile
763- tmp = tempfile .mkdtemp ()
764- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
765- f .write ('flask\n ' )
766- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
767- self .assertEqual (result , '-' )
768- import shutil
769- shutil .rmtree (tmp )
745+ with tempfile .TemporaryDirectory () as tmp :
746+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
747+ f .write ('flask\n ' )
748+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
749+ self .assertEqual (result , '-' )
770750
771751 def test_runtime_txt_takes_precedence (self ):
772752 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
773753 import tempfile
774- tmp = tempfile .mkdtemp ()
775- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
776- f .write ('flask\n ' )
777- with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
778- f .write ('python-3.12.0\n ' )
779- with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
780- f .write ('3.10\n ' )
781- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
782- self .assertEqual (result , '3.12' )
783- import shutil
784- shutil .rmtree (tmp )
754+ with tempfile .TemporaryDirectory () as tmp :
755+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
756+ f .write ('flask\n ' )
757+ with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
758+ f .write ('python-3.12.0\n ' )
759+ with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
760+ f .write ('3.10\n ' )
761+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
762+ self .assertEqual (result , '3.12' )
785763
786764
787765class TestValidateRuntimeOsCombo (unittest .TestCase ):
0 commit comments