Skip to content

Commit 840fac0

Browse files
seligj95Copilot
andcommitted
Fix review comments: unused import, HTML path, docstring, test cleanup, lint
- Remove unused LINUX_OS_NAME import (_create_util.py) - Fix static HTML path: use _dirpath instead of src_path in os.path.join - Update _detect_python_version docstring to match implementation - Fix duplicate string formatting argument (pylint W1308) - Replace tempfile.mkdtemp()+shutil.rmtree() with TemporaryDirectory() Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9dbee9c commit 840fac0

2 files changed

Lines changed: 67 additions & 90 deletions

File tree

src/azure-cli/azure/cli/command_modules/appservice/_create_util.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
from ._constants import (NETCORE_RUNTIME_NAME, NODE_RUNTIME_NAME, ASPDOTNET_RUNTIME_NAME, STATIC_RUNTIME_NAME,
1717
PYTHON_RUNTIME_NAME, LINUX_SKU_DEFAULT, OS_DEFAULT, DOTNET_RUNTIME_NAME,
18-
DOTNET_TARGET_FRAMEWORK_REGEX, GENERATE_RANDOM_APP_NAMES, DOTNET_REFERENCES_DIR_IN_ZIP,
19-
LINUX_OS_NAME)
18+
DOTNET_TARGET_FRAMEWORK_REGEX, GENERATE_RANDOM_APP_NAMES, DOTNET_REFERENCES_DIR_IN_ZIP)
2019
from .utils import get_resource_if_exists
2120

2221
logger = get_logger(__name__)
@@ -176,7 +175,7 @@ def get_lang_from_content(src_path, html=False, is_linux=False):
176175
if fnmatch.fnmatch(file, "*.html") or fnmatch.fnmatch(file, "*.htm") or \
177176
fnmatch.fnmatch(file, "*.shtml"):
178177
if not static_html_file:
179-
static_html_file = os.path.join(src_path, file)
178+
static_html_file = os.path.join(_dirpath, file)
180179
if fnmatch.fnmatch(file, "*.csproj"):
181180
package_netcore_file = os.path.join(src_path, file)
182181
if not os.path.isfile(package_netcore_file):
@@ -298,7 +297,7 @@ def parse_node_version(file_path):
298297

299298

300299
def _detect_python_version(file_path):
301-
"""Detect Python version from runtime.txt, .python-version, or Dockerfile in the project directory."""
300+
"""Detect Python version from runtime.txt or .python-version in the project directory."""
302301
import re
303302
src_dir = os.path.dirname(file_path) if file_path else ''
304303
if not src_dir:
@@ -472,10 +471,10 @@ def validate_runtime_os_combo(language, version_used_create, os_name, stack_help
472471
match = stack_helper.resolve(runtime_version, is_linux)
473472
if not match:
474473
raise ValidationError(
475-
"The runtime '{}' is not supported on {}. "
476-
"Please check supported runtimes with: 'az webapp list-runtimes --os {}'.\n"
474+
"The runtime '{}' is not supported on {os_name}. "
475+
"Please check supported runtimes with: 'az webapp list-runtimes --os {os_name}'.\n"
477476
"HINT: Try a different --os-type or --runtime value.".format(
478-
runtime_version, os_name, os_name))
477+
runtime_version, os_name=os_name))
479478

480479

481480
def get_plan_to_use(cmd, user, loc, sku, create_rg, resource_group_name, client, is_linux=False, plan=None):

src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_webapp_commands_thru_mock.py

Lines changed: 61 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -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

762752
class 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

790776
class 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

846824
class TestValidateRuntimeOsCombo(unittest.TestCase):

0 commit comments

Comments
 (0)