diff --git a/cmd/tezosconnect.go b/cmd/tezosconnect.go index e01138d..b8923e7 100644 --- a/cmd/tezosconnect.go +++ b/cmd/tezosconnect.go @@ -40,6 +40,14 @@ func init() { rootCmd.AddCommand(versionCommand()) rootCmd.AddCommand(configCommand()) rootCmd.AddCommand(fftmcmd.ClientCommand()) + migrateCmd := fftmcmd.MigrateCommand(func() error { + InitConfig() + err := config.ReadConfig("tezosconnect", cfgFile) + config.SetupLogging(context.Background()) + return err + }) + migrateCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file") + rootCmd.AddCommand(migrateCmd) } func Execute() error { diff --git a/cmd/tezosconnect_test.go b/cmd/tezosconnect_test.go index 8983ff8..bd1f6d3 100644 --- a/cmd/tezosconnect_test.go +++ b/cmd/tezosconnect_test.go @@ -47,3 +47,13 @@ func TestRun(t *testing.T) { }) } } + +func TestRunMigrationsBadConfig(t *testing.T) { + rootCmd.SetArgs([]string{ + "migrate", "leveldb2postgres", "-f", "../test/fail-start.tezosconnect.yaml", + }) + defer rootCmd.SetArgs([]string{}) + + err := Execute() + assert.Regexp(t, "FF21049", err) +} \ No newline at end of file diff --git a/cmd/version_test.go b/cmd/version_test.go index 193831e..4223619 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,77 +1,49 @@ package cmd import ( - "strconv" + "runtime/debug" "testing" - "time" "github.com/stretchr/testify/assert" ) -func TestVersionCommand(t *testing.T) { - testCases := []struct { - name string - buildDate string - buildCommit string - buildVersionOverride string - output string - shortened bool - errMsg string - }{ - { - name: "error", - buildDate: time.Now().String(), - buildCommit: "243413535", - buildVersionOverride: "0.0.1", - output: "unknown", - errMsg: "FF23016: Invalid output type: unknown", - }, - { - name: "yaml output", - buildDate: time.Now().String(), - buildCommit: "243413535", - buildVersionOverride: "0.0.1", - output: "yaml", - }, - { - name: "json output", - buildDate: time.Now().String(), - buildCommit: "243413535", - buildVersionOverride: "0.0.1", - output: "json", - }, - { - name: "shortened", - buildDate: time.Now().String(), - buildCommit: "243413535", - buildVersionOverride: "0.0.1", - shortened: true, - }, - { - name: "version is empty", - buildDate: time.Now().String(), - buildCommit: "243413535", - buildVersionOverride: "", - output: "json", - }, - } +func TestVersionCmdDefault(t *testing.T) { + rootCmd.SetArgs([]string{"version"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.NoError(t, err) +} + +func TestVersionCmdYAML(t *testing.T) { + rootCmd.SetArgs([]string{"version", "-o", "yaml"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.NoError(t, err) +} + +func TestVersionCmdJSON(t *testing.T) { + rootCmd.SetArgs([]string{"version", "-o", "json"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.NoError(t, err) +} + +func TestVersionCmdInvalidType(t *testing.T) { + rootCmd.SetArgs([]string{"version", "-o", "wrong"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.Regexp(t, "FF23016", err) +} - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - command := versionCommand() - BuildVersionOverride = tc.buildVersionOverride - BuildCommit = tc.buildCommit - BuildDate = tc.buildDate - command.Flags().Set("output", tc.output) - command.Flags().Set("short", strconv.FormatBool(tc.shortened)) - err := command.RunE(command, []string{"arg1"}) +func TestVersionCmdShorthand(t *testing.T) { + rootCmd.SetArgs([]string{"version", "-s"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.NoError(t, err) +} - if tc.errMsg != "" { - assert.Error(t, err) - assert.Equal(t, tc.errMsg, err.Error()) - } else { - assert.NoError(t, err) - } - }) - } +func TestSetBuildInfoWithBI(t *testing.T) { + info := &Info{} + setBuildInfo(info, &debug.BuildInfo{Main: debug.Module{Version: "12345"}}, true) + assert.Equal(t, "12345", info.Version) } diff --git a/test/fail-start.tezosconnect.yaml b/test/fail-start.tezosconnect.yaml new file mode 100644 index 0000000..88ecef0 --- /dev/null +++ b/test/fail-start.tezosconnect.yaml @@ -0,0 +1,3 @@ +persistence: + leveldb: + path: "../test/ldb" diff --git a/test/firefly.tezosconnect-without-connector.yaml b/test/firefly.tezosconnect-without-connector.yaml index cc9f2c3..88ecef0 100644 --- a/test/firefly.tezosconnect-without-connector.yaml +++ b/test/firefly.tezosconnect-without-connector.yaml @@ -1,3 +1,3 @@ persistence: leveldb: - path: ./.leveldb + path: "../test/ldb" diff --git a/test/firefly.tezosconnect.yaml b/test/firefly.tezosconnect.yaml index 7e2f170..77480a1 100644 --- a/test/firefly.tezosconnect.yaml +++ b/test/firefly.tezosconnect.yaml @@ -3,4 +3,4 @@ connector: rpc: https://ghostnet.example.com persistence: leveldb: - path: ./.leveldb + path: "../test/ldb"