Fix test failures caused by the cobra install command not respecting the secret key

Also added a persistLog() function so that I can easily inspect the hishtory logs from test runs.
This commit is contained in:
David Dworken 2022-11-16 20:28:25 -08:00
parent 35208680d8
commit 86f9d67aff
No known key found for this signature in database
4 changed files with 30 additions and 9 deletions

View File

@ -18,8 +18,13 @@ var installCmd = &cobra.Command{
Use: "install",
Hidden: true,
Short: "Copy this binary to ~/.hishtory/ and configure your shell to use it for recording your shell history",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
lib.CheckFatalError(lib.Install(*offlineInstall))
secretKey := ""
if len(args) > 0 {
secretKey = args[0]
}
lib.CheckFatalError(lib.Install(secretKey, *offlineInstall))
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
db, err := hctx.OpenLocalSqliteDb()
lib.CheckFatalError(err)

View File

@ -78,7 +78,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
return nil, err
}
newLogger := logger.New(
GetLogger(),
GetLogger().WithField("fromSQL", true),
logger.Config{
SlowThreshold: 100 * time.Millisecond,
LogLevel: logger.Warn,

View File

@ -652,7 +652,7 @@ func readFileToArray(path string) ([]string, error) {
return lines, nil
}
func Install(offline bool) error {
func Install(secretKey string, offline bool) error {
homedir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get user's home directory: %v", err)
@ -683,8 +683,8 @@ func Install(offline bool) error {
}
_, err = hctx.GetConfig()
if err != nil {
// No config, so set up a new installation with a new key
return Setup("", offline)
// No config, so set up a new installation
return Setup(secretKey, offline)
}
return nil
}

View File

@ -25,10 +25,8 @@ const (
func ResetLocalState(t *testing.T) {
homedir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("failed to retrieve homedir: %v", err)
}
Check(t, err)
persistLog()
_ = BackupAndRestoreWithId(t, "-reset-local-state")
_ = os.RemoveAll(path.Join(homedir, data.HISHTORY_PATH))
}
@ -90,6 +88,7 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() {
t.Fatalf("failed to execute killall hishtory, stdout=%#v: %v", string(stdout), err)
}
}
persistLog()
Check(t, os.RemoveAll(path.Join(homedir, data.HISHTORY_PATH)))
Check(t, os.MkdirAll(path.Join(homedir, data.HISHTORY_PATH), os.ModePerm))
for _, file := range renameFiles {
@ -298,3 +297,20 @@ func TestLog(t *testing.T, line string) {
Check(t, err)
}
}
func persistLog() {
homedir, err := os.UserHomeDir()
checkError(err)
fp := path.Join(homedir, data.HISHTORY_PATH, "hishtory.log")
log, err := os.ReadFile(fp)
if err != nil {
return
}
f, err := os.OpenFile("/tmp/hishtory.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
checkError(err)
defer f.Close()
_, err = f.Write(log)
checkError(err)
_, err = f.WriteString("\n")
checkError(err)
}