@@ -709,54 +709,44 @@ class TestDetectOsFromSrc(unittest.TestCase):
709709 def test_python_detected_as_linux (self ):
710710 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
711711 import tempfile
712- tmp = tempfile .mkdtemp ()
713- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
714- f .write ('flask\n ' )
715- result = detect_os_from_src (tmp )
716- self .assertEqual (result , "Linux" )
717- import shutil
718- shutil .rmtree (tmp )
712+ with tempfile .TemporaryDirectory () as tmp :
713+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
714+ f .write ('flask\n ' )
715+ result = detect_os_from_src (tmp )
716+ self .assertEqual (result , "Linux" )
719717
720718 def test_node_detected_as_linux (self ):
721719 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
722720 import tempfile
723721 import json
724- tmp = tempfile .mkdtemp ()
725- with open (os .path .join (tmp , 'package.json' ), 'w' ) as f :
726- json .dump ({"name" : "test" , "version" : "1.0.0" }, f )
727- result = detect_os_from_src (tmp )
728- self .assertEqual (result , "Linux" )
729- import shutil
730- shutil .rmtree (tmp )
722+ with tempfile .TemporaryDirectory () as tmp :
723+ with open (os .path .join (tmp , 'package.json' ), 'w' ) as f :
724+ json .dump ({"name" : "test" , "version" : "1.0.0" }, f )
725+ result = detect_os_from_src (tmp )
726+ self .assertEqual (result , "Linux" )
731727
732728 def test_html_detected_as_linux (self ):
733729 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
734730 import tempfile
735- tmp = tempfile .mkdtemp ()
736- with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
737- f .write ('<html></html>' )
738- result = detect_os_from_src (tmp , html = True )
739- self .assertEqual (result , "Linux" )
740- import shutil
741- shutil .rmtree (tmp )
731+ with tempfile .TemporaryDirectory () as tmp :
732+ with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
733+ f .write ('<html></html>' )
734+ result = detect_os_from_src (tmp , html = True )
735+ self .assertEqual (result , "Linux" )
742736
743737 def test_runtime_override_python (self ):
744738 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
745739 import tempfile
746- tmp = tempfile .mkdtemp ()
747- result = detect_os_from_src (tmp , runtime = "python|3.11" )
748- self .assertEqual (result , "Linux" )
749- import shutil
750- shutil .rmtree (tmp )
740+ with tempfile .TemporaryDirectory () as tmp :
741+ result = detect_os_from_src (tmp , runtime = "python|3.11" )
742+ self .assertEqual (result , "Linux" )
751743
752744 def test_runtime_override_aspnet (self ):
753745 from azure .cli .command_modules .appservice ._create_util import detect_os_from_src
754746 import tempfile
755- tmp = tempfile .mkdtemp ()
756- result = detect_os_from_src (tmp , runtime = "aspnet|4.8" )
757- self .assertEqual (result , "Windows" )
758- import shutil
759- shutil .rmtree (tmp )
747+ with tempfile .TemporaryDirectory () as tmp :
748+ result = detect_os_from_src (tmp , runtime = "aspnet|4.8" )
749+ self .assertEqual (result , "Windows" )
760750
761751
762752class TestGetLangFromContent (unittest .TestCase ):
@@ -765,26 +755,22 @@ class TestGetLangFromContent(unittest.TestCase):
765755 def test_html_autodetected_without_flag (self ):
766756 from azure .cli .command_modules .appservice ._create_util import get_lang_from_content
767757 import tempfile
768- tmp = tempfile .mkdtemp ()
769- with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
770- f .write ('<html></html>' )
771- result = get_lang_from_content (tmp , html = False )
772- self .assertEqual (result ['language' ], 'static' )
773- import shutil
774- shutil .rmtree (tmp )
758+ with tempfile .TemporaryDirectory () as tmp :
759+ with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
760+ f .write ('<html></html>' )
761+ result = get_lang_from_content (tmp , html = False )
762+ self .assertEqual (result ['language' ], 'static' )
775763
776764 def test_python_takes_precedence_over_html (self ):
777765 from azure .cli .command_modules .appservice ._create_util import get_lang_from_content
778766 import tempfile
779- tmp = tempfile .mkdtemp ()
780- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
781- f .write ('flask\n ' )
782- with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
783- f .write ('<html></html>' )
784- result = get_lang_from_content (tmp , html = False )
785- self .assertEqual (result ['language' ], 'python' )
786- import shutil
787- shutil .rmtree (tmp )
767+ with tempfile .TemporaryDirectory () as tmp :
768+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
769+ f .write ('flask\n ' )
770+ with open (os .path .join (tmp , 'index.html' ), 'w' ) as f :
771+ f .write ('<html></html>' )
772+ result = get_lang_from_content (tmp , html = False )
773+ self .assertEqual (result ['language' ], 'python' )
788774
789775
790776class TestDetectPythonVersion (unittest .TestCase ):
@@ -793,54 +779,46 @@ class TestDetectPythonVersion(unittest.TestCase):
793779 def test_detect_from_runtime_txt (self ):
794780 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
795781 import tempfile
796- tmp = tempfile .mkdtemp ()
797- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
798- f .write ('flask\n ' )
799- with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
800- f .write ('python-3.11.4\n ' )
801- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
802- self .assertEqual (result , '3.11' )
803- import shutil
804- shutil .rmtree (tmp )
782+ with tempfile .TemporaryDirectory () as tmp :
783+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
784+ f .write ('flask\n ' )
785+ with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
786+ f .write ('python-3.11.4\n ' )
787+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
788+ self .assertEqual (result , '3.11' )
805789
806790 def test_detect_from_python_version_file (self ):
807791 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
808792 import tempfile
809- tmp = tempfile .mkdtemp ()
810- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
811- f .write ('flask\n ' )
812- with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
813- f .write ('3.10.2\n ' )
814- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
815- self .assertEqual (result , '3.10' )
816- import shutil
817- shutil .rmtree (tmp )
793+ with tempfile .TemporaryDirectory () as tmp :
794+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
795+ f .write ('flask\n ' )
796+ with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
797+ f .write ('3.10.2\n ' )
798+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
799+ self .assertEqual (result , '3.10' )
818800
819801 def test_no_version_file_returns_dash (self ):
820802 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
821803 import tempfile
822- tmp = tempfile .mkdtemp ()
823- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
824- f .write ('flask\n ' )
825- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
826- self .assertEqual (result , '-' )
827- import shutil
828- shutil .rmtree (tmp )
804+ with tempfile .TemporaryDirectory () as tmp :
805+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
806+ f .write ('flask\n ' )
807+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
808+ self .assertEqual (result , '-' )
829809
830810 def test_runtime_txt_takes_precedence (self ):
831811 from azure .cli .command_modules .appservice ._create_util import _detect_python_version
832812 import tempfile
833- tmp = tempfile .mkdtemp ()
834- with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
835- f .write ('flask\n ' )
836- with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
837- f .write ('python-3.12.0\n ' )
838- with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
839- f .write ('3.10\n ' )
840- result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
841- self .assertEqual (result , '3.12' )
842- import shutil
843- shutil .rmtree (tmp )
813+ with tempfile .TemporaryDirectory () as tmp :
814+ with open (os .path .join (tmp , 'requirements.txt' ), 'w' ) as f :
815+ f .write ('flask\n ' )
816+ with open (os .path .join (tmp , 'runtime.txt' ), 'w' ) as f :
817+ f .write ('python-3.12.0\n ' )
818+ with open (os .path .join (tmp , '.python-version' ), 'w' ) as f :
819+ f .write ('3.10\n ' )
820+ result = _detect_python_version (os .path .join (tmp , 'requirements.txt' ))
821+ self .assertEqual (result , '3.12' )
844822
845823
846824class TestValidateRuntimeOsCombo (unittest .TestCase ):
0 commit comments