-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_generator.py
67 lines (54 loc) · 2.41 KB
/
test_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import pathlib
import shutil
import time
import pytest
import capturecli
import shlex
@pytest.fixture(autouse = False, scope="function", params=["name"])
def generate(name, tmpdir):
image = f"osdc-test-{str(time.time())}"
token = os.environ.get('MY_GITHUB_TOKEN')
shutil.copytree(os.path.join(os.getcwd(), name), os.path.join(tmpdir, name))
print(f"\n-------- building docker image {image} --------------")
exit_code = os.system(f'docker build -t {image} .')
if exit_code != 0:
raise Exception("Failed to build docker image")
print(f"\n-------- running docker container based on {image} --------------")
cmd = f'docker run --rm -w /data --env MY_GITHUB_TOKEN={token} -v{tmpdir}/{name}:/data {image}'
exit_code, out, err = capturecli.separated(shlex.split(cmd))
print(out)
print(err)
#if exit_code != 0:
# raise Exception("Failed to run the docker")
yield {"exit_code": exit_code, "out": out, "err": err}
print(f"\n--------removing docker image {image}")
exit_code = os.system(f'docker rmi {image}')
if exit_code != 0:
raise Exception("Failed to remove docker image")
@pytest.mark.parametrize("name", ["test1"])
def test_one(name, tmpdir, generate):
print(f"in test: {generate}")
assert generate['exit_code'] == 0
root = pathlib.Path(tmpdir).joinpath(name)
site = root.joinpath('_site')
assert site.exists()
assert site.joinpath('index.html').exists()
pages = site.joinpath('osdc-skeleton')
assert pages.exists()
print(pages.joinpath('about.html'))
with pages.joinpath('about.html').open() as fh:
html = fh.read()
assert '<title>About</title>' in html
with pages.joinpath('articles.html').open() as fh:
html = fh.read()
assert '<a href="https://dev.to/szabgab/open-source-development-courses-5d4b">Open Source Development Courses</a> by <a href="p/szabgab">Gabor Szabo</a><br>' in html
with pages.joinpath('p/szabgab.html').open() as fh:
html = fh.read()
assert 'GitHub page of <a href="https://Szabgab.github.io/">Gabor Szabo</a><br>' in html
@pytest.mark.parametrize("name", ["test2"])
def test_two(name, generate):
print(f"in test: {generate}")
assert generate["exit_code"] == 1
assert "ERROR: Exception: value of github fields 'other-demo' is not the same as the filename 'cm-demo.json'" in generate["err"]
assert "There were 1 errors" in generate["err"]