Skip to content
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

Add tests #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod

- name: golangci-lint
uses: golangci/golangci-lint-action@v7

- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test-coverage
test-coverage:
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.9.1
github.com/stretchr/testify v1.8.4
gopkg.in/ini.v1 v1.67.0
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/stretchr/testify v1.10.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
77 changes: 77 additions & 0 deletions src/cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

// Create mock AWS config
configPath := testutils.CreateMockAWSConfig(t, tempDir)
testutils.SetTestEnv(t, "AWS_CONFIG_FILE", configPath)
defer testutils.UnsetTestEnv(t, "AWS_CONFIG_FILE")

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

// Create mock AWS config
configPath := testutils.CreateMockAWSConfig(t, tempDir)
testutils.SetTestEnv(t, "AWS_CONFIG_FILE", configPath)
defer testutils.UnsetTestEnv(t, "AWS_CONFIG_FILE")

// Set HOME environment variable
testutils.SetTestEnv(t, "HOME", tempDir)
defer testutils.UnsetTestEnv(t, "HOME")

// Create a new command instance
cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

// Add the list command
listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

// Test both list and l aliases
aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All @@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All @@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
134 changes: 134 additions & 0 deletions src/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

// Create mock AWS config
configPath := testutils.CreateMockAWSConfig(t, tempDir)
testutils.SetTestEnv(t, "AWS_CONFIG_FILE", configPath)
defer testutils.UnsetTestEnv(t, "AWS_CONFIG_FILE")

// Set HOME environment variable
testutils.SetTestEnv(t, "HOME", tempDir)
defer testutils.UnsetTestEnv(t, "HOME")

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Remove any existing .awsd file before each test
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
// This is a simple test that just ensures the function doesn't panic
// since we can't easily capture stdout in tests
printColoredMessage("test", "test")
}
Loading
Loading