101 lines
3.7 KiB
Python
101 lines
3.7 KiB
Python
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from deploy_tool.installer import build_install_command, explain_install_failure, format_install_output, recommend_jdk_version
|
||
|
|
from deploy_tool.scanner import scan_project
|
||
|
|
|
||
|
|
|
||
|
|
class InstallerTests(unittest.TestCase):
|
||
|
|
def test_recommends_jdk_8_from_maven_source_property(self):
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
root = Path(tmp)
|
||
|
|
backend = root / "server"
|
||
|
|
(backend / "src" / "main" / "java").mkdir(parents=True)
|
||
|
|
(backend / "pom.xml").write_text(
|
||
|
|
"""
|
||
|
|
<project>
|
||
|
|
<properties>
|
||
|
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||
|
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||
|
|
</properties>
|
||
|
|
<dependencies>
|
||
|
|
<dependency>spring-boot-starter-web</dependency>
|
||
|
|
</dependencies>
|
||
|
|
</project>
|
||
|
|
""",
|
||
|
|
encoding="utf-8",
|
||
|
|
)
|
||
|
|
|
||
|
|
scan = scan_project(root)
|
||
|
|
|
||
|
|
recommendation = recommend_jdk_version(scan)
|
||
|
|
self.assertEqual(recommendation.version, "8")
|
||
|
|
self.assertIn("maven.compiler.source", recommendation.reason)
|
||
|
|
|
||
|
|
def test_recommends_jdk_17_for_spring_boot_3(self):
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
root = Path(tmp)
|
||
|
|
backend = root / "server"
|
||
|
|
(backend / "src" / "main" / "java").mkdir(parents=True)
|
||
|
|
(backend / "pom.xml").write_text(
|
||
|
|
"""
|
||
|
|
<project>
|
||
|
|
<parent>
|
||
|
|
<groupId>org.springframework.boot</groupId>
|
||
|
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||
|
|
<version>3.2.5</version>
|
||
|
|
</parent>
|
||
|
|
</project>
|
||
|
|
""",
|
||
|
|
encoding="utf-8",
|
||
|
|
)
|
||
|
|
|
||
|
|
scan = scan_project(root)
|
||
|
|
|
||
|
|
recommendation = recommend_jdk_version(scan)
|
||
|
|
self.assertEqual(recommendation.version, "17")
|
||
|
|
self.assertIn("Spring Boot 3", recommendation.reason)
|
||
|
|
|
||
|
|
def test_defaults_to_jdk_8_without_project_signal(self):
|
||
|
|
recommendation = recommend_jdk_version(None)
|
||
|
|
|
||
|
|
self.assertEqual(recommendation.version, "8")
|
||
|
|
self.assertIn("默认", recommendation.reason)
|
||
|
|
|
||
|
|
def test_builds_jdk_winget_install_command(self):
|
||
|
|
command = build_install_command("JDK", version="8")
|
||
|
|
|
||
|
|
self.assertEqual(command, ["winget", "install", "-e", "--id", "EclipseAdoptium.Temurin.8.JDK"])
|
||
|
|
|
||
|
|
def test_builds_node_winget_install_command(self):
|
||
|
|
command = build_install_command("Node.js")
|
||
|
|
|
||
|
|
self.assertEqual(command, ["winget", "install", "-e", "--id", "OpenJS.NodeJS.LTS"])
|
||
|
|
|
||
|
|
def test_formats_winget_progress_without_garbled_bar(self):
|
||
|
|
raw = "████████████▒▒▒▒▒▒ 63%\r".encode("utf-8")
|
||
|
|
|
||
|
|
self.assertEqual(format_install_output(raw), ["安装进度: 63%"])
|
||
|
|
|
||
|
|
def test_keeps_readable_winget_text(self):
|
||
|
|
raw = "已找到 Eclipse Temurin JDK [EclipseAdoptium.Temurin.8.JDK]\n".encode("utf-8")
|
||
|
|
|
||
|
|
self.assertEqual(format_install_output(raw), ["已找到 Eclipse Temurin JDK [EclipseAdoptium.Temurin.8.JDK]"])
|
||
|
|
|
||
|
|
|
||
|
|
def test_explains_missing_maven_winget_package(self):
|
||
|
|
message = explain_install_failure(
|
||
|
|
"Maven",
|
||
|
|
2316632084,
|
||
|
|
["找不到与输入条件匹配的程序包。"],
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assertIn("winget 当前源找不到 Maven 包", message)
|
||
|
|
self.assertIn("手动下载 Apache Maven", message)
|
||
|
|
self.assertIn("mvn.cmd", message)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|