8000 Fix Version Check & Add Tests by samtholiya · Pull Request #1120 · cloudposse/atmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Version Check & Add Tests #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package cmd

import (
"fmt"
"runtime"
"strings"

"github.com/spf13/cobra"

tuiUtils "github.com/cloudposse/atmos/internal/tui/utils"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/version"
"github.com/cloudposse/atmos/internal/exec"
)

var checkFlag bool
Expand All @@ -21,42 +15,7 @@
Example: "atmos version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Print a styled Atmos logo to the terminal
fmt.Println()
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(err)
}

atmosIcon := "\U0001F47D"

u.PrintMessage(fmt.Sprintf("%s Atmos %s on %s/%s", atmosIcon, version.Version, runtime.GOOS, runtime.GOARCH))
fmt.Println()

if checkFlag {
// Check for the latest Atmos release on GitHub
latestReleaseTag, err := u.GetLatestGitHubRepoRelease("cloudposse", "atmos")
if err == nil && latestReleaseTag != "" {
if err != nil {
u.LogWarning(fmt.Sprintf("Failed to check for updates: %v", err))
return
}
if latestReleaseTag == "" {
u.LogWarning("No release information available")
return
}
latestRelease := strings.TrimPrefix(latestReleaseTag, "v")
currentRelease := strings.TrimPrefix(version.Version, "v")

if latestRelease == currentRelease {
u.PrintMessage(fmt.Sprintf("You are running the latest version of Atmos (%s)", latestRelease))
} else {
u.PrintMessageToUpgradeToAtmosLatestRelease(latestRelease)
}
}
return
}

exec.NewVersionExec().Execute(checkFlag)

Check warning on line 18 in cmd/version.go

View check run for this annotation

Codecov / codecov/patch

cmd/version.go#L18

Added line #L18 was not covered by tests
// Check for the cache and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
},
Expand Down
1 change: 1 addition & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions internal/exec/mock_version_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions internal/exec/version.go
< 8000 td id="diff-f1cd69dfe19f71bd2bb9a56dc23c5aa7615be032abaa3a75a241c83ae12f3b6fR56" data-line-number="56" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package exec

import (
"fmt"
"runtime"
"strings"

log "github.com/charmbracelet/log"
tuiUtils "github.com/cloudposse/atmos/internal/tui/utils"
u "github.com/cloudposse/atmos/pkg/utils"

"github.com/cloudposse/atmos/pkg/version"
)

type versionExec struct {
printStyledText func(string) error
getLatestGitHubRepoRelease func(string, string) (string, error)
printMessage func(string)
printMessageToUpgradeToAtmosLatestRelease func(string)
}

func NewVersionExec() *versionExec {
return &versionExec{
printStyledText: tuiUtils.PrintStyledText,
getLatestGitHubRepoRelease: u.GetLatestGitHubRepoRelease,
printMessage: u.PrintMessage,
printMessageToUpgradeToAtmosLatestRelease: u.PrintMessageToUpgradeToAtmosLatestRelease,
}

Check warning on line 28 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L22-L28

Added lines #L22 - L28 were not covered by tests
}

func (v versionExec) Execute(checkFlag bool) {
// Print a styled Atmos logo to the terminal
v.printMessage("")
err := v.printStyledText("ATMOS")
if err != nil {
//nolint:revive
log.Fatal(err)
}

Check warning on line 38 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L36-L38

Added lines #L36 - L38 were not covered by tests

atmosIcon := "\U0001F47D"

v.printMessage(fmt.Sprintf("%s Atmos %s on %s/%s", atmosIcon, version.Version, runtime.GOOS, runtime.GOARCH))
v.printMessage("")

if checkFlag {
v.checkRelease()
}
}

func (v versionExec) checkRelease() {
// Check for the latest Atmos release on GitHub
latestReleaseTag, err := v.getLatestGitHubRepoRelease("cloudposse", "atmos")
if err != nil || latestReleaseTag == "" {
log.Debug("Did not get release tag", "err", err, "latestReleaseTag", latestReleaseTag)
return
}
latestRelease := strings.TrimPrefix(latestReleaseTag, "v")
currentRelease := strings.TrimPrefix(version.Version, "v")

if latestRelease == currentRelease {
log.Info("You are running the latest version of Atmos", "version", latestRelease)
} else {
v.printMessageToUpgradeToAtmosLatestRelease(latestRelease)
}
}
103 changes: 103 additions & 0 deletions internal/exec/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package exec

