spud/service_definition/fetcher_test.go

59 lines
1.3 KiB
Go
Raw Normal View History

package service_definition
import (
"os"
"strings"
"testing"
)
type MockGit struct {
calls []string
}
func (g *MockGit) Clone(path string, destination string) (string, error) {
g.calls = append(g.calls, path+":"+destination)
return path, nil
}
func TestGetDefinitionDetectsGitPathPrefix(t *testing.T) {
fetcher := NewDefinitionFetcher()
mockGit := MockGit{}
fetcher.Git = &mockGit
fetcher.GetDefinition("git+https://git.com/owner/repo.git")
if len(mockGit.calls) == 0 {
t.Errorf("Expected at least one call to git, got none.")
}
}
func TestGetDefinitionDefaultsToFilePathIfNoPrefix(t *testing.T) {
defPath := t.TempDir() + "/service.yml"
os.WriteFile(defPath, []byte("name: test-service"), 0755)
fetcher := NewDefinitionFetcher()
def, _ := fetcher.GetDefinition(defPath)
if def.Name != "test-service" {
t.Errorf("Expected mock service name to be 'test-service', got %s instead.", def.Name)
}
}
func TestGetDefinitionGetDefinitionFromGit(t *testing.T) {
fetcher := NewDefinitionFetcher()
mockGit := MockGit{}
fetcher.Git = &mockGit
mockUrl := "https://git.com/owner/repo.git"
fetcher.GetDefinition("git+" + mockUrl)
if !strings.HasPrefix(mockGit.calls[0], mockUrl) {
t.Errorf("Expected git cloning for %s, got %s instead.", mockUrl, mockGit.calls[0])
}
}