-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembed_func_test.go
76 lines (69 loc) · 2.03 KB
/
embed_func_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package templit_test
import (
"fmt"
"os"
"testing"
"github.com/euforic/templit"
"github.com/google/go-cmp/cmp"
)
// TestEmbedFunc tests the EmbedFunc function.
func TestEmbedFunc(t *testing.T) {
tests := []struct {
name string
repoAndPath string
ctx interface{}
expectedFile string
expectedText string
expectedError error
}{
{
name: "Valid template fetch and execute",
repoAndPath: "https://test_data/templates/basic_test/greeting.txt@main",
ctx: map[string]string{"Name": "John"},
expectedFile: "test_data/outputs/basic_test/greeting.txt",
},
{
name: "Valid template fetch and execute block",
repoAndPath: "https://test_data/templates/basic_test/-block.txt#example_block@main",
ctx: map[string]string{"Greeting": "Hey"},
expectedText: "Hey, this is an example block.",
},
{
name: "Invalid repo path format",
repoAndPath: "invalidpath",
ctx: nil,
expectedText: "",
expectedError: fmt.Errorf("invalid path format in embed URL"),
},
{
name: "Invalid repo",
repoAndPath: "https://localhost/owner/invalidrepo/greeting.txt@main",
ctx: nil,
expectedFile: "",
expectedError: fmt.Errorf("failed to clone repo: open localhost/owner/invalidrepo: no such file or directory"),
},
}
client := &MockGitClient{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executor := templit.NewExecutor(client)
result, err := executor.EmbedFunc(tt.repoAndPath, tt.ctx)
if err != nil {
if tt.expectedError == nil || err.Error() != tt.expectedError.Error() {
t.Fatalf("expected error %v, got %v", tt.expectedError, err)
}
return
}
var expected string
if tt.expectedFile != "" {
expectedBytes, _ := os.ReadFile(tt.expectedFile)
expected = string(expectedBytes)
} else {
expected = tt.expectedText
}
if diff := cmp.Diff(string(expected), result); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
})
}
}