go: style: unify indentation etc

This commit is contained in:
lilydjwg 2024-03-09 18:48:47 +08:00
parent 35a19f30ae
commit 4ad3bdb95d
2 changed files with 37 additions and 37 deletions

View file

@ -4,33 +4,33 @@
from lxml import html from lxml import html
from nvchecker.api import ( from nvchecker.api import (
VersionResult, Entry, AsyncCache, KeyManager, VersionResult, Entry, AsyncCache, KeyManager,
session, GetVersionError, HTTPError session, GetVersionError,
) )
GO_PKG_URL = 'https://pkg.go.dev/{pkg}?tab=versions' GO_PKG_URL = 'https://pkg.go.dev/{pkg}?tab=versions'
async def get_version( async def get_version(
name: str, conf: Entry, *, name: str, conf: Entry, *,
cache: AsyncCache, keymanager: KeyManager, cache: AsyncCache, keymanager: KeyManager,
**kwargs, **kwargs,
) -> VersionResult: ) -> VersionResult:
key = tuple(sorted(conf.items())) key = tuple(sorted(conf.items()))
return await cache.get(key, get_version_impl) return await cache.get(key, get_version_impl)
async def get_version_impl(info) -> VersionResult: async def get_version_impl(info) -> VersionResult:
conf = dict(info) conf = dict(info)
pkg_name = conf.get('go') pkg_name = conf.get('go')
url = GO_PKG_URL.format(pkg=pkg_name) url = GO_PKG_URL.format(pkg=pkg_name)
res = await session.get(url) res = await session.get(url)
doc = html.fromstring(res.body.decode()) doc = html.fromstring(res.body.decode())
elements = doc.xpath("//div[@class='Version-tag']/a/text()") elements = doc.xpath("//div[@class='Version-tag']/a/text()")
try: try:
version = elements[0] version = elements[0]
return version return version
except IndexError: except IndexError:
raise GetVersionError("parse error", pkg_name=pkg_name) raise GetVersionError("parse error", pkg_name=pkg_name)

View file

@ -5,32 +5,32 @@ import pytest
from nvchecker.api import HTTPError from nvchecker.api import HTTPError
lxml_available = True
try: try:
import lxml import lxml
lxml_available = True
except ImportError: except ImportError:
lxml_available = False lxml_available = False
pytestmark = [ pytestmark = [
pytest.mark.asyncio(scope="session"), pytest.mark.asyncio(scope="session"),
pytest.mark.needs_net, pytest.mark.needs_net,
pytest.mark.skipif(not lxml_available, reason="needs lxml") pytest.mark.skipif(not lxml_available, reason="needs lxml")
] ]
async def test_go(get_version): async def test_go(get_version):
assert await get_version("one version", { assert await get_version("one version", {
"source": "go", "source": "go",
"go": "github.com/caddyserver/replace-response", "go": "github.com/caddyserver/replace-response",
}) == "v0.0.0-20231221003037-a85d4ddc11d6" }) == "v0.0.0-20231221003037-a85d4ddc11d6"
assert await get_version("multiple version", { assert await get_version("multiple version", {
"source": "go", "source": "go",
"go": "github.com/corazawaf/coraza-caddy", "go": "github.com/corazawaf/coraza-caddy",
}) == "v1.2.2" }) == "v1.2.2"
with pytest.raises(HTTPError): with pytest.raises(HTTPError):
await get_version("not found", { await get_version("not found", {
"source": "go", "source": "go",
"go": "github.com/asdas/sadfasdf", "go": "github.com/asdas/sadfasdf",
}) })