带有pytest和版本控制的Exercism.io自动化

时间:2018-07-03 01:37:51

标签: python bash git continuous-integration pytest

CI新手在这里。我目前正在Exercism.io的python轨道上工作。我正在寻找一种方法来自动化从pytest运行测试,提交并推送到github的过程,并在所有测试均通过的情况下最终屈服于执行。我已经实现了pre-commit挂钩,以在提交时调用测试,但是我不确定如何将文件名传回exercism进行提交。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我为寻找类似工作流程的其他人提供了一个解决方案。它在根目录中使用python脚本,并通过子进程处理git命令。它还支持文件目录的自动完成(基于this gist)。该脚本假定您已经初始化了git repo并设置了远程repo。

#!/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"])