Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanmetin committed Sep 4, 2023
1 parent 0521b03 commit 23466d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/commands/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions base/commands/update_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)))
Expand Down
33 changes: 33 additions & 0 deletions base/commands/update_check_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 23466d7

Please sign in to comment.