2024-01-13 14:39:40 +00:00
|
|
|
package grm
|
2021-11-02 18:19:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-13 14:39:40 +00:00
|
|
|
"gitlab.com/revalus/grm/internal/commands"
|
|
|
|
"gitlab.com/revalus/grm/internal/config"
|
|
|
|
"gitlab.com/revalus/grm/internal/echo"
|
2021-11-02 18:19:31 +00:00
|
|
|
"os"
|
2021-11-07 13:05:45 +00:00
|
|
|
"reflect"
|
2021-11-02 18:19:31 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-11-07 13:05:45 +00:00
|
|
|
type FakeCommandToTest struct {
|
|
|
|
triggerError bool
|
|
|
|
triggerChanged bool
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:58:49 +00:00
|
|
|
type ExpectedMessageTester struct {
|
|
|
|
expectedMessages []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (emt ExpectedMessageTester) Write(p []byte) (n int, err error) {
|
|
|
|
msg := string(p)
|
|
|
|
if !checkIsItemInSlice(msg, emt.expectedMessages) {
|
|
|
|
panic(fmt.Sprintf("the message \"%v\"does not match any of the given patterns: %#v", msg, emt.expectedMessages))
|
|
|
|
} else {
|
|
|
|
fmt.Println(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
func (fk FakeCommandToTest) Command(repoCfg config.RepositoryConfig) commands.CommandStatus {
|
2021-11-07 13:05:45 +00:00
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: repoCfg.Name,
|
|
|
|
Changed: false,
|
|
|
|
Message: "response from fake command",
|
|
|
|
Error: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
if fk.triggerError {
|
|
|
|
status.Error = true
|
|
|
|
}
|
|
|
|
if fk.triggerChanged {
|
|
|
|
status.Changed = true
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
return status
|
2021-11-07 13:05:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 18:19:31 +00:00
|
|
|
func prepareConfigContent() (string, string) {
|
|
|
|
checkErrorDuringPreparation := func(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Cannot prepare a temporary directory for testing! %v ", err.Error())
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
baseTmp := fmt.Sprintf("%v/grmTest", os.TempDir())
|
|
|
|
if _, ok := os.Stat(baseTmp); ok != nil {
|
|
|
|
err := os.Mkdir(baseTmp, 0777)
|
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tempDir, err := os.MkdirTemp(baseTmp, "*")
|
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
|
|
|
configFilePath := fmt.Sprintf("%v/config-file.yaml", tempDir)
|
|
|
|
|
|
|
|
file, err := os.Create(configFilePath)
|
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
yamlConfig := fmt.Sprintf(`
|
|
|
|
workspace: %v
|
|
|
|
repositories:
|
2021-11-07 13:05:45 +00:00
|
|
|
- src: "https://github.com/golang/example.git"
|
|
|
|
tags: ['example']
|
|
|
|
`, tempDir)
|
2021-11-02 18:19:31 +00:00
|
|
|
|
|
|
|
_, err = file.WriteString(yamlConfig)
|
|
|
|
|
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
return tempDir, configFilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseApplication(t *testing.T) {
|
|
|
|
workdir, configFile := prepareConfigContent()
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove(workdir)
|
|
|
|
})
|
|
|
|
|
2024-01-13 14:39:40 +00:00
|
|
|
args := []string{"custom-grm", "sync", "-c", configFile}
|
2021-11-02 18:19:31 +00:00
|
|
|
grm := GitRepositoryManager{}
|
|
|
|
grm.Parse(args)
|
|
|
|
|
|
|
|
if workdir != grm.configuration.Workspace {
|
|
|
|
t.Errorf("Expected to get %v, instead of this got %v", workdir, grm.configuration.Repositories)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !grm.cliArguments.Sync {
|
|
|
|
t.Error("The value of \"sync\" is expected to be true")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRepo := config.RepositoryConfig{
|
|
|
|
Name: "example",
|
|
|
|
Src: "https://github.com/golang/example.git",
|
|
|
|
Dest: "example",
|
2021-11-07 13:05:45 +00:00
|
|
|
Tags: []string{"example"},
|
2021-11-02 18:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 13:05:45 +00:00
|
|
|
if !reflect.DeepEqual(expectedRepo, grm.configuration.Repositories[0]) {
|
2021-11-02 18:19:31 +00:00
|
|
|
t.Errorf("Expected to get %v, instead of this got %v", expectedRepo, grm.configuration.Repositories[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:58:49 +00:00
|
|
|
func TestOutputFromSync(t *testing.T) {
|
2021-11-02 18:19:31 +00:00
|
|
|
grm := GitRepositoryManager{
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Workspace: "/tmp",
|
|
|
|
},
|
|
|
|
cliArguments: config.CliArguments{
|
2021-11-08 17:30:45 +00:00
|
|
|
Sync: true,
|
|
|
|
Version: true,
|
|
|
|
Color: false,
|
|
|
|
Routines: 10,
|
2021-11-02 18:19:31 +00:00
|
|
|
},
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Info: Synchronizing repositories\n",
|
|
|
|
"Info: All repositories are synced\n",
|
|
|
|
fmt.Sprintf("Info: Current version: %v\n", VERSION),
|
2021-11-02 18:19:31 +00:00
|
|
|
},
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
grm.Run(emt)
|
2021-11-07 13:05:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:58:49 +00:00
|
|
|
func TestLimitTags(t *testing.T) {
|
2021-11-07 13:05:45 +00:00
|
|
|
grm := GitRepositoryManager{
|
|
|
|
cliArguments: config.CliArguments{
|
2021-11-08 17:30:45 +00:00
|
|
|
LimitToTags: []string{"example"},
|
|
|
|
Routines: 10,
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Repositories: []config.RepositoryConfig{
|
|
|
|
{Name: "example1", Tags: []string{"example"}},
|
|
|
|
{Name: "example2", Tags: []string{"example"}},
|
|
|
|
{Name: "notExample"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeCommand := FakeCommandToTest{
|
|
|
|
triggerError: false,
|
|
|
|
triggerChanged: false,
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"example1\": response from fake command\n",
|
|
|
|
"Repository \"example2\": response from fake command\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
2024-06-13 19:19:04 +00:00
|
|
|
grm.limitRepositoriesToTags()
|
2021-11-07 13:05:45 +00:00
|
|
|
grm.runCommand(fakeCommand)
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:58:49 +00:00
|
|
|
func TestLimitName(t *testing.T) {
|
2021-11-07 13:05:45 +00:00
|
|
|
grm := GitRepositoryManager{
|
|
|
|
cliArguments: config.CliArguments{
|
2021-11-08 17:30:45 +00:00
|
|
|
LimitToName: "notExample",
|
|
|
|
Routines: 10,
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Repositories: []config.RepositoryConfig{
|
|
|
|
{Name: "example1", Tags: []string{"example"}},
|
|
|
|
{Name: "example2", Tags: []string{"example"}},
|
|
|
|
{Name: "notExample"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeCommand := FakeCommandToTest{
|
|
|
|
triggerError: false,
|
|
|
|
triggerChanged: false,
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"notExample\": response from fake command\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
2024-06-13 19:19:04 +00:00
|
|
|
grm.limitRepositoryToName()
|
2021-11-07 13:05:45 +00:00
|
|
|
grm.runCommand(fakeCommand)
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
func TestRunWithNotExistingNameInLimit(t *testing.T) {
|
2021-11-07 13:05:45 +00:00
|
|
|
grm := GitRepositoryManager{
|
|
|
|
cliArguments: config.CliArguments{
|
2021-11-08 17:30:45 +00:00
|
|
|
LimitToName: "not-existing-name",
|
|
|
|
Routines: 10,
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Repositories: []config.RepositoryConfig{
|
|
|
|
{Name: "example1", Tags: []string{"example"}},
|
|
|
|
{Name: "example2", Tags: []string{"example"}},
|
|
|
|
{Name: "notExample"},
|
|
|
|
},
|
|
|
|
},
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Error: no repository was found with the specified name\n",
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
echo.Color(false)
|
|
|
|
status := grm.Run(emt)
|
|
|
|
if status != 1 {
|
|
|
|
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
|
|
|
|
}
|
2021-11-07 13:05:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:58:49 +00:00
|
|
|
func TestRunWithNotExistingTagsInLimit(t *testing.T) {
|
2021-11-07 13:05:45 +00:00
|
|
|
grm := GitRepositoryManager{
|
|
|
|
cliArguments: config.CliArguments{
|
2021-11-08 17:30:45 +00:00
|
|
|
LimitToTags: []string{"not-existing-tag"},
|
|
|
|
Routines: 10,
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Repositories: []config.RepositoryConfig{
|
|
|
|
{Name: "example1", Tags: []string{"example"}},
|
|
|
|
{Name: "example2", Tags: []string{"example"}},
|
|
|
|
{Name: "notExample"},
|
|
|
|
},
|
|
|
|
},
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Error: no repository was found with the specified tags\n",
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
echo.Color(false)
|
|
|
|
status := grm.Run(emt)
|
|
|
|
if status != 1 {
|
|
|
|
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
|
|
|
|
}
|
2021-11-07 13:05:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:58:49 +00:00
|
|
|
func TestGetStatusOutput(t *testing.T) {
|
2021-11-07 13:05:45 +00:00
|
|
|
grm := GitRepositoryManager{
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Workspace: "/tmp",
|
|
|
|
},
|
|
|
|
cliArguments: config.CliArguments{
|
2021-11-08 17:30:45 +00:00
|
|
|
Status: true,
|
|
|
|
Routines: 10,
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Info: Current status of repositories\n",
|
2021-11-07 13:05:45 +00:00
|
|
|
},
|
|
|
|
}
|
2021-11-07 18:58:49 +00:00
|
|
|
echo.Color(false)
|
|
|
|
status := grm.Run(emt)
|
|
|
|
if status != 0 {
|
|
|
|
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
|
|
|
|
}
|
2021-11-08 17:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeStatusErrorNoColor(t *testing.T) {
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"Test\": an error occurred: test\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: "Test",
|
|
|
|
Message: "test",
|
|
|
|
Error: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
describeStatus(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeStatusErrorColor(t *testing.T) {
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
fmt.Sprintf("%vRepository \"Test\": an error occurred: test%v\n", echo.ColorRed, echo.ColorReset),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(true)
|
|
|
|
echo.Output(emt)
|
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: "Test",
|
|
|
|
Message: "test",
|
|
|
|
Error: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
describeStatus(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeStatusChangedNoColor(t *testing.T) {
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"Test\": test\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: "Test",
|
|
|
|
Message: "test",
|
|
|
|
Changed: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
describeStatus(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeStatusChangedColor(t *testing.T) {
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
fmt.Sprintf("%vRepository \"Test\": test%v\n", echo.ColorYellow, echo.ColorReset),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(true)
|
|
|
|
echo.Output(emt)
|
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: "Test",
|
|
|
|
Message: "test",
|
|
|
|
Changed: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
describeStatus(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeStatusNoChangeNoColor(t *testing.T) {
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"Test\": test\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: "Test",
|
|
|
|
Message: "test",
|
|
|
|
Changed: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
describeStatus(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeStatusNoChangeColor(t *testing.T) {
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
fmt.Sprintf("%vRepository \"Test\": test%v\n", echo.ColorGreen, echo.ColorReset),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(true)
|
|
|
|
echo.Output(emt)
|
|
|
|
status := commands.CommandStatus{
|
|
|
|
Name: "Test",
|
|
|
|
Message: "test",
|
|
|
|
Changed: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
describeStatus(status)
|
2021-11-02 18:19:31 +00:00
|
|
|
}
|
2021-11-08 19:34:32 +00:00
|
|
|
|
|
|
|
func TestSkipRepository(t *testing.T) {
|
|
|
|
grm := GitRepositoryManager{
|
|
|
|
cliArguments: config.CliArguments{
|
|
|
|
LimitToTags: []string{"example"},
|
|
|
|
Routines: 10,
|
|
|
|
},
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Repositories: []config.RepositoryConfig{
|
|
|
|
{Name: "example1"},
|
|
|
|
{Name: "example2", Skip: true},
|
|
|
|
{Name: "example3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeCommand := FakeCommandToTest{
|
|
|
|
triggerError: false,
|
|
|
|
triggerChanged: false,
|
|
|
|
}
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"example1\": response from fake command\n",
|
|
|
|
"Repository \"example3\": response from fake command\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
|
|
|
grm.runCommand(fakeCommand)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSkipRepositoryWithIgnore(t *testing.T) {
|
|
|
|
grm := GitRepositoryManager{
|
|
|
|
cliArguments: config.CliArguments{
|
|
|
|
LimitToTags: []string{"example"},
|
|
|
|
Routines: 10,
|
|
|
|
IgnoreSkipped: true,
|
|
|
|
},
|
|
|
|
configuration: config.Configuration{
|
|
|
|
Repositories: []config.RepositoryConfig{
|
|
|
|
{Name: "example1"},
|
|
|
|
{Name: "example2", Skip: true},
|
|
|
|
{Name: "example3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeCommand := FakeCommandToTest{
|
|
|
|
triggerError: false,
|
|
|
|
triggerChanged: false,
|
|
|
|
}
|
|
|
|
emt := ExpectedMessageTester{
|
|
|
|
expectedMessages: []string{
|
|
|
|
"Repository \"example1\": response from fake command\n",
|
|
|
|
"Repository \"example2\": response from fake command\n",
|
|
|
|
"Repository \"example3\": response from fake command\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
echo.Color(false)
|
|
|
|
echo.Output(emt)
|
|
|
|
grm.runCommand(fakeCommand)
|
|
|
|
}
|