157 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			157 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package config
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"os"
 | |
| 	"reflect"
 | |
| 	"testing"
 | |
| )
 | |
| 
 | |
| func TestNotSupportedFileExcension(t *testing.T) {
 | |
| 
 | |
| 	_, err := GetRepositoryConfig([]byte("test"), "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) {
 | |
| 
 | |
| 	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"
 | |
| `)
 | |
| 
 | |
| 	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"},
 | |
| 			},
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	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)
 | |
| 	}
 | |
| }
 |