Verified Commit b52b394a authored by w-mj's avatar w-mj

本地测试

parent c2b59f27
......@@ -9,4 +9,8 @@ temp
__pycache__
.idea
\ No newline at end of file
.idea
pygrading/static/copy_files.txt
test/cg-kernel
test/env
\ No newline at end of file
......@@ -20,6 +20,7 @@ import pygrading as gg
import shutil
import zipapp
def render_file(pkg_path, target_path, name, **kwargs):
data = pkgutil.get_data(__package__, pkg_path).decode()
template = Template(data)
......@@ -45,7 +46,7 @@ def init(name="cg-kernel"):
if os.path.exists(new_project_path) and len(os.listdir(new_project_path)) > 0:
raise RuntimeError(f"{new_project_path} is already exists.")
render_file_list = ['README.md', 'manage.py', 'docker/Dockerfile']
copy_file_list = pkgutil.get_data(__package__, 'static/copy_files.txt').decode().split('\n')
copy_file_list = pkgutil.get_data(__package__, 'static/copy_files.txt').decode().replace('\r', '').split('\n')
render_data = {
'proj_name': name,
'version': gg.__version__
......@@ -112,6 +113,7 @@ def test():
result_root = os.path.join(os.getcwd(), "test_result")
shutil.rmtree(result_root, ignore_errors=True)
all_cnt, pass_cnt = 0, 0
all_result = {}
for case in testcases:
name = case["name"]
if 'submits' in case:
......@@ -137,9 +139,10 @@ def test():
cmd = ' '.join(["docker", "run", "--rm",
"--mount", f'type=bind,source="{local_testdata_dir}",target="{testdata_dir}",readonly',
"--mount", f'type=bind,source="{local_submit_dir}",target="{submit_dir}"',
"--mount", f'type=bind,source="{local_kernel}",target="{kernel}",readonly',
"--mount", f'type=bind,source="{local_kernel}",target="{kernel}"',
"--entrypoint", entrypoint[0], f"{project_name}:{project_version}",
' '.join(entrypoint[1:])])
# print(cmd)
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf8")
out, err = process.communicate(timeout=timeout)
end = time.time()
......@@ -161,6 +164,7 @@ def test():
write_file(os.path.join(result_dir, "detail.html"), result.get("detail", "None"))
except Exception as e:
summary['error'] = repr(e)
all_result[name + "--" + submit['name']] = summary
with open(os.path.join(result_dir, "summary.json"), "w") as f:
json.dump(summary, f)
print(f"exec time: {exec_time}, error: {summary.get('error')}")
......@@ -174,6 +178,8 @@ def test():
all_cnt += 1
if final_result:
pass_cnt += 1
with open(os.path.join(result_root, 'summary.json'), 'w', encoding='utf-8') as f:
json.dump(all_result, f)
print(f"{pass_cnt}/{all_cnt} TESTS PASSED!")
return pass_cnt == all_cnt
......
.gitignore
copy_files.txt
docker/Dockerfile
kernel/postwork.py
kernel/prework.py
kernel/run.py
kernel/templates/html/base.jinja
kernel/templates/html/index.html
kernel/templates/__init__.py
kernel/__init__.py
kernel/__main__.py
manage.py
README.md
test/a+b/submit/aaa.c
test/a+b/testdata/input1.txt
test/a+b/testdata/input2.txt
test/a+b/testdata/input3.txt
test/a+b/testdata/input4.txt
test/a+b/testdata/output1.txt
test/a+b/testdata/output2.txt
test/a+b/testdata/output3.txt
test/a+b/testdata/output4.txt
test/gcd/submits/correct/gcd.c
test/gcd/submits/wrong/ce.c
test/gcd/testdata/input1.txt
test/gcd/testdata/output1.txt
test/hello world/submit/submit.c
test/hello world/testdata/input1.txt
test/hello world/testdata/output1.txt
test.json
......@@ -15,6 +15,14 @@ from pygrading import __version__
with open('README.md', 'r', encoding='utf-8') as fh:
long_description = fh.read()
import os
file_list = ([os.path.join(path, file) for file in file_list] for path, dir_list, file_list in os.walk("pygrading/static"))
file_list = [x.replace("pygrading/", "").replace('\\', '/') for x in sum(file_list, [])]
with open("pygrading/static/copy_files.txt", "w", encoding="utf-8") as f:
f.write('\n'.join(x[len("static/"):] for x in file_list))
file_list.append("copy_files.txt")
setuptools.setup(
name='pygrading',
version=__version__,
......@@ -26,15 +34,7 @@ setuptools.setup(
url='https://gitlab.educg.net/zhangmingyuan/PyGrading',
packages=['pygrading', 'cg_learning'],
package_data={
'pygrading': [
'static/.gitignore',
'static/*',
'static/test/**'
'static/kernel/*',
'static/kernel/templates/*',
'static/kernel/templates/html/*',
'static/docker/*'
]
'pygrading': file_list
},
classifiers=[
'Programming Language :: Python :: 3',
......
import json
import time
import unittest
import shutil
import os
class ProjectTest(unittest.TestCase):
def test_all(self):
# 构建
shutil.rmtree("../build", ignore_errors=True)
shutil.rmtree("../dist", ignore_errors=True)
shutil.rmtree("../pygrading.egg-info", ignore_errors=True)
self.assertEqual(os.path.exists("../build"), False)
self.assertEqual(os.path.exists("../dist"), False)
self.assertEqual(os.path.exists("pygrading.edd-info"), False)
os.system("cd .. && python setup.py sdist bdist_wheel")
whls = [x for x in os.listdir("../dist") if x[-4:] == ".whl"]
self.assertEqual(len(whls), 1)
whl_path = os.path.join(os.getcwd(), "../dist", whls[0])
# 创建虚拟环境
os.system("python -m venv --clear env")
python = os.path.join("env", "Scripts", "python")
os.system(f"{python} -m pip install {whl_path}")
# 开始测试
shutil.rmtree("cg-kernel", ignore_errors=True)
os.system(f"{python} -m pygrading init")
shutil.copyfile(whl_path, f"cg-kernel/docker/{whls[0]}")
os.system(f"{python} cg-kernel/manage.py package")
os.system(f"{python} cg-kernel/manage.py docker-build")
os.system(f"{python} cg-kernel/manage.py test")
self.assertEqual(os.path.exists("cg-kernel/test_result/summary.json"), True)
summary = json.load(open("cg-kernel/test_result/summary.json", encoding='utf-8'))
summary = [v["asserts"] for k, v in summary.items()]
summary = all(x['res'] for x in sum(summary, []))
self.assertEqual(summary, True)
# os.makedirs("docker", exist_ok=True)
# shutil.copyfile(self.whl_path, "docker/pygrading-1.1.8.dev0-py3-none-any.whl")
# os.system("cd docker && docker build --rm -t test_pygrading .")
# time.sleep(1)
# os.system("docker run -it --rm --privileged -v //var/run/docker.sock:/var/run/docker.sock test_pygrading")
if __name__ == '__main__':
unittest.main()
"""
docker run --rm -v "/cg-kernel/test/a+b/testdata:/coursegrader/testdata" -v "/cg-kernel/test/a+b/submit:/coursegrader/submit" -v "/cg-kernel/kernel.pyz:/coursegrader/dockerext/kernel.pyz" --entrypoint python3 cg-kernel:0.1 /coursegrader/dockerext/kernel.pyz
"""
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment