1#!/usr/bin/python 2# Copyright (c) 2023 Nordic Semiconductor ASA 3# 4# SPDX-License-Identifier: Apache-2.0 5 6""" 7Simply mock for bash script to use with unit tests. 8""" 9import sys 10import time 11from argparse import ArgumentParser 12 13from zen_of_python import zen_of_python 14 15 16def main() -> int: 17 parser = ArgumentParser() 18 parser.add_argument('--sleep', action='store', default=0, type=float) 19 parser.add_argument('--long-sleep', action='store_true') 20 parser.add_argument('--return-code', action='store', default=0, type=int) 21 parser.add_argument('--exception', action='store_true') 22 23 args = parser.parse_args() 24 25 if args.exception: 26 # simulate crashing application 27 raise Exception 28 29 if args.long_sleep: 30 # prints data and wait for certain time 31 for line in zen_of_python: 32 print(line, flush=True) 33 time.sleep(args.sleep) 34 else: 35 # prints lines with delay 36 for line in zen_of_python: 37 print(line, flush=True) 38 time.sleep(args.sleep) 39 40 print('End of script', flush=True) 41 print('Returns with code', args.return_code, flush=True) 42 time.sleep(1) # give a moment for external programs to collect all outputs 43 return args.return_code 44 45 46if __name__ == '__main__': 47 sys.exit(main()) 48