Repository: shadowmoose/pyderman (language: python)

Issue:
Edge driver enhancements
Closes #22.


Discussion / hints:

Fix patch:
diff --git a/pyderman/drivers/edge.py b/pyderman/drivers/edge.py
index 5fb08ca..67dff05 100644
--- a/pyderman/drivers/edge.py
+++ b/pyderman/drivers/edge.py
@@ -1,38 +1,46 @@
 from __future__ import annotations
 
+import re
+
 from pyderman.util import downloader
 
+_stable = "https://msedgedriver.azureedge.net/LATEST_STABLE"
+_base_version = "https://msedgedriver.azureedge.net/LATEST_RELEASE"
+_base_download = "https://msedgedriver.azureedge.net/{}/edgedriver_{}{}.zip"
+
 
 def get_url(
     version: str = "latest", _os: str | None = None, _os_bit: str | None = None
 ) -> tuple[str, str, str]:
-    if version == "latest":
-        try:
-            version = latest()
-        except Exception as e:
-            print(e)
+
     if _os == "win":
-        url = "https://msedgedriver.azureedge.net/{}/edgedriver_win{}.zip".format(
-            version,
-            _os_bit,
-        )
-    elif _os == "mac":
-        url = "https://msedgedriver.azureedge.net/%s/edgedriver_mac64.zip" % version
+        _os_name = "WINDOWS"
     elif _os == "linux":
-        url = "https://msedgedriver.azureedge.net/%s/edgedriver_linux64.zip" % version
+        _os_name = "LINUX"
+    elif _os == "mac":
+        _os_name = "MACOS"
     else:
-        raise OSError("There is no valid EdgeDriver release for %s" % _os)
-    if not version:
-        raise ValueError("Unable to automatically locate latest version of EdgeDriver!")
-    return "msedgedriver", url, version
-
-
-def latest() -> str:
-    url = "https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver/LATEST_STABLE"
-    data = downloader.raw(url, "utf-16")
-    if data is None:
-        raise Exception("Unable to get: %s" % url)
-    return data.strip()
+        raise OSError("There is no valid EdgeDriver release for {}".format(_os))
+
+    if version in ("latest", "stable"):
+        resolved_version = downloader.raw(_stable, "utf-16")
+        resolved_version = resolved_version.strip() if resolved_version else ""
+
+        if version == "latest":
+            match = re.match(r"^(\d*)[.]?(\d*)[.]?(\d*)[.]?(\d*)$", resolved_version)
+            if match:
+                major, minor, patch, build = match.groups()
+                _url = "{}_{}_{}".format(_base_version, major, _os_name)
+                resolved_version = downloader.raw(_url, "utf-16")
+                resolved_version = resolved_version.strip() if resolved_version else ""
+    else:
+        resolved_version = version
+
+    url = _base_download.format(resolved_version, _os, _os_bit)
+
+    if not resolved_version:
+        raise Exception("Unable to locate EdgeDriver version: {}!".format(version))
+    return "msedgedriver", url, resolved_version
 
 
 if __name__ == "__main__":


Test patch:
diff --git a/pyderman/test.py b/pyderman/test.py
index 127d25a..17ece43 100644
--- a/pyderman/test.py
+++ b/pyderman/test.py
@@ -117,5 +117,126 @@ def test_get_nonsense(self) -> None:
         return
 
 
