Skip to content
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
8 changes: 4 additions & 4 deletions internal/cli/confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"strings"
)

func AskForConfirmation(s string) bool {
func AskForConfirmation(s string) (bool, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/N]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
panic(err)
return false, fmt.Errorf("failed to read user input: %w", err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
return true, nil
}
return false
return false, nil
}
230 changes: 230 additions & 0 deletions internal/cli/confirmation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package cli_test

import (
"os"
"testing"

"github.com/openstatusHQ/cli/internal/cli"
)

func Test_AskForConfirmation(t *testing.T) {
t.Run("Returns true for 'y' input", func(t *testing.T) {
// Create a pipe to simulate stdin
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

// Save original stdin
oldStdin := os.Stdin
os.Stdin = r

// Write the input
go func() {
w.WriteString("y\n")
w.Close()
}()

// Restore stdin after test
t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !result {
t.Error("Expected true for 'y' input, got false")
}
})

t.Run("Returns true for 'yes' input", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("yes\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !result {
t.Error("Expected true for 'yes' input, got false")
}
})

t.Run("Returns true for 'Y' input (case insensitive)", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("Y\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !result {
t.Error("Expected true for 'Y' input, got false")
}
})

t.Run("Returns true for 'YES' input (case insensitive)", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("YES\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !result {
t.Error("Expected true for 'YES' input, got false")
}
})

t.Run("Returns false for 'n' input", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("n\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result {
t.Error("Expected false for 'n' input, got true")
}
})

t.Run("Returns false for 'no' input", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("no\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result {
t.Error("Expected false for 'no' input, got true")
}
})

t.Run("Returns false for empty input", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result {
t.Error("Expected false for empty input, got true")
}
})

t.Run("Returns false for random input", func(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}

oldStdin := os.Stdin
os.Stdin = r

go func() {
w.WriteString("maybe\n")
w.Close()
}()

t.Cleanup(func() {
os.Stdin = oldStdin
})

result, err := cli.AskForConfirmation("Test prompt")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result {
t.Error("Expected false for random input, got true")
}
})
}
74 changes: 74 additions & 0 deletions internal/cmd/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd_test

import (
"testing"

"github.com/openstatusHQ/cli/internal/cmd"
)

func Test_NewApp(t *testing.T) {
t.Parallel()

t.Run("Returns valid app command", func(t *testing.T) {
app := cmd.NewApp()

if app == nil {
t.Fatal("Expected non-nil app")
}

if app.Name != "openstatus" {
t.Errorf("Expected app name 'openstatus', got %s", app.Name)
}

if app.Version != "v1.0.0" {
t.Errorf("Expected version 'v1.0.0', got %s", app.Version)
}

if !app.Suggest {
t.Error("Expected Suggest to be true")
}
})

t.Run("Has expected commands", func(t *testing.T) {
app := cmd.NewApp()

if len(app.Commands) != 3 {
t.Errorf("Expected 3 commands, got %d", len(app.Commands))
}

expectedCommands := map[string]bool{
"monitors": false,
"run": false,
"whoami": false,
}

for _, subcmd := range app.Commands {
if _, exists := expectedCommands[subcmd.Name]; exists {
expectedCommands[subcmd.Name] = true
}
}

for name, found := range expectedCommands {
if !found {
t.Errorf("Expected command '%s' not found", name)
}
}
})

t.Run("Has correct usage text", func(t *testing.T) {
app := cmd.NewApp()

expectedUsage := "This is OpenStatus Command Line Interface, the OpenStatus.dev CLI"
if app.Usage != expectedUsage {
t.Errorf("Expected usage '%s', got '%s'", expectedUsage, app.Usage)
}
})

t.Run("Has description", func(t *testing.T) {
app := cmd.NewApp()

if app.Description == "" {
t.Error("Expected non-empty description")
}
})
}
Loading