knock/tests/tests.py

65 lines
1.5 KiB
Python
Raw Normal View History

2022-06-06 01:20:38 +00:00
#!/usr/bin/env python3
2022-06-05 20:21:12 +00:00
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from pathlib import Path
import requests, sys, subprocess, shutil, os
2022-06-06 01:20:38 +00:00
knock = Path("./result/bin/knock")
if not knock.exists():
print("error: " + str(knock) + " does not exist", file=sys.stderr)
sys.exit()
if len(sys.argv) != 2:
print(
"error: missing required argument: directory in which to perform the tests",
file=sys.stderr
)
sys.exit()
print("Testing " + str(knock))
2022-06-05 20:21:12 +00:00
2022-06-07 02:19:34 +00:00
result = subprocess.run(knock)
if result.returncode != 0:
print("Test failed: knock failed to describe itself")
sys.exit()
print("---")
workspace = Path(sys.argv[1])
2022-06-05 20:21:12 +00:00
if workspace.exists():
shutil.rmtree(workspace)
workspace.mkdir()
html = requests \
.get("https://www.adobe.com/solutions/ebook/digital-editions/sample-ebook-library.html") \
.text
soup = BeautifulSoup(html, 'html.parser')
links = []
for a_tag in soup.find_all('a'):
if a_tag.string != "Download eBook":
continue
if not urlparse(a_tag.get("href")).path.endswith(".acsm"):
continue
links.append(a_tag.get("href"))
for i, link in enumerate(links):
i = str(i)
print("Testing URL #" + i + ":\n" + link)
file = workspace.joinpath(i + ".acsm")
r = requests.get(link)
open(file, "wb").write(r.content)
2022-06-06 01:20:38 +00:00
result = subprocess.run([knock, file])
2022-06-05 20:21:12 +00:00
if result.returncode != 0:
2022-06-07 02:19:34 +00:00
print("Test failed: knock failed to convert a file")
2022-06-05 20:21:12 +00:00
sys.exit()
print("Success\n---")
print("All tests passed")