+class TestEdge(unittest.TestCase):
+    version_re = re.compile(r"^(\d+)\.(\d+)\.(\d+)\.(\d+)$")
+    stable = None
+
+    @staticmethod
+    def fetch(url: str) -> str:
+        resp = urlopen(url, timeout=15)
+        html = resp.read().decode("utf-16", errors="ignore").strip()
+        return str(html)
+
+    def setUp(self) -> None:
+        url = "https://msedgedriver.azureedge.net/LATEST_STABLE"
+        if self.stable is None:
+            self.stable = self.fetch(url)
+
+            match = self.version_re.match(self.stable)
+            if not match:
+                raise ValueError(f"Invalid version string: {self.stable!r}")
+
+            major, minor, patch, build = match.groups()
+            self.major = major
+        return
+
+    def get_latest_os(self, major: str, _os: str) -> str:
+        url = f"https://msedgedriver.azureedge.net/LATEST_RELEASE_{major}_{_os.upper()}"
+        return self.fetch(url)
+
+    def test_get_latest_mac(self) -> None:
+        drvr, url, vers = edge.get_url("latest", _os="mac", _os_bit="64")
+        latest_mac = self.get_latest_os(self.major, "MACOS")
+        self.assertEqual(vers, latest_mac)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{latest_mac}/edgedriver_mac64.zip",
+        )
+
+    def test_get_stable_mac(self) -> None:
+        drvr, url, vers = edge.get_url("stable", _os="mac", _os_bit="64")
+        self.assertEqual(vers, self.stable)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{self.stable}/edgedriver_mac64.zip",
+        )
+
+    def test_get_latest_linux(self) -> None:
+        drvr, url, vers = edge.get_url("latest", _os="linux", _os_bit="64")
+        latest_linux = self.get_latest_os(self.major, "LINUX")
+        self.assertEqual(vers, latest_linux)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{latest_linux}/edgedriver_linux64.zip",
+        )
+
+    def test_get_stable_linux(self) -> None:
+        drvr, url, vers = edge.get_url("stable", _os="linux", _os_bit="64")
+        self.assertEqual(vers, self.stable)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{self.stable}/edgedriver_linux64.zip",
+        )
+
+    def test_get_latest_windows(self) -> None:
+        drvr, url, vers = edge.get_url("latest", _os="win", _os_bit="64")
+        latest_win = self.get_latest_os(self.major, "WINDOWS")
+        self.assertEqual(vers, latest_win)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{latest_win}/edgedriver_win64.zip",
+        )
+
+    def test_get_stable_windows(self) -> None:
+        drvr, url, vers = edge.get_url("stable", _os="win", _os_bit="64")
+        self.assertEqual(vers, self.stable)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{self.stable}/edgedriver_win64.zip",
+        )
+
+    def test_get_major(self) -> None:
+        """only proves url is created, not that it's valid"""
+        drvr, url, vers = edge.get_url(self.major, _os="mac", _os_bit="64")
+        self.assertEqual(vers, self.major)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{self.major}/edgedriver_mac64.zip",
+        )
+
+    def test_get_build(self) -> None:
+        url_latest = f"https://msedgedriver.azureedge.net/LATEST_RELEASE_{self.major}"
+        self.latest = self.fetch(url_latest)
+
+        match_latest = self.version_re.match(self.latest)
+        if not match_latest:
+            raise ValueError(f"Invalid version string: {self.latest!r}")
+
+        major_latest, minor_latest, patch_latest, build_latest = match_latest.groups()
+        self.major_latest = major_latest
+
+        drvr, url, vers = edge.get_url(self.latest, _os="mac", _os_bit="64")
+        self.assertEqual(vers, self.latest)
+        self.assertEqual(
+            url,
+            f"https://msedgedriver.azureedge.net/{self.latest}/edgedriver_mac64.zip",
+        )
+
+    def test_get_invalid_os(self) -> None:
+        with self.assertRaises(OSError) as exc:
+            edge.get_url("1.2.3.4", "lcars", "4096")
+        self.assertEqual(
+            str(exc.exception), "There is no valid EdgeDriver release for lcars"
+        )
+
+    def test_unresolved_version(self) -> None:
+        with self.assertRaises(Exception) as exc:
+            edge.get_url(None, _os="mac", _os_bit="64")  # type: ignore
+        self.assertEqual(
+            str(exc.exception), "Unable to locate EdgeDriver version: None!"
+        )
+        return
+
+
 if __name__ == "__main__":
     unittest.main()