通过 pytest 和版本控制实现 Exercism.io 自动化



CI新手在这里。我目前正在研究 Python 轨道 Exercism.io。我正在寻找一种方法来自动化从pytest运行测试,提交并推送到github的过程,如果所有测试都通过,则最终提交到exercism。我已经实现了一个预提交钩子来调用提交时的测试,但我不确定如何将文件名传回 exercism 进行提交。任何帮助将不胜感激!

我为其他寻找类似工作流程的人提供了一个解决方案。它利用根目录中的 python 脚本,并通过子进程处理 git 命令。它还支持文件目录的自动完成(基于此要点(。此脚本假定你已初始化 git 存储库并设置远程存储库。

#!/usr/bin/env python
import pytest
import subprocess
import readline
import glob
class tabCompleter(object):
""" 
A tab completer that can complete filepaths from the filesystem.
Partially taken from:
http://stackoverflow.com/questions/5637124/tab-completion-in-pythons-raw-input
"""
def pathCompleter(self,text,state):
""" 
This is the tab completer for systems paths.
Only tested on *nix systems
"""
return [x for x in glob.glob(text+'*')][state]
if __name__=="__main__":
# Take user input for commit message
commit_msg = raw_input('Enter the commit message: ')
t = tabCompleter()
readline.set_completer_delims('t')
# Necessary for MacOS, Linux uses tab: complete syntax
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
#Use the new pathCompleter
readline.set_completer(t.pathCompleter)
#Take user input for exercise file for submission
ans = raw_input("Select the file to submit to Exercism: ")
if pytest.main([]) == 0:
"""
If all tests pass: 
add files to index,
commit locally,
submit to exercism.io,
then finally push to remote repo.
"""
subprocess.call(["git","add","."])
subprocess.call(["git","commit","-m", commit_msg])
subprocess.call(["exercism","submit",ans])
subprocess.call(["git","push","origin","master"])

最新更新