Python 如何运行WAF编译的C++;测试脚本中的程序?
尊敬的WAF构建系统专家: 让我们假设您使用WAF构建系统来构建一个库Python 如何运行WAF编译的C++;测试脚本中的程序?,python,c++,testing,waf,Python,C++,Testing,Waf,尊敬的WAF构建系统专家: 让我们假设您使用WAF构建系统来构建一个库doubib和一个程序fooProg。然后,您希望通过检查fooProg输出的Python脚本fooProgTest来检查程序fooProg 以下是傻瓜ib和fooProg的最低示例: $ cat fooLib/fooLib.cpp int foo() { return 42; } 我的问题如下: 你们中有人知道如何避免手动设置LD_LIBRARY_PATH吗 如何避免通过环境变量“FOO_EXE”设置fooPr
doubib
和一个程序fooProg
。然后,您希望通过检查fooProg
输出的Python脚本fooProgTest
来检查程序fooProg
以下是傻瓜ib
和fooProg
的最低示例:
$ cat fooLib/fooLib.cpp
int foo()
{
return 42;
}
我的问题如下:
- 你们中有人知道如何避免手动设置LD_LIBRARY_PATH吗
- 如何避免通过环境变量“FOO_EXE”设置
的路径fooProg
dougib.so
与fooProg
位于同一目录中,对wscript进行以下更改就足够了:
bld(features=“cxx cxxprogram”,
target=“fooProg/fooProg”,
source=“fooProg/fooProg.cpp”,
使用=“傻瓜”,
rpath=“$ORIGIN”)
这使得LD在搜索共享对象时也要考虑存储可执行文件的目录
如何避免通过环境变量“FOO_EXE”设置fooProg的路径
使用子流程。检查输出
可以传递多个参数。即
subprocess.check\u输出([
“您的\u可执行文件\u到\u启动”,
“arg1”,
“arg2”
])
在测试脚本中,必须使用sys.argv
或argparse
读取参数
额外的: 启动解释器来启动您的应用程序似乎有点老套。相反,定义一个自定义任务(实现
waflib.task.task
),然后运行子流程。检查\u输出
1
1 AFAIK waf为您提供了启动流程的便捷方法,尽管我记不起它的名称
$ cat fooProg/fooProg.cpp
#include <iostream>
extern int foo();
int main()
{
std::cout << foo() << std::endl;
return 0;
}
import os
from waflib.Tools import waf_unit_test
def options(opt):
opt.load("compiler_cxx waf_unit_test python")
def configure(cnf):
cnf.load("compiler_cxx waf_unit_test python")
def build(bld):
bld.add_post_fun(waf_unit_test.summary)
bld.options.clear_failed_tests= True
bld(features= "cxx cxxshlib",
target= "fooLib",
source= "fooLib/fooLib.cpp")
bld(features= "cxx cxxprogram",
target= "fooProg/fooProg",
source= "fooProg/fooProg.cpp",
use= "fooLib")
testEnv= os.environ.copy()
testEnv["FOO_EXE"]= bld.path.find_or_declare("fooProg/fooProg").abspath()
bld(features= "test_scripts",
test_scripts_source= "fooProgTest/fooProgTest.py",
test_scripts_template= "${PYTHON} ${SRC[0].abspath()}",
test_scripts_paths= {
"LD_LIBRARY_PATH": bld.bldnode.abspath()
},
test_scripts_env= testEnv
)
cat fooProgTest/fooProgTest.py
#!/usr/bin/env python
import os
import subprocess
assert subprocess.check_output("{}".format(
os.environ["FOO_EXE"])).startswith("42")