2021-11-02 18:19:31 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2024-06-16 13:28:12 +00:00
|
|
|
"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
|
2021-11-07 13:05:45 +00:00
|
|
|
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",
|
2021-11-07 13:05:45 +00:00
|
|
|
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")
|
|
|
|
|
2024-06-16 13:28:12 +00:00
|
|
|
expectedError := errors.New(fmt.Sprintf(errMissingSrcField, 0))
|
2021-11-02 18:19:31 +00:00
|
|
|
|
2024-06-16 13:28:12 +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})
|
|
|
|
|
2024-06-16 13:28:12 +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.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})
|
|
|
|
|
2024-06-16 13:28:12 +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)
|
|
|
|
}
|
|
|
|
}
|