本文介绍了python脚本内部发生故障时,管道不会失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以-我正在编写一个脚本,用于根据项目中的更改运行iOS测试(可能对主题不重要)。



在脚本中,有一个命令可以运行测试:



cmd = xcodebuild -workspace xxx.xcworkspace -scheme xxx -destination'platform = iOS Simulator,name = {0},OS = latest'-configuration Debug -derivedDataPath {1} test-without-building {2} -parallel-启用测试功能的否-enableCodeCoverage是| xcpretty .format(os.environ ['TEST_SIMULATOR_NAME'],os.environ ['PWD'],结果)



并按以下方式执行:



do(cmd)



do()方法定义为(

解决方案

好,我发现唯一可行的解​​决方案是将带有命令的字符串发送回gitlab的shell并在那里执行。
像这样:



在Python中:

  cmd = 
xcodebuild
-workspace xxx.xcworkspace
-scheme xxx
-destination'platform = iOS Simulator,name = {0},OS =最新的
-配置调试
-derivedDataPath {1}测试而无构建{2}
-启用并行测试否
-enableCodeCoverage是| xcpretty
.format(os.environ ['TEST_SIMULATOR_NAME'],os.environ ['PWD'],结果)

print(cmd)

YAML:

  manualUiTestsBasedOnChanges: 
阶段:uiTests仅
:...某些条件...
before_script:
-设置-o pipefail
脚本:
-result =` ../ scripts / ci / run_UI_tests_based_on_changes.py`
-评估 $ result


So - I am writing a script for running iOS tests based on changes in project (not important to topic probably).

Within the script, there is a command, which will run the tests:

cmd = "xcodebuild -workspace xxx.xcworkspace -scheme xxx -destination 'platform=iOS Simulator,name={0},OS=latest' -configuration Debug -derivedDataPath {1} test-without-building {2} -parallel-testing-enabled NO -enableCodeCoverage YES | xcpretty".format(os.environ['TEST_SIMULATOR_NAME'], os.environ['PWD'], result)

and is executed like this:

do(cmd)

The do() method definition is (source):

def do(command):
  return_code = call([ '/bin/bash', '-c', 'set -o pipefail; ' + command ])

Gitlab job settings:

manualUiTestsBasedOnChanges:
  stage: uiTests
  only: ...some conditions...
  before_script:
    - set -o pipefail
  script:
    - ../scripts/ci/run_UI_tests_based_on_changes.py

The problem with this is, that if a fail occurs within this script, it won't fail the job, even when set -o pipefail is set in before script AND in in the do() method. Visible on image below.

Any ideas why it's behaving like this?

解决方案

Ok, the only working solution, I found is to send the string with command back to gitlab's shell and execute it there.Like this:

In Python:

cmd = "
xcodebuild
   -workspace xxx.xcworkspace
   -scheme xxx
   -destination 'platform=iOS Simulator,name={0},OS=latest'
   -configuration Debug
   -derivedDataPath {1} test-without-building {2}
   -parallel-testing-enabled NO
   -enableCodeCoverage YES | xcpretty"
.format(os.environ['TEST_SIMULATOR_NAME'], os.environ['PWD'], result)

print(cmd)

YAML:

manualUiTestsBasedOnChanges:
  stage: uiTests
  only: ...some conditions...
  before_script:
    - set -o pipefail
  script:
    - result=`../scripts/ci/run_UI_tests_based_on_changes.py`
    - eval "$result"

这篇关于python脚本内部发生故障时,管道不会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:52