Remove the dedicated test shell scripts and instead auto-generate them

This commit is contained in:
David Dworken 2022-10-23 17:35:02 -07:00
parent 253f2fdc27
commit 41586ba034
8 changed files with 46 additions and 131 deletions

View File

@ -294,8 +294,8 @@ func testBasicUserFlow(t *testing.T, tester shellTester) string {
if err != nil {
t.Fatalf("failed to read config.sh: %v", err)
}
if !strings.Contains(string(dat), "except it doesn't run the save process in the background") {
t.Fatalf("config.sh is the prod version when it shouldn't be, config.sh=%#v", dat)
if strings.Contains(string(dat), "# Background Run") {
t.Fatalf("config.sh is the prod version when it shouldn't be, config.sh=%#v", string(dat))
}
// Test the banner

View File

@ -13,7 +13,8 @@ function __hishtory_on_prompt --on-event fish_prompt
if [ -n "$_hishtory_first_prompt" ]
set --global -e _hishtory_first_prompt
else if [ -n "$_hishtory_command" ]
hishtory saveHistoryEntry fish $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time &
hishtory saveHistoryEntry fish $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time & # Background Run
# hishtory saveHistoryEntry fish $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time # Foreground Run
set --global -e _hishtory_command # Unset _hishtory_command so we don't double-save entries when fish_prompt is invoked but fish_postexec isn't
end
end

View File

@ -23,7 +23,8 @@ function __hishtory_postcommand() {
fi
# Run after every prompt
(hishtory saveHistoryEntry bash $EXIT_CODE "`history 1`" $HISHTORY_START_TIME &)
(hishtory saveHistoryEntry bash $EXIT_CODE "`history 1`" $HISHTORY_START_TIME &) # Background Run
hishtory saveHistoryEntry bash $EXIT_CODE "`history 1`" $HISHTORY_START_TIME # Foreground Run
}
PROMPT_COMMAND="__hishtory_postcommand; $PROMPT_COMMAND"
export HISTTIMEFORMAT=$HISTTIMEFORMAT

View File

@ -19,7 +19,8 @@ function _hishtory_precmd() {
unset _hishtory_first_prompt
return
fi
(hishtory saveHistoryEntry zsh $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time &)
(hishtory saveHistoryEntry zsh $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time &) # Background Run
# hishtory saveHistoryEntry zsh $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time # Foreground Run
}
_hishtory_widget() {

View File

@ -40,21 +40,12 @@ import (
//go:embed config.sh
var ConfigShContents string
//go:embed test_config.sh
var TestConfigShContents string
//go:embed config.zsh
var ConfigZshContents string
//go:embed test_config.zsh
var TestConfigZshContents string
//go:embed config.fish
var ConfigFishContents string
//go:embed test_config.fish
var TestConfigFishContents string
var Version string = "Unknown"
// 256KB ought to be enough for any reasonable cmd
@ -599,8 +590,6 @@ func promptOnUpgradedFeatures() error {
return nil
}
// TODO: deduplicate shell config code
func configureFish(homedir, binaryPath string) error {
// Check if fish is installed
_, err := exec.LookPath("fish")
@ -611,7 +600,11 @@ func configureFish(homedir, binaryPath string) error {
fishConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.fish")
configContents := ConfigFishContents
if os.Getenv("HISHTORY_TEST") != "" {
configContents = TestConfigFishContents
testConfig, err := tweakConfigForTests(ConfigFishContents)
if err != nil {
return err
}
configContents = testConfig
}
err = ioutil.WriteFile(fishConfigPath, []byte(configContents), 0o644)
if err != nil {
@ -659,7 +652,11 @@ func configureZshrc(homedir, binaryPath string) error {
zshConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.zsh")
configContents := ConfigZshContents
if os.Getenv("HISHTORY_TEST") != "" {
configContents = TestConfigZshContents
testConfig, err := tweakConfigForTests(configContents)
if err != nil {
return err
}
configContents = testConfig
}
err := ioutil.WriteFile(zshConfigPath, []byte(configContents), 0o644)
if err != nil {
@ -703,7 +700,11 @@ func configureBashrc(homedir, binaryPath string) error {
bashConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.sh")
configContents := ConfigShContents
if os.Getenv("HISHTORY_TEST") != "" {
configContents = TestConfigShContents
testConfig, err := tweakConfigForTests(ConfigShContents)
if err != nil {
return err
}
configContents = testConfig
}
err := ioutil.WriteFile(bashConfigPath, []byte(configContents), 0o644)
if err != nil {
@ -1289,3 +1290,26 @@ func GetBanner(ctx *context.Context, gitCommit string) ([]byte, error) {
url := "/api/v1/banner?commit_hash=" + gitCommit + "&user_id=" + data.UserId(config.UserSecret) + "&device_id=" + config.DeviceId + "&version=" + Version + "&forced_banner=" + os.Getenv("FORCED_BANNER")
return ApiGet(url)
}
func tweakConfigForTests(configContents string) (string, error) {
madeSubstitution := false
skipLineIndex := -1
ret := ""
split := strings.Split(configContents, "\n")
for i, line := range split {
if strings.Contains(line, "# Background Run") {
ret += strings.ReplaceAll(split[i+1], "# hishtory", "hishtory")
madeSubstitution = true
skipLineIndex = i + 1
} else if i == skipLineIndex {
continue
} else {
ret += line
}
ret += "\n"
}
if !madeSubstitution {
return "", fmt.Errorf("failed to find substitution line in configConents=%#v", configContents)
}
return ret, nil
}

View File

@ -1,33 +0,0 @@
function _hishtory_post_exec --on-event fish_postexec
# Runs after <ENTER>, but before the command is executed
set --global _hishtory_command $argv
set --global _hishtory_start_time (date +%s)
end
set --global _hishtory_first_prompt 1
function __hishtory_on_prompt --on-event fish_prompt
# Runs after the command is executed in order to render the prompt
# $? contains the exit code
set _hishtory_exit_code $status
if [ -n "$_hishtory_first_prompt" ]
set --global -e _hishtory_first_prompt
else if [ -n "$_hishtory_command" ]
hishtory saveHistoryEntry fish $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time
set --global -e _hishtory_command # Unset _hishtory_command so we don't double-save entries when fish_prompt is invoked but fish_postexec isn't
end
end
function __hishtory_on_control_r
set -l tmp (mktemp -t fish.XXXXXX)
set -x init_query (commandline -b)
hishtory tquery $init_query > $tmp
set -l res $status
commandline -f repaint
if [ -s $tmp ]
commandline -r (cat $tmp)
end
rm -f $tmp
end
[ (hishtory config-get enable-control-r) = true ] && bind \cr __hishtory_on_control_r

View File

@ -1,41 +0,0 @@
# This script should be sourced inside of .bashrc to integrate bash with hishtory
# This is the same as config.sh, except it doesn't run the save process in the background. This is crucial to making tests reproducible.
# Implementation of running before/after every command based on https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/
function __hishtory_precommand() {
if [ -z "$HISHTORY_AT_PROMPT" ]; then
return
fi
unset HISHTORY_AT_PROMPT
# Run before every command
HISHTORY_START_TIME=`date +%s`
}
trap "__hishtory_precommand" DEBUG
HISHTORY_FIRST_PROMPT=1
function __hishtory_postcommand() {
EXIT_CODE=$?
HISHTORY_AT_PROMPT=1
if [ -n "$HISHTORY_FIRST_PROMPT" ]; then
unset HISHTORY_FIRST_PROMPT
return
fi
# Run after every prompt
hishtory saveHistoryEntry bash $EXIT_CODE "`history 1`" $HISHTORY_START_TIME
}
PROMPT_COMMAND="__hishtory_postcommand; $PROMPT_COMMAND"
export HISTTIMEFORMAT=$HISTTIMEFORMAT
__history_control_r() {
READLINE_LINE=$(hishtory tquery "$READLINE_LINE" | tr -d '\n')
READLINE_POINT=0x7FFFFFFF
}
__hishtory_bind_control_r() {
bind -x '"\C-r": __history_control_r'
}
[ "$(hishtory config-get enable-control-r)" = true ] && __hishtory_bind_control_r

View File

@ -1,38 +0,0 @@
# This is the same as config.zsh, except it doesn't run the save process in the background. This is crucial to making tests reproducible.
autoload -U add-zsh-hook
add-zsh-hook zshaddhistory _hishtory_add
add-zsh-hook precmd _hishtory_precmd
_hishtory_first_prompt=1
function _hishtory_add() {
# Runs after <ENTER>, but before the command is executed
# $1 contains the command that was run
_hishtory_command=$1
_hishtory_start_time=`date +%s`
}
function _hishtory_precmd() {
# Runs after the command is executed in order to render the prompt
# $? contains the exit code
_hishtory_exit_code=$?
if [ -n "$_hishtory_first_prompt" ]; then
unset _hishtory_first_prompt
return
fi
hishtory saveHistoryEntry zsh $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time
}
_hishtory_widget() {
BUFFER=$(hishtory tquery $BUFFER | tr -d '\n')
CURSOR=${#BUFFER}
zle reset-prompt
}
_hishtory_bind_control_r() {
zle -N _hishtory_widget
bindkey '^R' _hishtory_widget
}
[ "$(hishtory config-get enable-control-r)" = true ] && _hishtory_bind_control_r