Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/nixguix/tests/test_lister.py
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | def page_response(datadir, instance: str = "success") -> List[Dict]: | ||||
return json.loads(datapath.read_text()) if datapath.exists else [] | return json.loads(datapath.read_text()) if datapath.exists else [] | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"name,expected_result", | "name,expected_result", | ||||
[(f"one.{ext}", True) for ext in TARBALL_EXTENSIONS] | [(f"one.{ext}", True) for ext in TARBALL_EXTENSIONS] | ||||
+ [(f"one.{ext}?foo=bar", True) for ext in TARBALL_EXTENSIONS] | + [(f"one.{ext}?foo=bar", True) for ext in TARBALL_EXTENSIONS] | ||||
+ [(f"one?p0=1&foo=bar.{ext}", True) for ext in DEFAULT_EXTENSIONS_TO_IGNORE] | + [(f"one?p0=1&foo=bar.{ext}", True) for ext in DEFAULT_EXTENSIONS_TO_IGNORE] | ||||
+ [("two?file=something.el", False), ("foo?two=two&three=three", False)], | + [ | ||||
("two?file=something.el", False), | |||||
("foo?two=two&three=three", False), | |||||
("v1.2.3", False), # with raise_when_no_extension is False | |||||
("2048-game-20151026.1233", False), | |||||
("v2048-game-20151026.1233", False), | |||||
], | |||||
) | ) | ||||
def test_url_endswith(name, expected_result): | def test_url_endswith(name, expected_result): | ||||
"""It should detect whether url or query params of the urls ends with extensions""" | """It should detect whether url or query params of the urls ends with extensions""" | ||||
urlparsed = urlparse(f"https://example.org/{name}") | urlparsed = urlparse(f"https://example.org/{name}") | ||||
assert ( | assert ( | ||||
url_endswith( | url_endswith( | ||||
urlparsed, | urlparsed, | ||||
TARBALL_EXTENSIONS + DEFAULT_EXTENSIONS_TO_IGNORE, | TARBALL_EXTENSIONS + DEFAULT_EXTENSIONS_TO_IGNORE, | ||||
raise_when_no_extension=False, | raise_when_no_extension=False, | ||||
) | ) | ||||
is expected_result | is expected_result | ||||
) | ) | ||||
def test_url_endswith_raise(): | @pytest.mark.parametrize( | ||||
"name", ["foo?two=two&three=three", "tar.gz/0.1.5", "tar.gz/v10.3.1"] | |||||
) | |||||
def test_url_endswith_raise(name): | |||||
"""It should raise when the tested url has no extension""" | """It should raise when the tested url has no extension""" | ||||
urlparsed = urlparse("https://example.org/foo?two=two&three=three") | urlparsed = urlparse(f"https://example.org/{name}") | ||||
with pytest.raises(ArtifactWithoutExtension): | with pytest.raises(ArtifactWithoutExtension): | ||||
url_endswith(urlparsed, ["unimportant"]) | url_endswith(urlparsed, ["unimportant"]) | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"tarballs", | "tarballs", | ||||
[[f"one.{ext}", f"two.{ext}"] for ext in TARBALL_EXTENSIONS] | [[f"one.{ext}", f"two.{ext}"] for ext in TARBALL_EXTENSIONS] | ||||
+ [[f"one.{ext}?foo=bar"] for ext in TARBALL_EXTENSIONS], | + [[f"one.{ext}?foo=bar"] for ext in TARBALL_EXTENSIONS], | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | requests_mock.head( | ||||
headers={"Content-Type": "application/gzip; charset=ISO-8859-1"}, | headers={"Content-Type": "application/gzip; charset=ISO-8859-1"}, | ||||
) | ) | ||||
requests_mock.head( | requests_mock.head( | ||||
"https://crates.io/api/v1/crates/syntect/4.6.0/download", | "https://crates.io/api/v1/crates/syntect/4.6.0/download", | ||||
headers={ | headers={ | ||||
"Location": "https://static.crates.io/crates/syntect/syntect-4.6.0.crate" | "Location": "https://static.crates.io/crates/syntect/syntect-4.6.0.crate" | ||||
}, | }, | ||||
) | ) | ||||
requests_mock.head( | |||||
"https://codeload.github.com/fifengine/fifechan/tar.gz/0.1.5", | |||||
headers={ | |||||
"Content-Type": "application/x-gzip", | |||||
}, | |||||
) | |||||
expected_visit_types = defaultdict(int) | expected_visit_types = defaultdict(int) | ||||
# origin upstream is added as origin | # origin upstream is added as origin | ||||
expected_nb_origins = 1 | expected_nb_origins = 1 | ||||
expected_visit_types["git"] += 1 | expected_visit_types["git"] += 1 | ||||
for artifact in response["sources"]: | for artifact in response["sources"]: | ||||
# Each artifact is considered an origin (even "url" artifacts with mirror urls) | # Each artifact is considered an origin (even "url" artifacts with mirror urls) | ||||
expected_nb_origins += 1 | expected_nb_origins += 1 | ||||
artifact_type = artifact["type"] | artifact_type = artifact["type"] | ||||
if artifact_type in [ | if artifact_type in [ | ||||
"git", | "git", | ||||
"svn", | "svn", | ||||
"hg", | "hg", | ||||
]: | ]: | ||||
expected_visit_types[artifact_type] += 1 | expected_visit_types[artifact_type] += 1 | ||||
elif artifact_type == "url": | elif artifact_type == "url": | ||||
url = artifact["urls"][0] | url = artifact["urls"][0] | ||||
if url.endswith(".git"): | if url.endswith(".git"): | ||||
expected_visit_types["git"] += 1 | expected_visit_types["git"] += 1 | ||||
elif url.endswith(".c") or url.endswith(".txt"): | elif url.endswith(".c") or url.endswith(".txt"): | ||||
expected_visit_types["content"] += 1 | expected_visit_types["content"] += 1 | ||||
elif url.startswith("svn"): # mistyped artifact rendered as vcs nonetheless | elif url.startswith("svn"): # mistyped artifact rendered as vcs nonetheless | ||||
expected_visit_types["svn"] += 1 | expected_visit_types["svn"] += 1 | ||||
elif "crates.io" in url: | elif "crates.io" in url or "codeload.github.com" in url: | ||||
expected_visit_types["directory"] += 1 | expected_visit_types["directory"] += 1 | ||||
else: # tarball artifacts | else: # tarball artifacts | ||||
expected_visit_types["directory"] += 1 | expected_visit_types["directory"] += 1 | ||||
assert set(expected_visit_types.keys()) == { | assert set(expected_visit_types.keys()) == { | ||||
"content", | "content", | ||||
"git", | "git", | ||||
"svn", | "svn", | ||||
▲ Show 20 Lines • Show All 94 Lines • Show Last 20 Lines |