我正在为一个遗留的 Python 脚本编写一个功能测试,这样我就可以对其进行一行更改,而不会因恐惧而瘫痪。 ;)
有问题的脚本使用 wget(1) 调用子进程.Popen下载一个 XML 文件,然后对其进行解析:
def download_files():
os.mkdir(FEED_DIR)
os.chdir(FEED_DIR)
wget_process = Popen(
["wget", "--quiet", "--output-document", "-", "ftp://foo.com/bar.tar"],
stdout=PIPE
)
tar_process = Popen(["tar", "xf", "-"], stdin=wget_process.stdout)
stdout, stderr = tar_process.communicate()
显然,最好修改脚本以使用 HTTP 库而不是 exec-ing wget,但正如我所说,这是一个遗留脚本,因此我需要保持最小的更改并绝对关注业务需求,这与如何获取 XML 文件无关。
对我来说显而易见的解决方案是拦截对子进程.Popen并返回我自己的测试 XML。拦截Python中的方法调用 https://stackoverflow.com/questions/2704434/intercept-method-calls-in-python演示如何使用setattr要做到这一点,但我一定错过了一些东西:
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> object.__getattribute__(subprocess, 'Popen')
<class 'subprocess.Popen'>
>>> attr = object.__getattribute__(subprocess, 'Popen')
>>> hasattr(attr, '__call__')
True
>>> def foo(): print('foo')
...
>>> foo
<function foo at 0x7f8e3ced3c08>
>>> foo()
foo
>>> setattr(subprocess, '__call__', foo)
>>> getattr(subprocess, '__call__')
<function foo at 0x7f8e3ced3c08>
>>> subprocess.Popen([ r"tail", "-n 1", "x.txt" ], stdout = subprocess.PIPE)
<subprocess.Popen object at 0x7f8e3ced9cd0>
>>> tail: cannot open `x.txt' for reading: No such file or directory
正如你所看到的,真正的子进程.Popen尽管属性设置正确(至少在我很大程度上未经训练的眼睛看来),但仍被调用。这只是在交互式 Python 中运行它的结果吗,还是我应该期望将此类代码放入我的测试脚本中得到相同的结果:
class MockProcess:
def __init__(self, output):
self.output = output
def stderr(): pass
def stdout(): return self.output
def communicate():
return stdout, stderr
# Runs script, returning output
#
def run_agent():
real_popen = getattr(subprocess.Popen, '__call__')
try:
setattr(subprocess.Popen, '__call__', lambda *ignored: MockProcess('<foo bar="baz" />')
)
return real_popen(['myscript.py'], stdout = subprocess.PIPE).communicate()[0]
finally:
setattr(subprocess.Popen, '__call__', real_popen)