GitRepositoryManager/app/app_test.go

95 lines
2.1 KiB
Go
Raw Normal View History

2021-11-02 18:19:31 +00:00
package app
import (
"fmt"
"os"
"testing"
"gitlab.com/revalus/grm/config"
)
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:
- src: "https://github.com/golang/example.git"`, tempDir)
_, err = file.WriteString(yamlConfig)
checkErrorDuringPreparation(err)
return tempDir, configFilePath
}
func TestParseApplication(t *testing.T) {
workdir, configFile := prepareConfigContent()
t.Cleanup(func() {
os.Remove(workdir)
})
args := []string{"custom-app", "sync", "-c", configFile}
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",
}
if expectedRepo != grm.configuration.Repositories[0] {
t.Errorf("Expected to get %v, instead of this got %v", expectedRepo, grm.configuration.Repositories[0])
}
}
func Example_test_sync_output() {
grm := GitRepositoryManager{
configuration: config.Configuration{
Workspace: "/tmp",
},
cliArguments: config.CliArguments{
Sync: true,
Version: true,
},
console: ConsoleOutput{
Color: false,
},
}
grm.Run()
// Output:
// Info: Synchronizing repositories
// Info: All repositories are synced
2021-11-03 17:20:28 +00:00
// Info: Current version: 0.1.1
2021-11-02 18:19:31 +00:00
}