在过去的几天里,我们正在尝试为我们存储库中的所有分支创建一个包含所有更改的包。这个命令似乎这样做,但会生成一堆我们不想在我们的自动化流程中看到的输出:
% git bundle create /tmp/some_bundle --branches --since=2.days.ago
warning: ref '4.28' is excluded by the rev-list options
warning: ref '4.30' is excluded by the rev-list options
warning: ref '4.36' is excluded by the rev-list options
warning: ref 'run_lcov_refactor' is excluded by the rev-list options
Counting objects: 4745, done.
Delta compression using up to 48 threads.
Compressing objects: 100% (1296/1296), done.
Writing objects: 100% (3536/3536), 1.00 MiB, done.
Total 3536 (delta 3019), reused 2655 (delta 2224)
Run Code Online (Sandbox Code Playgroud)
我认为警告告诉我的是,最近两天命名的分支没有变化,这正是我所期望的。
bundle 命令似乎没有任何选项可以安静或抑制此输出。在 bundle 失败之前添加 …
我正在尝试编写一个单元测试,该测试执行一个写入标准输出的函数,捕获该输出并检查结果。所讨论的函数是一个黑匣子:我们无法更改它写入输出的方式。出于本示例的目的,我对其进行了相当多的简化,但本质上该函数使用 subprocess.call() 生成其输出。
无论我尝试什么,我都无法捕获输出。它总是被写入屏幕,并且测试失败,因为它没有捕获任何内容。我尝试了 print() 和 os.system()。使用 print() 我可以捕获标准输出,但也不能使用 os.system() 。
它也不是特定于单元测试的。我已经编写了我的测试示例,但没有得到相同的结果。
类似的问题已经被问了很多,答案似乎都归结为使用 subprocess.Popen() 和 communications(),但这需要更改黑匣子。我确信有一个我还没有遇到的答案,但我很困惑。
我们使用的是Python-2.7。
无论如何,我的示例代码是这样的:
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.dont_write_bytecode = True
import os
import unittest
import subprocess
from contextlib import contextmanager
from cStringIO import StringIO
# from somwhere import my_function
def my_function(arg):
#print('my_function:', arg)
subprocess.call(['/bin/echo', 'my_function: ', arg], shell=False)
#os.system('echo my_function: ' + arg)
@contextmanager
def redirect_cm(new_stdout):
old_stdout = sys.stdout
sys.stdout = new_stdout
try:
yield
finally:
sys.stdout = old_stdout
class Test_something(unittest.TestCase):
def …Run Code Online (Sandbox Code Playgroud)