2012-11-22 36 views
1

我們有一個包含多個Python包(*)的大型倉庫。我希望scons在每個子目錄中運行py.test,而不是從項目根目錄運行。這證明相當令人沮喪。目前,我有檢查刪除了所有的錯誤這個動作:Scons在不同的子目錄中運行py.test

def runTests (target = None, source = None, env = None): 
    cmd = which(env['TEST_RUNNER']) 
    if cmd: 
     retCode = True 
     for path in env['TEST_DIR_LIST']: 
      print 'Doing directory %s:' % (path) 
      retCode = retCode and subprocess.call([cmd], cwd=path) 
     env.Exit(retCode) 

我稱之爲作爲SConstruct文件:

runTestsCmd = env.Command('runTests', None, Action(runTests, "Running tests")) 
AlwaysBuild(runTestsCmd) 
Alias('test', runTestsCmd) 

而且在每個SConscript文件,我有這樣的:

env.Append(TEST_DIR_LIST = ['PackageDirectory']) 

我得到的是隻運行py.test的一個實例。我可以看到「執行目錄X」消息,但沒有運行py.test。

顯然,需要不要在SConscript中克隆環境,或者如果克隆了env,請確保在原始env上添加TEST_DIR_LIST。

所以,我的問題是雙重的:

  1. 這是做什麼,我想一種合適的方式?
  2. 如果是,我在做什麼錯?如果不是,我應該做什麼

(*)是的,我們正在考慮改變這種情況。不,它不會很快發生,所以我確實需要上述。

回答

1

的問題是線:

retCode = retCode and subprocess.call([cmd], cwd=path) 

subprocess.call返回0(其計算爲False)上的成功。你需要插入一個not或者做這樣的適當檢查:

retcode = subprocess.call([cmd], cwd=path) 
if retcode != 0: 
    print ("failed ...") 
    break # or not break if you want to continue anyway 
+0

確實。我昨天晚上才意識到。謝謝! – Sardathrion