Skip to content

Commit 87fe350

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 180f0db commit 87fe350

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__)
@@ -172,7 +171,7 @@ def get_lang_from_content(src_path, html=False, is_linux=False):
172171
if fnmatch.fnmatch(file, "*.html") or fnmatch.fnmatch(file, "*.htm") or \
173172
fnmatch.fnmatch(file, "*.shtml"):
174173
if not static_html_file:
175-
static_html_file = os.path.join(src_path, file)
174+
static_html_file = os.path.join(_dirpath, file)
176175
if fnmatch.fnmatch(file, "*.csproj"):
177176
package_netcore_file = os.path.join(src_path, file)
178177
if not os.path.isfile(package_netcore_file):
@@ -294,7 +293,7 @@ def parse_node_version(file_path):
294293

295294

296295
def _detect_python_version(file_path):
297-
"""Detect Python version from runtime.txt, .python-version, or Dockerfile in the project directory."""
296+
"""Detect Python version from runtime.txt or .python-version in the project directory."""
298297
import re
299298
src_dir = os.path.dirname(file_path) if file_path else ''
300299
if not src_dir:
@@ -468,10 +467,10 @@ def validate_runtime_os_combo(language, version_used_create, os_name, stack_help
468467
match = stack_helper.resolve(runtime_version, is_linux)
469468
if not match:
470469
raise ValidationError(
471-
"The runtime '{}' is not supported on {}. "
472-
"Please check supported runtimes with: 'az webapp list-runtimes --os {}'.\n"
470+
"The runtime '{}' is not supported on {os_name}. "
471+
"Please check supported runtimes with: 'az webapp list-runtimes --os {os_name}'.\n"
473472
"HINT: Try a different --os-type or --runtime value.".format(
474-
runtime_version, os_name, os_name))
473+
runtime_version, os_name=os_name))
475474

476475

477476
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
@@ -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

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

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

787765
class TestValidateRuntimeOsCombo(unittest.TestCase):

0 commit comments

Comments
 (0)