Skip to content

Commit

Permalink
Updates to Edge driver enhancements (#24)
Browse files Browse the repository at this point in the history
* Adding tests for edge

* Updating edge to download latest AND stable
  • Loading branch information
bandophahita authored May 12, 2022
1 parent e4cc40a commit a5f7838
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 24 deletions.
56 changes: 32 additions & 24 deletions pyderman/drivers/edge.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down
121 changes: 121 additions & 0 deletions pyderman/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit a5f7838

Please sign in to comment.