From 23466d7ad9ddc302f3576b5e39f1690f0be76ef5 Mon Sep 17 00:00:00 2001 From: kmetin Date: Mon, 4 Sep 2023 17:08:44 +0300 Subject: [PATCH] add test --- base/commands/shell.go | 2 +- base/commands/update_check.go | 6 +++--- base/commands/update_check_test.go | 33 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 base/commands/update_check_test.go diff --git a/base/commands/shell.go b/base/commands/shell.go index 9123f270..4a437230 100644 --- a/base/commands/shell.go +++ b/base/commands/shell.go @@ -77,7 +77,7 @@ func (cm *ShellCommand) ExecInteractive(ctx context.Context, ec plug.ExecContext logText = fmt.Sprintf("Log %9s : %s", logLevel, logPath) } I2(fmt.Fprintf(ec.Stdout(), banner, internal.Version, cfgText, logText)) - if err = maybePrintNewVersionNotification(ctx, ec); err != nil { + if err = MaybePrintNewVersionNotification(ctx, ec); err != nil { ec.Logger().Error(err) } } diff --git a/base/commands/update_check.go b/base/commands/update_check.go index 79905564..af0af109 100644 --- a/base/commands/update_check.go +++ b/base/commands/update_check.go @@ -29,7 +29,7 @@ const ( checkInterval = time.Hour * 24 * 7 ) -func maybePrintNewVersionNotification(ctx context.Context, ec plug.ExecContext) error { +func MaybePrintNewVersionNotification(ctx context.Context, ec plug.ExecContext) error { sa := store.NewStoreAccessor(filepath.Join(paths.Caches(), "update"), ec.Logger()) shouldSkip, err := shouldSkipNewerVersion(sa) if err != nil { @@ -49,7 +49,7 @@ func maybePrintNewVersionNotification(ctx context.Context, ec plug.ExecContext) if err != nil { return err } - if err = updateVersionAndNextCheckTime(sa, latest); err != nil { + if err = UpdateVersionAndNextCheckTime(sa, latest); err != nil { return err } } @@ -97,7 +97,7 @@ func trimVersion(v string) string { return strings.TrimPrefix(strings.Split(v, "-")[0], "v") } -func updateVersionAndNextCheckTime(sa *store.StoreAccessor, v string) error { +func UpdateVersionAndNextCheckTime(sa *store.StoreAccessor, v string) error { _, err := sa.WithLock(func(s *store.Store) (any, error) { err := s.SetEntry([]byte(updateCheckKey), []byte(strconv.FormatInt(time.Now().Add(checkInterval).Unix(), 10))) diff --git a/base/commands/update_check_test.go b/base/commands/update_check_test.go new file mode 100644 index 00000000..03a67b0f --- /dev/null +++ b/base/commands/update_check_test.go @@ -0,0 +1,33 @@ +package commands_test + +import ( + "context" + "path/filepath" + "testing" + + "github.com/hazelcast/hazelcast-commandline-client/base/commands" + "github.com/hazelcast/hazelcast-commandline-client/clc/paths" + "github.com/hazelcast/hazelcast-commandline-client/clc/store" + "github.com/hazelcast/hazelcast-commandline-client/internal" + "github.com/hazelcast/hazelcast-commandline-client/internal/check" + "github.com/hazelcast/hazelcast-commandline-client/internal/it" + "github.com/stretchr/testify/assert" +) + +func Test_maybePrintNewVersionNotification(t *testing.T) { + tcx := it.TestContext{T: t} + tcx.Tester(func(tcx it.TestContext) { + ec := it.NewExecuteContext(nil) + sa := store.NewStoreAccessor(filepath.Join(paths.Caches(), "update"), ec.Logger()) + check.Must(commands.UpdateVersionAndNextCheckTime(sa, "v5.3.2")) + internal.Version = "v5.3.0" + check.Must(commands.MaybePrintNewVersionNotification(context.TODO(), ec)) + o := ec.StdoutText() + expected := `A newer version of CLC is available. +Visit the following link for release notes and to download: +https://github.com/hazelcast/hazelcast-commandline-client/releases/v5.3.2 + +` + assert.Equal(t, expected, o) + }) +}