"""Script test runner."""from__future__importannotationsimportloggingimportsubprocessimportsysfromsubprocessimportCalledProcessErrorfromtypingimportTYPE_CHECKING,Anyfrom..._loggingimportPrefixAdaptorfrom...tests.handlers.baseimportTestHandlerifTYPE_CHECKING:from...config.components.runway.baseimportConfigPropertyTYPE_NAME="script"LOGGER=logging.getLogger(__name__)
[docs]classScriptHandler(TestHandler):"""Handle script tests. Args: commands: A list of commands to be executed in order. Each command is run in its own subprocess. The working directory will be the same as where the 'runway test' command was executed. .. rubric:: Example .. code-block: yaml tests: - name: example-test type: script args: commands: - echo "this is an example" - pwd """
[docs]@classmethoddefhandle(cls,name:str,args:ConfigProperty|dict[str,Any])->None:"""Perform the actual test."""logger=PrefixAdaptor(name,LOGGER)forcmdinargs["commands"]:try:exit_code=subprocess.call(cmd,shell=True)ifexit_code!=0:raiseValueError(exit_code)exceptCalledProcessErroraserr:logger.error("failed to execute command: %s",cmd)raiseerrexceptValueError:logger.error("failed command: %s",cmd)sys.exit(1)