151 lines
4.0 KiB
Go
151 lines
4.0 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
var exampleYamlConfig = []byte(`
|
||
|
workspace: /tmp
|
||
|
repositories:
|
||
|
- src: "https://github.com/example/example.git"
|
||
|
dest: "example/path"
|
||
|
name: "custom_example"
|
||
|
- src: https://github.com/example/example2.git
|
||
|
`)
|
||
|
|
||
|
var destinationConfiguration = Configuration{
|
||
|
Workspace: "/tmp",
|
||
|
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",
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func TestNotSupportedFileExcension(t *testing.T) {
|
||
|
_, err := GetRepositoryConfig(exampleYamlConfig, "custom")
|
||
|
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) {
|
||
|
|
||
|
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 := fmt.Sprintf(errMissingSrcField, 0)
|
||
|
|
||
|
if err.Error() != expectedError {
|
||
|
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 err.Error() != expectedError.Error() {
|
||
|
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 err.Error() != expectedError.Error() {
|
||
|
t.Errorf("Expected to get error with value \"%v\", instead of this got: \"%v\"", expectedError, err)
|
||
|
}
|
||
|
}
|