Skip to content

Commit

Permalink
chore: cli test script
Browse files Browse the repository at this point in the history
  • Loading branch information
JaleelB committed Dec 13, 2024
1 parent 39faac6 commit c836c69
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
115 changes: 115 additions & 0 deletions tests/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package tests

import (
"bytes"
"io"
"os"
"testing"

"github.com/JaleelB/jira-flow/internal"
)

func TestCLIMenu(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Status Check",
input: "2\n", // Select Status option
expected: "JiraFlow Status:",
},
{
name: "Exit Option",
input: "4\n", // Select Exit option
expected: "Exiting JiraFlow",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup
config := internal.NewConfig()
oldStdin := os.Stdin
oldStdout := os.Stdout

// Create pipes for input/output
r, w, _ := os.Pipe()
os.Stdin = r

outR, outW, _ := os.Pipe()
os.Stdout = outW

// Write test input
io.WriteString(w, tt.input)
w.Close()

// Run CLI
cmd := internal.CLIMenu(config)
cmd.Execute()

// Capture output
outW.Close()
var buf bytes.Buffer
io.Copy(&buf, outR)

// Cleanup
os.Stdin = oldStdin
os.Stdout = oldStdout

// Assert
if !bytes.Contains(buf.Bytes(), []byte(tt.expected)) {
t.Errorf("expected output to contain %q, got %q", tt.expected, buf.String())
}
})
}
}

func TestStatusCommand(t *testing.T) {
config := internal.NewConfig()

// Test when JiraFlow is not active
t.Run("Inactive Status", func(t *testing.T) {
var buf bytes.Buffer
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

internal.CheckStatus(config)

w.Close()
io.Copy(&buf, r)
os.Stdout = oldStdout

if !bytes.Contains(buf.Bytes(), []byte("not active")) {
t.Error("expected status to show as not active")
}
})
}

func TestToggleJiraFlow(t *testing.T) {
config := internal.NewConfig()

t.Run("Enable JiraFlow", func(t *testing.T) {
err := internal.ToggleJiraFlow(config, true)
if err != nil {
t.Errorf("failed to enable JiraFlow: %v", err)
}

if !internal.IsJiraFlowActive() {
t.Error("JiraFlow should be active after enabling")
}
})

t.Run("Disable JiraFlow", func(t *testing.T) {
err := internal.ToggleJiraFlow(config, false)
if err != nil {
t.Errorf("failed to disable JiraFlow: %v", err)
}

if internal.IsJiraFlowActive() {
t.Error("JiraFlow should not be active after disabling")
}
})
}
2 changes: 1 addition & 1 deletion tests/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package tests

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion tests/git_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package tests

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion tests/jira_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package tests

import (
"testing"
Expand Down

0 comments on commit c836c69

Please sign in to comment.