演习.iopytest和version con的自动化

2024-04-26 14:59:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的。我目前正在为Exercism.io开发python。我正在寻找一种方法来自动化从pytest运行测试的过程,提交并推送到github,最后在所有测试都通过的情况下提交到exercism。我已经实现了一个预提交钩子来调用提交时的测试,但是我不知道如何将文件名传递回exercism进行提交。非常感谢您的帮助!你知道吗


Tags: 方法iogithubpytest文件名过程情况钩子
1条回答
网友
1楼 · 发布于 2024-04-26 14:59:54

我为其他寻找类似工作流的人提供了一个解决方案。它利用根目录中的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"])

相关问题 更多 >