spud/service_definition/fetcher_test.go
Marc Cataford 5a18a5bf44
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m6s
Pull-Request / static-analysis (pull_request) Successful in 1m31s
Pull-Request / post-run (pull_request) Successful in 24s
Push / pre-run (push) Successful in 28s
Push / tests (push) Successful in 1m3s
Push / static-analysis (push) Successful in 1m29s
Push / post-run (push) Successful in 38s
refactor: isolate + inject DefinitionFetcher
docs: add documentation to core functions and structs
2024-11-10 23:05:57 -05:00

58 lines
1.3 KiB
Go

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])
}
}