import (
"testing"

"github.com/cloudposse/atmos/pkg/version"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
)

// Mock interfaces generated by mockgen
// VersionExecutor defines the interface for version execution operations
//
//go:generate mockgen -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE
type VersionExecutor interface {
PrintStyledText(text string) error
GetLatestGitHubRepoRelease(owner, repo string) (string, error)
PrintMessage(message string)
PrintMessageToUpgradeToAtmosLatestRelease(version string)
}

func TestVersionExec_Execute(t *testing.T) {
// Save original values
originalVersion := version.Version
defer func() { version.Version = originalVersion }()

tests := []struct {
name string
checkFlag bool
version string
latestRelease string
printStyledTextErr error
getLatestReleaseErr error
}{
{
name: "Basic execution without check",
checkFlag: false,
version: "v1.0.0",
},
{
name: "Check flag with same version",
checkFlag: true,
version: "v1.0.0",
latestRelease: "v1.0.0",
},
{
name: "Check flag with different version",
checkFlag: true,
version: "v1.0.0",
latestRelease: "v1.1.0",
},
{
name: "Check flag with release check error",
checkFlag: true,
version: "v1.0.0",
getLatestReleaseErr: errors.New("github error"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup mock controller
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// Create mocks
mockExec := NewMockVersionExecutor(ctrl)

// Set version
version.Version = tt.version

// Setup mock expectations
mockExec.EXPECT().PrintStyledText("ATMOS").Return(tt.printStyledTextErr)
//nolint:nestif
if tt.printStyledTextErr == nil {
mockExec.EXPECT().PrintMessage(gomock.Any()).Times(3)

if tt.checkFlag {
mockExec.EXPECT().GetLatestGitHubRepoRelease("cloudposse", "atmos").
Return(tt.latestRelease, tt.getLatestReleaseErr)

if tt.getLatestReleaseErr == nil && tt.latestRelease != "" {
if tt.latestRelease != tt.version {
mockExec.EXPECT().PrintMessageToUpgradeToAtmosLatestRelease(
gomock.Eq(tt.latestRelease[1:])).Times(1)
}
}
}
}

// Create test instance with mocks
v := versionExec{
printStyledText: mockExec.PrintStyledText,
getLatestGitHubRepoRelease: mockExec.GetLatestGitHubRepoRelease,
printMessage: mockExec.PrintMessage,
printMessageToUpgradeToAtmosLatestRelease: mockExec.PrintMessageToUpgradeToAtmosLatestRelease,
}

// Execute the function
v.Execute(tt.checkFlag)
})
}
}
22 changes: 10 additions & 12 deletions pkg/utils/github_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import (
"context"
"os"
"time"

log "github.com/charmbracelet/log"
"github.com/google/go-github/v59/github"
"golang.org/x/oauth2"
)
Expand All @@ -26,25 +26,23 @@
return github.NewClient(tc)
}

< C4D6 /span>
// GetLatestGitHubRepoRelease returns the latest release tag for a GitHub repository
// GetLatestGitHubRepoRelease returns the latest release tag for a GitHub repository.
func GetLatestGitHubRepoRelease(owner string, repo string) (string, error) {
opt := &github.ListOptions{Page: 1, PerPage: 1}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
log.Debug("Fetching latest release from Github API", "owner", owner, "repo", repo)

Check warning on line 31 in pkg/utils/github_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/github_utils.go#L31

Added line #L31 was not covered by tests

// Create a new GitHub client with authentication if available
ctx := context.Background()

Check warning on line 34 in pkg/utils/github_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/github_utils.go#L33-L34

Added lines #L33 - L34 were not covered by tests
client := newGitHubClient(ctx)

releases, _, err := client.Repositories.ListReleases(ctx, owner, repo, opt)
// Get the latest release
release, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)

Check warning on line 38 in pkg/utils/github_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/github_utils.go#L37-L38

Added lines #L37 - L38 were not covered by tests
if err != nil {
return "", err
}

if len(releases) > 0 {
latestRelease := releases[0]
latestReleaseTag := *latestRelease.TagName
return latestReleaseTag, nil
if release == nil || release.TagName == nil {
return "", nil

Check warning on line 44 in pkg/utils/github_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/github_utils.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

return "", nil
return *release.TagName, nil

Check warning on line 47 in pkg/utils/github_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/github_utils.go#L47

Added line #L47 was not covered by tests
}
Loading
0