1
1
package templates
2
2
3
3
import (
4
+ "regexp"
4
5
"strings"
5
6
"testing"
6
7
)
@@ -12,6 +13,7 @@ func TestGetDockerfileTemplate(t *testing.T) {
12
13
transportType TransportType
13
14
data TemplateData
14
15
wantContains []string
16
+ wantMatches []string // New field for regex patterns
15
17
wantNotContains []string
16
18
wantErr bool
17
19
}{
@@ -23,11 +25,13 @@ func TestGetDockerfileTemplate(t *testing.T) {
23
25
MCPArgs : []string {"--arg1" , "--arg2" , "value" },
24
26
},
25
27
wantContains : []string {
26
- "FROM python:3.12-slim" ,
27
28
"apt-get install -y --no-install-recommends ca-certificates" ,
28
29
"pip install --no-cache-dir uv" ,
29
30
"ENTRYPOINT [\" uvx\" , \" example-package\" , \" --arg1\" , \" --arg2\" , \" value\" ]" ,
30
31
},
32
+ wantMatches : []string {
33
+ `FROM python:\d+\.\d+-slim` , // Match any Python version
34
+ },
31
35
wantNotContains : []string {
32
36
"Add custom CA certificate" ,
33
37
"update-ca-certificates" ,
@@ -43,7 +47,6 @@ func TestGetDockerfileTemplate(t *testing.T) {
43
47
CACertContent : "-----BEGIN CERTIFICATE-----\n MIICertificateContent\n -----END CERTIFICATE-----" ,
44
48
},
45
49
wantContains : []string {
46
- "FROM python:3.12-slim" ,
47
50
"apt-get install -y --no-install-recommends ca-certificates" ,
48
51
"pip install --no-cache-dir uv" ,
49
52
"ENTRYPOINT [\" uvx\" , \" example-package\" , \" --arg1\" , \" --arg2\" , \" value\" ]" ,
@@ -52,6 +55,9 @@ func TestGetDockerfileTemplate(t *testing.T) {
52
55
"cat /tmp/custom-ca.crt >> /etc/ssl/certs/ca-certificates.crt" ,
53
56
"update-ca-certificates" ,
54
57
},
58
+ wantMatches : []string {
59
+ `FROM python:\d+\.\d+-slim` , // Match any Python version
60
+ },
55
61
wantNotContains : []string {},
56
62
wantErr : false ,
57
63
},
@@ -63,9 +69,11 @@ func TestGetDockerfileTemplate(t *testing.T) {
63
69
MCPArgs : []string {"--arg1" , "--arg2" , "value" },
64
70
},
65
71
wantContains : []string {
66
- "FROM node:22-alpine" ,
67
72
"ENTRYPOINT [\" npx\" , \" --yes\" , \" --\" , \" example-package\" , \" --arg1\" , \" --arg2\" , \" value\" ]" ,
68
73
},
74
+ wantMatches : []string {
75
+ `FROM node:\d+-alpine` , // Match any Node version
76
+ },
69
77
wantNotContains : []string {
70
78
"Add custom CA certificate" ,
71
79
"update-ca-certificates" ,
@@ -81,13 +89,15 @@ func TestGetDockerfileTemplate(t *testing.T) {
81
89
CACertContent : "-----BEGIN CERTIFICATE-----\n MIICertificateContent\n -----END CERTIFICATE-----" ,
82
90
},
83
91
wantContains : []string {
84
- "FROM node:22-alpine" ,
85
92
"ENTRYPOINT [\" npx\" , \" --yes\" , \" --\" , \" example-package\" , \" --arg1\" , \" --arg2\" , \" value\" ]" ,
86
93
"Add custom CA certificate BEFORE any network operations" ,
87
94
"COPY ca-cert.crt /tmp/custom-ca.crt" ,
88
95
"cat /tmp/custom-ca.crt >> /etc/ssl/certs/ca-certificates.crt" ,
89
96
"update-ca-certificates" ,
90
97
},
98
+ wantMatches : []string {
99
+ `FROM node:\d+-alpine` , // Match any Node version
100
+ },
91
101
wantNotContains : []string {},
92
102
wantErr : false ,
93
103
},
@@ -99,9 +109,11 @@ func TestGetDockerfileTemplate(t *testing.T) {
99
109
MCPArgs : []string {"--arg1" , "--arg2" , "value" },
100
110
},
101
111
wantContains : []string {
102
- "FROM golang:1.24-alpine" ,
103
112
"ENTRYPOINT [\" go\" , \" run\" , \" example-package\" , \" --arg1\" , \" --arg2\" , \" value\" ]" ,
104
113
},
114
+ wantMatches : []string {
115
+ `FROM golang:\d+\.\d+-alpine` , // Match any Go version
116
+ },
105
117
wantNotContains : []string {
106
118
"Add custom CA certificate" ,
107
119
"update-ca-certificates" ,
@@ -117,13 +129,15 @@ func TestGetDockerfileTemplate(t *testing.T) {
117
129
CACertContent : "-----BEGIN CERTIFICATE-----\n MIICertificateContent\n -----END CERTIFICATE-----" ,
118
130
},
119
131
wantContains : []string {
120
- "FROM golang:1.24-alpine" ,
121
132
"ENTRYPOINT [\" go\" , \" run\" , \" example-package\" , \" --arg1\" , \" --arg2\" , \" value\" ]" ,
122
133
"Add custom CA certificate BEFORE any network operations" ,
123
134
"COPY ca-cert.crt /tmp/custom-ca.crt" ,
124
135
"cat /tmp/custom-ca.crt >> /etc/ssl/certs/ca-certificates.crt" ,
125
136
"update-ca-certificates" ,
126
137
},
138
+ wantMatches : []string {
139
+ `FROM golang:\d+\.\d+-alpine` , // Match any Go version
140
+ },
127
141
wantNotContains : []string {},
128
142
wantErr : false ,
129
143
},
@@ -136,10 +150,12 @@ func TestGetDockerfileTemplate(t *testing.T) {
136
150
IsLocalPath : true ,
137
151
},
138
152
wantContains : []string {
139
- "FROM golang:1.24-alpine" ,
140
153
"COPY . /app/" ,
141
154
"ENTRYPOINT [\" go\" , \" run\" , \" ./cmd/server\" , \" --arg1\" , \" value\" ]" ,
142
155
},
156
+ wantMatches : []string {
157
+ `FROM golang:\d+\.\d+-alpine` , // Match any Go version
158
+ },
143
159
wantNotContains : []string {
144
160
"Add custom CA certificate" ,
145
161
},
@@ -154,10 +170,12 @@ func TestGetDockerfileTemplate(t *testing.T) {
154
170
IsLocalPath : true ,
155
171
},
156
172
wantContains : []string {
157
- "FROM golang:1.24-alpine" ,
158
173
"COPY . /app/" ,
159
174
"ENTRYPOINT [\" go\" , \" run\" , \" .\" ]" ,
160
175
},
176
+ wantMatches : []string {
177
+ `FROM golang:\d+\.\d+-alpine` , // Match any Go version
178
+ },
161
179
wantNotContains : []string {
162
180
"Add custom CA certificate" ,
163
181
},
@@ -189,12 +207,26 @@ func TestGetDockerfileTemplate(t *testing.T) {
189
207
return
190
208
}
191
209
210
+ // Check for exact string matches
192
211
for _ , want := range tt .wantContains {
193
212
if ! strings .Contains (got , want ) {
194
213
t .Errorf ("GetDockerfileTemplate() = %v, want to contain %v" , got , want )
195
214
}
196
215
}
197
216
217
+ // Check for regex pattern matches
218
+ for _ , pattern := range tt .wantMatches {
219
+ matched , err := regexp .MatchString (pattern , got )
220
+ if err != nil {
221
+ t .Errorf ("Invalid regex pattern %v: %v" , pattern , err )
222
+ continue
223
+ }
224
+ if ! matched {
225
+ t .Errorf ("GetDockerfileTemplate() = %v, want to match pattern %v" , got , pattern )
226
+ }
227
+ }
228
+
229
+ // Check for strings that should not be present
198
230
for _ , notWant := range tt .wantNotContains {
199
231
if strings .Contains (got , notWant ) {
200
232
t .Errorf ("GetDockerfileTemplate() = %v, want NOT to contain %v" , got , notWant )
0 commit comments