mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
test: cobra tests added
This commit is contained in:
@@ -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
|
||||
@@ -2,16 +2,18 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui"
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"os"
|
||||
|
||||
"github.com/inancgumus/screen"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// 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] ... ||
|
||||
cocommit <commit message> <co-author1:email> [co-author2:email] ... ||
|
||||
cocommit <commit message> all ||
|
||||
@@ -28,6 +30,7 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
// check if the print flag is set
|
||||
pflag, _ := cmd.Flags().GetBool("print")
|
||||
tflag, _ := cmd.Flags().GetBool("test_print")
|
||||
// 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
|
||||
wrap_around:
|
||||
@@ -42,6 +45,11 @@ var rootCmd = &cobra.Command{
|
||||
goto tui
|
||||
case 1:
|
||||
if len(args) == 1 {
|
||||
if tflag {
|
||||
fmt.Println(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
utils.GitWrapper(args[0])
|
||||
if pflag {
|
||||
fmt.Println(args[0])
|
||||
@@ -77,12 +85,15 @@ func Execute() {
|
||||
// define users
|
||||
utils.Define_users(author_file)
|
||||
|
||||
err := rootCmd.Execute()
|
||||
err := rootCmD.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ import (
|
||||
var authorfile = utils.Find_authorfile()
|
||||
|
||||
// usersCmd represents the users command
|
||||
var usersCmd = &cobra.Command{
|
||||
func UsersCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "users",
|
||||
Short: "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)
|
||||
println(strings.Join(user_sb, ""))
|
||||
os.Exit(1)
|
||||
os.Exit(0)
|
||||
}
|
||||
bat_check := exec.Command("which", "bat")
|
||||
out, _ := bat_check.CombinedOutput()
|
||||
@@ -49,8 +50,10 @@ var usersCmd = &cobra.Command{
|
||||
bat.Run()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(usersCmd)
|
||||
usersCmd := UsersCmd()
|
||||
rootCmD.AddCommand(usersCmd)
|
||||
usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user