#!/opt/homebrew/bin/bash
#------------------------------------------------------------------------------
## Repro for: <ONE-LINE DESCRIPTION OF THE BUG/BEHAVIOR>
set -u
## Usage:
##
## ./repro.sh [-f] 2>&1
##
## Scenario: <SCENARIO TITLE>
##
## <PROSE EXPLANATION>
##
## Explain, in a few paragraphs:
## - The setup that's needed for the bug/behavior to appear (workspace
## state, stream config, prior submits, opened files, etc.).
## - The exact command(s) whose output is "wrong" vs. "right"/expected.
## Quote the actual expected output strings if known, e.g.:
##
## p4 opened --> File(s) not opened on this client. (WRONG)
## p4 opened ... --> correctly lists the still-opened file.
##
## - Why this matters in practice (what real workflow/automation breaks).
##
## The test uses a disposable P4-native DVCS micro-repo created with:
##
## p4 init -C0 -n
##
## This creates a depot and default stream/client without touching any real
## server, depot, or workspace. This script does not create a depot manually.
export P4CONFIG=.p4config.local
export P4ENVIRO=/dev/null/.p4enviro
declare ThisScript=${0##*/}
declare Version=1.0.0
declare -i ErrorCount=0
declare AppHome="$PWD"
declare H1="=============================================================================="
declare Log="${ThisScript%.sh}.log"
declare CmdLog=
declare ReproDir=/tmp/repro
## Micro functions.
function msg () { echo -e "${1:-Hi}"; }
function errmsg () { msg "\nError: ${1:-Unknown Error}"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "$ErrorCount"; }
function cmd () { msg "${2:-Executing command: $1}"; $1; return $?; }
function usage () { grep '^##' "$0" | sed 's/^##//'; exit 1; }
declare -i Force=0
declare -i Scenario=1
declare ScenarioTitle="<SCENARIO TITLE>"
#------------------------------------------------------------------------------
## Command Line Args
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-f) Force=1;;
(-h) usage;;
(-*) bail "Usage error: Unknown option ($1).";;
(*) bail "Usage error: Unknown parameter ($1).";;
esac
shift
done
set -u
if [[ "$Log" != off ]]; then
touch "$Log" || bail "Couldn't touch log file [$Log]."
## Redirect stdout and stderr to a log file.
exec > >(tee "$Log")
exec 2>&1
fi
msg "Started ${0##*/} v$Version at $(date) in $AppHome."
msg "ReproDir=$ReproDir"
[[ -d "$ReproDir" && "$Force" -eq 1 ]] && /bin/rm -rf "$ReproDir"
[[ -d "$ReproDir" ]] && bail "Old repro dir [$ReproDir] exists. Use -f to remove it first."
mkdir "$ReproDir" || bail "Could not do: mkdir $ReproDir"
cd "$ReproDir" || bail "Could not do: cd $ReproDir"
CmdLog="$ReproDir/cmd.log"
msg "$H1\nScenario $Scenario: $ScenarioTitle\n"
msg "\nPreliminary info: Show versions of p4/p4d on the PATH:"
cmd "p4 -V"
cmd "p4d -V"
msg "\nPreliminary setup: Spin up a local repo."
msg "Operating in: $PWD"
cmd "p4 init -C0 -n"
msg "\nAfter 'p4 init -C0 -n', the local repo should already have a stream depot and a mainline stream, along with a client workspace switched to that stream."
cmd "p4 info"
#------------------------------------------------------------------------------
# Scenario setup: <DESCRIBE THE SETUP STEPS THAT PRECEDE THE BUG>
#------------------------------------------------------------------------------
msg "\n$H1\nScenario $Scenario setup: <SETUP TITLE>.\n"
# <ADD/SUBMIT FILES, OPEN FILES, EDIT SPECS, ETC. HERE>
#
# msg "..."
# cmd "p4 ..."
#------------------------------------------------------------------------------
# <NEXT STEP THAT TRIGGERS THE BUG>
#------------------------------------------------------------------------------
Scenario=$((Scenario+1))
msg "\n$H1\nScenario $Scenario: <TITLE OF THE STEP THAT TRIGGERS THE BUG>.\n"
# <PERFORM THE TRIGGERING ACTION, e.g. edit a stream spec, sync, etc.>
#------------------------------------------------------------------------------
# Observe and assert the disagreement/bug.
#------------------------------------------------------------------------------
Scenario=$((Scenario+1))
msg "\n$H1\nScenario $Scenario: Observe the disagreement/bug.\n"
msg "\n'<COMMAND VARIANT 1>':"
# p4 <command> > "$CmdLog" 2>&1 || true
# cat "$CmdLog"
# if ! grep -q '<expected substring>' "$CmdLog"; then
# errmsg "'<command variant 1>' did not show the expected (buggy) output."
# fi
msg "\n'<COMMAND VARIANT 2, for comparison>':"
# p4 <command> > "$CmdLog" 2>&1 || true
# cat "$CmdLog"
# if ! grep -q '<expected substring>' "$CmdLog"; then
# errmsg "'<command variant 2>' did not show the expected (correct) output."
# fi
#------------------------------------------------------------------------------
# Result.
#------------------------------------------------------------------------------
msg "\n$H1\nThe Result:\n"
if [[ "$ErrorCount" -eq 0 ]]; then
rm -f "$CmdLog"
msg "Yay, we reproduced the <bug/behavior>!"
msg "\n<Explain in plain language what was observed, why it's unexpected/interesting, and what real-world impact it has.>"
else
msg "One or more checks reported errors. Review the log above; the repro conditions may not have reproduced as expected on this version of p4d."
fi
exit "$ErrorCount"