- versions: require entire normalized string to be numeric so dates and hotfix suffixes compare as incomparable instead of eq/gt - hosts: exact host or dot-suffix match rejects lookalikes like notmediafire.com and evilmega.nz; GitHub stays exact-set - census: lazy extractor loop so a later OSError no longer discards an earlier sidecar changelog hit - jobs: watchlist parses only table rows whose vault-note cell is a bare wikilink (skips parenthetical related/complete notes); wanted also matches bold names in Wanted.md tables with Pokemon prefix stripped and casefolded - cli: validate-scouts and judge raise SystemExit when jobs.json has jobs but scouts/ has zero valid files - fetch: download forwards timeout=60 to urlopen and writes to a .part file then os.replace, removing the partial on any failure
66 lines
2.9 KiB
Python
66 lines
2.9 KiB
Python
import os, sys, tempfile, unittest, zipfile
|
|
from unittest.mock import patch
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
sys.path.insert(0, os.path.join(ROOT, "scripts"))
|
|
from update_swarm.fetch import GatedHostError, download, english_mismatch, resolve_mediafire
|
|
|
|
class TestFetch(unittest.TestCase):
|
|
def test_mediafire_regex(self):
|
|
html = '<a href="https://download123.mediafire.com/abc/file.zip">dl</a>'
|
|
self.assertEqual(resolve_mediafire(html), "https://download123.mediafire.com/abc/file.zip")
|
|
|
|
def test_refuses_gated(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
with self.assertRaises(GatedHostError):
|
|
download("https://foo.itch.io/x", os.path.join(td, "x.zip"))
|
|
|
|
def test_download_writes_bytes(self):
|
|
class Resp:
|
|
def __init__(self):
|
|
self._done = False
|
|
def __enter__(self): return self
|
|
def __exit__(self, *a): pass
|
|
def read(self, n=-1):
|
|
if self._done:
|
|
return b""
|
|
self._done = True
|
|
return b"rombytes"
|
|
with tempfile.TemporaryDirectory() as td:
|
|
dest = os.path.join(td, "g.gba")
|
|
with patch("update_swarm.fetch.urllib.request.urlopen", return_value=Resp()) as mock_urlopen:
|
|
rec = download("https://github.com/a/b/releases/download/v1/g.gba", dest)
|
|
self.assertTrue(os.path.isfile(dest))
|
|
self.assertEqual(rec["size"], 8)
|
|
# timeout kwarg must be forwarded to urlopen
|
|
self.assertEqual(mock_urlopen.call_args.kwargs.get("timeout"), 60)
|
|
|
|
def test_download_failure_leaves_no_dest(self):
|
|
class Resp:
|
|
def __enter__(self): return self
|
|
def __exit__(self, *a): pass
|
|
def read(self, n=-1):
|
|
raise OSError("network died")
|
|
with tempfile.TemporaryDirectory() as td:
|
|
dest = os.path.join(td, "g.gba")
|
|
with patch("update_swarm.fetch.urllib.request.urlopen", return_value=Resp()):
|
|
with self.assertRaises(OSError):
|
|
download("https://github.com/a/b/releases/download/v1/g.gba", dest)
|
|
self.assertFalse(os.path.exists(dest))
|
|
self.assertFalse(os.path.exists(dest + ".part"))
|
|
|
|
def test_mega_not_implemented(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
with self.assertRaises(NotImplementedError):
|
|
download("https://mega.nz/file/abc#key", os.path.join(td, "x"))
|
|
|
|
def test_changelog_mismatch(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
zpath = os.path.join(td, "g.zip")
|
|
with zipfile.ZipFile(zpath, "w") as z:
|
|
z.writestr("changelog.txt", "Current Version: 2.16\n")
|
|
self.assertTrue(english_mismatch(zpath, "2.13"))
|
|
self.assertFalse(english_mismatch(zpath, "v2.16 (ES)"))
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|