Files
cocommit/src_code/go_src/cmd/cmd_test.go
T
Slug-Boi b1c4df81c8 feat: added commitizen support (due to cmd execution styles this will not be in the test suite)
Due to the way that the interactive cli call has to be made there is no
good way to automate the tests for it without spending a long time
trying to understand go pipes. since this is more of an extra feature
anyways I am fine with leaving this somewhat untested especially since
it is small isolated code
2024-10-31 21:24:17 +01:00

161 lines
2.9 KiB
Go

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)
}
// Skip cobra cmd tests on CI causes problems apparenly idk why
// test will be run locally before releasing a new version
func skipCI(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
}
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) {
skipCI(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) {
skipCI(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) {
skipCI(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