Some test fixes for the cobra integration

This commit is contained in:
David Dworken 2022-11-14 20:55:10 -08:00
parent be2cde72c2
commit fe6394d1b5
No known key found for this signature in database
7 changed files with 59 additions and 66 deletions

View File

@ -1206,6 +1206,10 @@ echo other`)
}
func testInstallViaPythonScript(t *testing.T, tester shellTester) {
if !testutils.IsOnline() {
t.Skip("skipping because we're currently offline")
}
// Set up
defer testutils.BackupAndRestore(t)()
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")()
@ -1314,7 +1318,7 @@ func testHelpCommand(t *testing.T, tester shellTester) {
// Test the help command
out := tester.RunInteractiveShell(t, `hishtory help`)
if !strings.HasPrefix(out, "hiSHtory: Better shell history\n\nSupported commands:\n") {
if !strings.HasPrefix(out, "hiSHtory: Better shell history") {
t.Fatalf("expected hishtory help to contain intro, actual=%#v", out)
}
out2 := tester.RunInteractiveShell(t, `hishtory -h`)
@ -1708,7 +1712,7 @@ func testHandleUpgradedFeatures(t *testing.T, tester shellTester) {
// And check that hishtory says it is false by default
out := tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
if out != "false" {
if out != "false\n" {
t.Fatalf("unexpected config-get output: %#v", out)
}
@ -1718,7 +1722,7 @@ func testHandleUpgradedFeatures(t *testing.T, tester shellTester) {
// Now it should be enabled
out = tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
if out != "true" {
if out != "true\n" {
t.Fatalf("unexpected config-get output: %#v", out)
}
}

View File

@ -71,4 +71,5 @@ var deleteDisplayedColumnCommand = &cobra.Command{
func init() {
rootCmd.AddCommand(configDeleteCmd)
configDeleteCmd.AddCommand(deleteCustomColumnsCmd)
configDeleteCmd.AddCommand(deleteDisplayedColumnCommand)
}

View File

@ -1,57 +0,0 @@
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/ddworken/hishtory/client/hctx"
"github.com/ddworken/hishtory/client/lib"
"github.com/spf13/cobra"
)
var offline *bool
var initCmd = &cobra.Command{
Use: "init",
Short: "Re-initialize hiSHtory with a specified secret key",
GroupID: GROUP_ID_CONFIG,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cmd.Flag("offline").Value.String()
db, err := hctx.OpenLocalSqliteDb()
lib.CheckFatalError(err)
data, err := lib.Search(nil, db, "", 10)
lib.CheckFatalError(err)
if len(data) > 0 {
fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset?\nNote: This won't clear any imported history entries from your existing shell\n[y/N]")
reader := bufio.NewReader(os.Stdin)
resp, err := reader.ReadString('\n')
lib.CheckFatalError(err)
if strings.TrimSpace(resp) != "y" {
fmt.Printf("Aborting init per user response of %#v\n", strings.TrimSpace(resp))
return
}
}
secretKey := ""
if len(args) > 0 {
secretKey = args[0]
}
lib.CheckFatalError(lib.Setup(secretKey, *offline))
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
fmt.Println("Importing existing shell history...")
ctx := hctx.MakeContext()
numImported, err := lib.ImportHistory(ctx, false, false)
lib.CheckFatalError(err)
if numImported > 0 {
fmt.Printf("Imported %v history entries from your existing shell history\n", numImported)
}
}
},
}
func init() {
rootCmd.AddCommand(initCmd)
offline = initCmd.Flags().Bool("offline", false, "Install hiSHtory in offline mode wiht all syncing capabilities disabled")
}

View File

@ -1,20 +1,25 @@
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/ddworken/hishtory/client/hctx"
"github.com/ddworken/hishtory/client/lib"
"github.com/spf13/cobra"
)
var offlineInit *bool
var offlineInstall *bool
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",
Run: func(cmd *cobra.Command, args []string) {
lib.CheckFatalError(lib.Install())
lib.CheckFatalError(lib.Install(*offlineInstall))
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
db, err := hctx.OpenLocalSqliteDb()
lib.CheckFatalError(err)
@ -33,6 +38,46 @@ var installCmd = &cobra.Command{
},
}
var initCmd = &cobra.Command{
Use: "init",
Short: "Re-initialize hiSHtory with a specified secret key",
GroupID: GROUP_ID_CONFIG,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
db, err := hctx.OpenLocalSqliteDb()
lib.CheckFatalError(err)
data, err := lib.Search(nil, db, "", 10)
lib.CheckFatalError(err)
if len(data) > 0 {
fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset?\nNote: This won't clear any imported history entries from your existing shell\n[y/N]")
reader := bufio.NewReader(os.Stdin)
resp, err := reader.ReadString('\n')
lib.CheckFatalError(err)
if strings.TrimSpace(resp) != "y" {
fmt.Printf("Aborting init per user response of %#v\n", strings.TrimSpace(resp))
return
}
}
secretKey := ""
if len(args) > 0 {
secretKey = args[0]
}
lib.CheckFatalError(lib.Setup(secretKey, *offlineInit))
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
fmt.Println("Importing existing shell history...")
ctx := hctx.MakeContext()
numImported, err := lib.ImportHistory(ctx, false, false)
lib.CheckFatalError(err)
if numImported > 0 {
fmt.Printf("Imported %v history entries from your existing shell history\n", numImported)
}
}
},
}
func init() {
rootCmd.AddCommand(installCmd)
rootCmd.AddCommand(initCmd)
offlineInit = initCmd.Flags().Bool("offline", false, "Install hiSHtory in offline mode wiht all syncing capabilities disabled")
offlineInstall = installCmd.Flags().Bool("offline", false, "Install hiSHtory in offline mode wiht all syncing capabilities disabled")
}

View File

@ -13,7 +13,7 @@ import (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "hiSHtory",
Short: "A better shell history",
Short: "hiSHtory: Better shell history",
}
// Execute adds all child commands to the root command and sets flags appropriately.

View File

@ -652,7 +652,7 @@ func readFileToArray(path string) ([]string, error) {
return lines, nil
}
func Install() error {
func Install(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() error {
}
_, err = hctx.GetConfig()
if err != nil {
// No config, so set up a new installation with a new key and in online mode
return Setup("", false)
// No config, so set up a new installation with a new key
return Setup("", offline)
}
return nil
}

View File

@ -8,4 +8,4 @@ func main() {
cmd.Execute()
}
// TODO(feature): Add a session_id column that corresponds to the shell session the command was run in
// TODO(feature): Add a session_id column that corresponds to the shell session the command was run in