GitRepositoryManager/internal/config/config_file_parse_test.go

159 lines
4.1 KiB
Go
Raw Normal View History

2021-11-02 18:19:31 +00:00
package config
import (
"errors"
2021-11-02 18:19:31 +00:00
"fmt"
2021-11-03 17:20:28 +00:00
"os"
2021-11-02 18:19:31 +00:00
"reflect"
"testing"
)
func TestNotSupportedFileExcension(t *testing.T) {
2021-11-03 17:20:28 +00:00
_, err := GetRepositoryConfig([]byte("test"), "custom")
2021-11-02 18:19:31 +00:00
if err == nil {
t.Error("Expected to get error")
}
if err.Error() != errNotSupportedType {
t.Errorf("Expected to get %v, instead of this got %v", errNotSupportedType, err.Error())
}
}
func TestGetRepositoryConfigFromYaml(t *testing.T) {
2021-11-03 17:20:28 +00:00
var exampleYamlConfig = []byte(`
workspace: ${HOME}
repositories:
- src: "https://github.com/example/example.git"
dest: "example/path"
name: "custom_example"
- src: https://github.com/example/example2.git
tags:
- "example"
2021-11-03 17:20:28 +00:00
`)
homedir, _ := os.UserHomeDir()
var destinationConfiguration = Configuration{
Workspace: homedir,
Repositories: []RepositoryConfig{
{
Name: "custom_example",
Dest: "example/path",
Src: "https://github.com/example/example.git",
},
{
Name: "example2",
Src: "https://github.com/example/example2.git",
Dest: "example2",
Tags: []string{"example"},
2021-11-03 17:20:28 +00:00
},
},
}
2021-11-02 18:19:31 +00:00
result, err := GetRepositoryConfig(exampleYamlConfig, "yaml")
if err != nil {
t.Errorf("Unexpected error %v", err.Error())
}
if !reflect.DeepEqual(result.Repositories, destinationConfiguration.Repositories) {
t.Errorf("Default value for configurationFile should be:\n %v \ninstead of this got:\n %v", result, destinationConfiguration)
}
}
func TestWrongYamlFormat(t *testing.T) {
exampleWrongYamlConfig := []byte(`---
workspace: "/test"
repositories:
- src: "https://github.com/example/example.git"
dest: "example/path"
name: "custom_example"
`)
_, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
expectedError := "yaml: line 2: found character that cannot start any token"
if err.Error() != expectedError {
t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError, err.Error())
}
}
func TestMissingWorkspaceRequiredField(t *testing.T) {
exampleWrongYamlConfig := []byte(`---
repositories:
- src: "https://github.com/example/example.git"
dest: "example/path"
name: "custom_example"
`)
_, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
if err.Error() != errMissingWorkspaceField {
t.Errorf("Expected to get error with value %v, instead of this got: %v", errMissingWorkspaceField, err.Error())
}
}
func TestMissingSourceRequiredField(t *testing.T) {
exampleWrongYamlConfig := []byte(`---
workspace: /tmp
repositories:
- dest: "example/path"
name: "custom_example"
`)
_, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
expectedError := errors.New(fmt.Sprintf(errMissingSrcField, 0))
2021-11-02 18:19:31 +00:00
if errors.Is(err, expectedError) {
2021-11-02 18:19:31 +00:00
t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError, err.Error())
}
}
func TestDuplicatedNameField(t *testing.T) {
exampleWrongYamlConfig := []byte(`
workspace: "/tmp"
repositories:
- src: "https://github.com/example/example1.git"
dest: "example/path"
name: "custom_example"
- src: "https://github.com/example/example2.git"
name: "example2"
- src: "https://github.com/example/example2.git"
`)
result, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
if err == nil {
t.Errorf("Unexpected result: %v", result)
}
expectedError := getDuplicateFieldError("name", "example2", []int{1, 2})
if errors.Is(err, expectedError) {
2021-11-02 18:19:31 +00:00
t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError.Error(), err.Error())
}
}
func TestDuplicatedDestField(t *testing.T) {
exampleWrongYamlConfig := []byte(`
workspace: "/tmp"
repositories:
- src: "https://github.com/example/example1.git"
dest: "example/path"
- src: "https://github.com/example/example2.git"
dest: "example"
- src: "https://github.com/example/example3.git"
dest: "example"
`)
result, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
if err == nil {
t.Errorf("Unexpected result: %v", result)
}
expectedError := getDuplicateFieldError("dest", "example", []int{1, 2})
if errors.Is(err, expectedError) {
2021-11-02 18:19:31 +00:00
t.Errorf("Expected to get error with value \"%v\", instead of this got: \"%v\"", expectedError, err)
}
}