test: cobra tests added

This commit is contained in:
Slug-Boi
2024-10-26 12:51:24 +02:00
parent ef674c20f9
commit 47128cf3f6
3 changed files with 170 additions and 7 deletions
+149
View File
@@ -0,0 +1,149 @@
package cmd
import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
)
const author_data = `syntax for the test file
te|testing|TestUser|test@test.test|ex
ti|testtest|UserName2|testing@user.io;;gr1`
var envVar = utils.Find_authorfile()
func setup() {
// setup test data
err := os.WriteFile("author_file_test", []byte(author_data), 0644)
if err != nil {
panic(err)
}
os.Setenv("author_file", "author_file_test")
envVar = os.Getenv("author_file")
}
func teardown() {
// remove test data
os.Remove("author_file_test")
os.Setenv("author_file", envVar)
}
func StdoutReader() (chan string, *os.File, *os.File, *os.File) {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
outC := make(chan string)
return outC, r, w, old
}
// users CMD TEST BEGIN
func Test_UsersCmd(t *testing.T) {
setup()
defer teardown()
//stdout reader
outC, r, w, old := StdoutReader()
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
cmd := UsersCmd()
authorfile = "author_file_test"
b := new(bytes.Buffer)
cmd.SetErr(b)
cmd.Execute()
out, err := io.ReadAll(b)
if err != nil {
t.Fatal(err)
}
w.Close()
os.Stdout = old
outStr := <-outC
if outStr == "" {
t.Errorf("Expected output but got nothing")
}
if !strings.Contains(outStr, author_data) {
t.Errorf("Expected to find 'syntax for the test file' in output but got %s", outStr)
}
if string(out) != "" {
t.Errorf("Expected empty output but got %s", string(out))
}
}
// users CMD TEST END
// root CMD TEST BEGIN
func Test_CommitCmd(t *testing.T) {
setup()
defer teardown()
//stdout reader
outC, r, w, old := StdoutReader()
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
cmd := rootCmD
cmd.SetArgs([]string{"-t", "Test commit message"})
cmd.Execute()
w.Close()
os.Stdout = old
outStr := <-outC
if outStr == "" {
t.Errorf("Expected output but got nothing")
}
if !strings.Contains(outStr, "Test commit message\n") {
t.Errorf("Expected to find 'Test commit message' in output but got %s", outStr)
}
}
func Test_CommitCmdWithM(t *testing.T) {
setup()
defer teardown()
//stdout reader
outC, r, w, old := StdoutReader()
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
cmd := rootCmD
cmd.SetArgs([]string{"-m", "-t", "Test commit message"})
cmd.Execute()
w.Close()
os.Stdout = old
outStr := <-outC
if outStr == "" {
t.Errorf("Expected output but got nothing")
}
if !strings.Contains(outStr, "Test commit message\n") {
t.Errorf("Expected to find 'Test commit message' in output but got %s", outStr)
}
}
// root CMD TEST END
+15 -4
View File
@@ -2,16 +2,18 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui" "github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils" "github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"os"
"github.com/inancgumus/screen" "github.com/inancgumus/screen"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ // func RootCmd() *cobra.Command {
var rootCmD = &cobra.Command{
Use: `cocommit <commit message> <co-author1> [co-author2] ... || Use: `cocommit <commit message> <co-author1> [co-author2] ... ||
cocommit <commit message> <co-author1:email> [co-author2:email] ... || cocommit <commit message> <co-author1:email> [co-author2:email] ... ||
cocommit <commit message> all || cocommit <commit message> all ||
@@ -28,6 +30,7 @@ var rootCmd = &cobra.Command{
// check if the print flag is set // check if the print flag is set
pflag, _ := cmd.Flags().GetBool("print") pflag, _ := cmd.Flags().GetBool("print")
tflag, _ := cmd.Flags().GetBool("test_print")
// run execute commands again as root run will not call this part // run execute commands again as root run will not call this part
// redundant check for now but will be useful later when we add tui // redundant check for now but will be useful later when we add tui
wrap_around: wrap_around:
@@ -42,6 +45,11 @@ var rootCmd = &cobra.Command{
goto tui goto tui
case 1: case 1:
if len(args) == 1 { if len(args) == 1 {
if tflag {
fmt.Println(args[0])
return
}
utils.GitWrapper(args[0]) utils.GitWrapper(args[0])
if pflag { if pflag {
fmt.Println(args[0]) fmt.Println(args[0])
@@ -77,12 +85,15 @@ func Execute() {
// define users // define users
utils.Define_users(author_file) utils.Define_users(author_file)
err := rootCmd.Execute() err := rootCmD.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
func init() { func init() {
rootCmd.Flags().BoolP("print", "p", false, "Prints the commit message to the console") //rootCmD := RootCmd()
rootCmD.Flags().BoolP("print", "p", false, "Prints the commit message to the console")
rootCmD.Flags().BoolP("test_print", "t", false, "Prints the commit message to the console without running the git commit command")
rootCmD.Flags().BoolP("message", "m", false, "Does nothing but allows for -m to be used in the command")
} }
+6 -3
View File
@@ -15,7 +15,8 @@ import (
var authorfile = utils.Find_authorfile() var authorfile = utils.Find_authorfile()
// usersCmd represents the users command // usersCmd represents the users command
var usersCmd = &cobra.Command{ func UsersCmd() *cobra.Command {
return &cobra.Command{
Use: "users", Use: "users",
Short: "Displays all users from the author file located at: " + authorfile, Short: "Displays all users from the author file located at: " + authorfile,
Long: `Displays all users from the author file located at: ` + authorfile, Long: `Displays all users from the author file located at: ` + authorfile,
@@ -35,7 +36,7 @@ var usersCmd = &cobra.Command{
} }
sort.Strings(user_sb) sort.Strings(user_sb)
println(strings.Join(user_sb, "")) println(strings.Join(user_sb, ""))
os.Exit(1) os.Exit(0)
} }
bat_check := exec.Command("which", "bat") bat_check := exec.Command("which", "bat")
out, _ := bat_check.CombinedOutput() out, _ := bat_check.CombinedOutput()
@@ -49,8 +50,10 @@ var usersCmd = &cobra.Command{
bat.Run() bat.Run()
}, },
} }
}
func init() { func init() {
rootCmd.AddCommand(usersCmd) usersCmd := UsersCmd()
rootCmD.AddCommand(usersCmd)
usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users") usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users")
} }