| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# @HEADER |
| 4 |
# ***************************************************************************** |
| 5 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 6 |
# |
| 7 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 8 |
# SPDX-License-Identifier: BSD-3-Clause |
| 9 |
# ***************************************************************************** |
| 10 |
# @HEADER |
| 11 |
|
| 12 |
# |
| 13 |
# Usage: mockprogram.py [any arguments] |
| 14 |
# |
| 15 |
# Mock program that takes input arguments and produces stdout by reading from |
| 16 |
# a file .mockprogram_inout.txt in the current directory or the file specified |
| 17 |
# by the env var MOCKPROGRAM_INOUT_FILE_OVERRIDE (which can be in any |
| 18 |
# directory). This script is used to take the place of real commands during a |
| 19 |
# test that involves calling commands on the commandline. |
| 20 |
# |
| 21 |
# The file .mockprogram_inout.txt (or pointed to by |
| 22 |
# MOCKPROGRAM_INOUT_FILE_OVERRIDE) is of the form: |
| 23 |
# |
| 24 |
# MOCK_PROGRAM_INPUT: <args_1> |
| 25 |
# MOCK_PROGRAM_RETURN: <rtn> |
| 26 |
# MOCK_PROGRAM_OUTPUT: <outline_1_line_1> |
| 27 |
# <outline_1_line_2> |
| 28 |
# ... |
| 29 |
# MOCK_PROGRAM_INPUT: <args_2> |
| 30 |
# |
| 31 |
# The program reads in the blocks starting at the time and removes the block |
| 32 |
# from the file after it runs. After all of the blocks are read in, if run |
| 33 |
# again it will error out with error code 2. |
| 34 |
# |
| 35 |
# This program can be used, for example, to simulate git command. For |
| 36 |
# example, a couple of git commits might be simulated like: |
| 37 |
# |
| 38 |
# MOCK_PROGRAM_INPUT: log -1 |
| 39 |
# MOCK_PROGRAM_RETURN: 0 |
| 40 |
# MOCK_PROGRAM_OUTPUT: This is the summary line |
| 41 |
# |
| 42 |
# The is the body of the commit msg |
| 43 |
# MOCK_PROGRAM_INPUT: diff --name-only HEAD --not @{u} |
| 44 |
# MOCK_PROGRAM_RETURN: 0 |
| 45 |
# MOCK_PROGRAM_OUTPUT: file_name_1.txt |
| 46 |
# file_name_2.txt |
| 47 |
# file_name_3.txt |
| 48 |
|
| 49 |
# |
| 50 |
|
| 51 |
import sys |
| 52 |
import os |
| 53 |
|
| 54 |
inputArgs = ' '.join(sys.argv[1:]) |
| 55 |
#print("inputArgs = '" + inputArgs + "'" |
| 56 |
|
| 57 |
if os.environ.get("MOCKPROGRAM_INOUT_FILE_OVERRIDE"): |
| 58 |
mockProgramInOutFilePath=os.environ.get("MOCKPROGRAM_INOUT_FILE_OVERRIDE") |
| 59 |
else: |
| 60 |
mockProgramInOutFilePath='.mockprogram_inout.txt' |
| 61 |
|
| 62 |
if not os.path.exists(mockProgramInOutFilePath): |
| 63 |
print("Error: "+mockProgramInOutFilePath+" is missing!") |
| 64 |
sys.exit(1) |
| 65 |
|
| 66 |
mockprogramInout = open(mockProgramInOutFilePath, 'r').read() |
| 67 |
mockprogramInoutArray = mockprogramInout.splitlines() |
| 68 |
if len(mockprogramInoutArray) and mockprogramInoutArray[-1] == "": |
| 69 |
mockprogramInoutArray = mockprogramInoutArray[:-1] |
| 70 |
|
| 71 |
if len(mockprogramInoutArray) < 3: |
| 72 |
print("Error: "+mockProgramInOutFilePath+" has less than three lines:\n" |
| 73 |
"-------------\n" + mockprogramInout + "-------------") |
| 74 |
sys.exit(2) |
| 75 |
|
| 76 |
# Assert input |
| 77 |
expectedInputLine = mockprogramInoutArray[0] |
| 78 |
if expectedInputLine.find("MOCK_PROGRAM_INPUT:") != 0: |
| 79 |
print("Error, first line = '" + expectedInputLine + "', does not match " |
| 80 |
"^MOCK_PROGRAM_INPUT:") |
| 81 |
sys.exit(3) |
| 82 |
expectedInput = expectedInputLine.replace("MOCK_PROGRAM_INPUT:", "").strip() |
| 83 |
if inputArgs != expectedInput: |
| 84 |
print("Error, input args='" + inputArgs + "' does not match expected='" + |
| 85 |
expectedInput + "'") |
| 86 |
sys.exit(4) |
| 87 |
|
| 88 |
# Get return code |
| 89 |
returnCodeLine = mockprogramInoutArray[1] |
| 90 |
if returnCodeLine.find("MOCK_PROGRAM_RETURN:") != 0: |
| 91 |
print("Error, second line = '" + returnCodeLine + "', does not match " |
| 92 |
"^MOCK_PROGRAM_RETURN:") |
| 93 |
sys.exit(5) |
| 94 |
returnCode = returnCodeLine.replace("MOCK_PROGRAM_RETURN:", "").strip() |
| 95 |
|
| 96 |
# Get output (can be multi-line) |
| 97 |
outputLine = mockprogramInoutArray[2] |
| 98 |
if outputLine.find("MOCK_PROGRAM_OUTPUT:") != 0: |
| 99 |
print("Error, third line = '" + outputLine + "', does not match " |
| 100 |
"^MOCK_PROGRAM_OUTPUT:") |
| 101 |
sys.exit(6) |
| 102 |
outputStr = outputLine.replace("MOCK_PROGRAM_OUTPUT: ", "") |
| 103 |
numLinesOuput = 1 |
| 104 |
if len(mockprogramInoutArray) > 3: |
| 105 |
for line in mockprogramInoutArray[3:]: |
| 106 |
if line.find("MOCK_PROGRAM_INPUT:") == 0: |
| 107 |
break |
| 108 |
outputStr = outputStr+"\n"+line |
| 109 |
numLinesOuput = numLinesOuput + 1 |
| 110 |
print(outputStr) |
| 111 |
|
| 112 |
# Write the remaining lines back into the file |
| 113 |
lineLineIndex = 2 + numLinesOuput |
| 114 |
if len(mockprogramInoutArray) > lineLineIndex: |
| 115 |
open(mockProgramInOutFilePath, 'w').write( |
| 116 |
('\n'.join(mockprogramInoutArray[lineLineIndex:]))+"\n" ) |
| 117 |
else: |
| 118 |
open(mockProgramInOutFilePath, 'w').write("") |
| 119 |
|
| 120 |
# Return exit code |
| 121 |
sys.exit(int(returnCode)) |