Skip to content

Commit 3723a9c

Browse files
committed
Add tests for Kitfile generation with variable depth
Signed-off-by: Angel Misevski <amisevsk@gmail.com>
1 parent 40a0590 commit 3723a9c

3 files changed

Lines changed: 280 additions & 1 deletion

File tree

pkg/lib/kitfile/generate/generate_test.go

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package generate
1818

1919
import (
2020
"testing"
21+
22+
"github.com/kitops-ml/kitops/pkg/artifact"
23+
"github.com/stretchr/testify/assert"
2124
)
2225

2326
func TestDetermineFileType(t *testing.T) {
@@ -131,3 +134,275 @@ func TestDetermineFileType(t *testing.T) {
131134
})
132135
}
133136
}
137+
138+
func TestGenerateWithDepth(t *testing.T) {
139+
testDirListing := DirectoryListing{
140+
Name: "root-dir",
141+
Path: "root-dir",
142+
Files: []FileListing{
143+
{Name: "root-one", Path: "root-one.md", Size: 100},
144+
{Name: "root-two", Path: "root-two.md", Size: 100},
145+
{Name: "root-three", Path: "root-three.md", Size: 100},
146+
},
147+
Subdirs: []DirectoryListing{
148+
{
149+
Name: "subdir-one",
150+
Path: "subdir-one",
151+
Files: []FileListing{
152+
{Name: "subdir-one-one", Path: "subdir-one/subdir-one-one.md", Size: 100},
153+
{Name: "subdir-one-two", Path: "subdir-one/subdir-one-two.md", Size: 100},
154+
{Name: "subdir-one-three", Path: "subdir-one/subdir-one-three.md", Size: 100},
155+
},
156+
Subdirs: []DirectoryListing{
157+
{
158+
Name: "subdir-two",
159+
Path: "subdir-one/subdir-two",
160+
Files: []FileListing{
161+
{Name: "subdir-two-one", Path: "subdir-one/subdir-two/subdir-two-one.md", Size: 100},
162+
{Name: "subdir-two-two", Path: "subdir-one/subdir-two/subdir-two-two.md", Size: 100},
163+
{Name: "subdir-two-three", Path: "subdir-one/subdir-two/subdir-two-three.md", Size: 100},
164+
},
165+
Subdirs: []DirectoryListing{
166+
{
167+
Name: "subdir-three",
168+
Path: "subdir-one/subdir-two/subdir-three",
169+
Files: []FileListing{
170+
{Name: "subdir-three-one", Path: "subdir-one/subdir-two/subdir-three/subdir-three-one.md", Size: 100},
171+
{Name: "subdir-three-two", Path: "subdir-one/subdir-two/subdir-three/subdir-three-two.md", Size: 100},
172+
{Name: "subdir-three-three", Path: "subdir-one/subdir-two/subdir-three/subdir-three-three.md", Size: 100},
173+
},
174+
},
175+
},
176+
},
177+
},
178+
},
179+
},
180+
}
181+
182+
t.Run("Depth zero Kitfile", func(t *testing.T) {
183+
kitfile, err := GenerateKitfile(&testDirListing, nil, 0)
184+
assert.NoError(t, err)
185+
expectedDocs := []artifact.Docs{
186+
{Path: "root-one.md"},
187+
{Path: "root-two.md"},
188+
{Path: "root-three.md"},
189+
}
190+
for _, expectedDoc := range expectedDocs {
191+
assert.Contains(t, kitfile.Docs, expectedDoc)
192+
}
193+
assert.Contains(t, kitfile.Code, artifact.Code{Path: "subdir-one/"})
194+
})
195+
196+
t.Run("Depth one Kitfile", func(t *testing.T) {
197+
kitfile, err := GenerateKitfile(&testDirListing, nil, 1)
198+
assert.NoError(t, err)
199+
expectedDocs := []artifact.Docs{
200+
{Path: "root-one.md"},
201+
{Path: "root-two.md"},
202+
{Path: "root-three.md"},
203+
{Path: "subdir-one/subdir-one-one.md"},
204+
{Path: "subdir-one/subdir-one-two.md"},
205+
{Path: "subdir-one/subdir-one-three.md"},
206+
}
207+
for _, expectedDoc := range expectedDocs {
208+
assert.Contains(t, kitfile.Docs, expectedDoc)
209+
}
210+
assert.Contains(t, kitfile.Code, artifact.Code{Path: "subdir-one/subdir-two/"})
211+
})
212+
213+
t.Run("Depth two Kitfile", func(t *testing.T) {
214+
kitfile, err := GenerateKitfile(&testDirListing, nil, 2)
215+
assert.NoError(t, err)
216+
expectedDocs := []artifact.Docs{
217+
{Path: "root-one.md"},
218+
{Path: "root-two.md"},
219+
{Path: "root-three.md"},
220+
{Path: "subdir-one/subdir-one-one.md"},
221+
{Path: "subdir-one/subdir-one-two.md"},
222+
{Path: "subdir-one/subdir-one-three.md"},
223+
{Path: "subdir-one/subdir-two/subdir-two-one.md"},
224+
{Path: "subdir-one/subdir-two/subdir-two-two.md"},
225+
{Path: "subdir-one/subdir-two/subdir-two-three.md"},
226+
}
227+
for _, expectedDoc := range expectedDocs {
228+
assert.Contains(t, kitfile.Docs, expectedDoc)
229+
}
230+
assert.Contains(t, kitfile.Code, artifact.Code{Path: "subdir-one/subdir-two/subdir-three/"})
231+
})
232+
233+
t.Run("Depth three Kitfile", func(t *testing.T) {
234+
kitfile, err := GenerateKitfile(&testDirListing, nil, 3)
235+
assert.NoError(t, err)
236+
expectedDocs := []artifact.Docs{
237+
{Path: "root-one.md"},
238+
{Path: "root-two.md"},
239+
{Path: "root-three.md"},
240+
{Path: "subdir-one/subdir-one-one.md"},
241+
{Path: "subdir-one/subdir-one-two.md"},
242+
{Path: "subdir-one/subdir-one-three.md"},
243+
{Path: "subdir-one/subdir-two/subdir-two-one.md"},
244+
{Path: "subdir-one/subdir-two/subdir-two-two.md"},
245+
{Path: "subdir-one/subdir-two/subdir-two-three.md"},
246+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-one.md"},
247+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-two.md"},
248+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-three.md"},
249+
}
250+
for _, expectedDoc := range expectedDocs {
251+
assert.Contains(t, kitfile.Docs, expectedDoc)
252+
}
253+
assert.Len(t, kitfile.Code, 0, "Should not contain any code layers")
254+
})
255+
256+
t.Run("Full depth Kitfile", func(t *testing.T) {
257+
kitfile, err := GenerateKitfile(&testDirListing, nil, -1)
258+
assert.NoError(t, err)
259+
expectedDocs := []artifact.Docs{
260+
{Path: "root-one.md"},
261+
{Path: "root-two.md"},
262+
{Path: "root-three.md"},
263+
{Path: "subdir-one/subdir-one-one.md"},
264+
{Path: "subdir-one/subdir-one-two.md"},
265+
{Path: "subdir-one/subdir-one-three.md"},
266+
{Path: "subdir-one/subdir-two/subdir-two-one.md"},
267+
{Path: "subdir-one/subdir-two/subdir-two-two.md"},
268+
{Path: "subdir-one/subdir-two/subdir-two-three.md"},
269+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-one.md"},
270+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-two.md"},
271+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-three.md"},
272+
}
273+
for _, expectedDoc := range expectedDocs {
274+
assert.Contains(t, kitfile.Docs, expectedDoc)
275+
}
276+
assert.Len(t, kitfile.Code, 0, "Should not contain any code layers")
277+
})
278+
}
279+
280+
func TestGenerateReadmeAndLicenseInSubdirs(t *testing.T) {
281+
testDirListing := DirectoryListing{
282+
Name: "root-dir",
283+
Path: "root-dir",
284+
Files: []FileListing{
285+
{Name: "README.md", Path: "README.md", Size: 100},
286+
{Name: "LICENSE", Path: "LICENSE", Size: 100},
287+
},
288+
Subdirs: []DirectoryListing{
289+
{
290+
Name: "subdir-one",
291+
Path: "subdir-one",
292+
Files: []FileListing{
293+
{Name: "README.md", Path: "subdir-one/README.md", Size: 100},
294+
{Name: "LICENSE", Path: "subdir-one/LICENSE", Size: 100},
295+
},
296+
Subdirs: []DirectoryListing{
297+
{
298+
Name: "subdir-two",
299+
Path: "subdir-one/subdir-two",
300+
Files: []FileListing{
301+
{Name: "README.md", Path: "subdir-one/subdir-two/README.md", Size: 100},
302+
{Name: "LICENSE", Path: "subdir-one/subdir-two/LICENSE", Size: 100},
303+
},
304+
},
305+
},
306+
},
307+
},
308+
}
309+
310+
t.Run("Depth zero only includes root README and LICENSE", func(t *testing.T) {
311+
kitfile, err := GenerateKitfile(&testDirListing, nil, 0)
312+
assert.NoError(t, err)
313+
assert.Len(t, kitfile.Docs, 2)
314+
assert.Contains(t, kitfile.Docs, artifact.Docs{Path: "README.md", Description: "Readme file"})
315+
assert.Contains(t, kitfile.Docs, artifact.Docs{Path: "LICENSE", Description: "License file"})
316+
})
317+
318+
t.Run("Depth one includes subdir README and LICENSE with full path", func(t *testing.T) {
319+
kitfile, err := GenerateKitfile(&testDirListing, nil, 1)
320+
assert.NoError(t, err)
321+
assert.Len(t, kitfile.Docs, 4)
322+
assert.Contains(t, kitfile.Docs, artifact.Docs{Path: "README.md", Description: "Readme file"})
323+
assert.Contains(t, kitfile.Docs, artifact.Docs{Path: "LICENSE", Description: "License file"})
324+
assert.Contains(t, kitfile.Docs, artifact.Docs{Path: "subdir-one/README.md", Description: "Readme file"})
325+
assert.Contains(t, kitfile.Docs, artifact.Docs{Path: "subdir-one/LICENSE", Description: "License file"})
326+
})
327+
328+
t.Run("Full depth includes all README and LICENSE files with full paths", func(t *testing.T) {
329+
kitfile, err := GenerateKitfile(&testDirListing, nil, -1)
330+
assert.NoError(t, err)
331+
expectedDocs := []artifact.Docs{
332+
{Path: "README.md", Description: "Readme file"},
333+
{Path: "LICENSE", Description: "License file"},
334+
{Path: "subdir-one/README.md", Description: "Readme file"},
335+
{Path: "subdir-one/LICENSE", Description: "License file"},
336+
{Path: "subdir-one/subdir-two/README.md", Description: "Readme file"},
337+
{Path: "subdir-one/subdir-two/LICENSE", Description: "License file"},
338+
}
339+
for _, expected := range expectedDocs {
340+
assert.Contains(t, kitfile.Docs, expected)
341+
}
342+
})
343+
}
344+
345+
func TestGenerateModelPartsDepth(t *testing.T) {
346+
testDirListing := DirectoryListing{
347+
Name: "root-dir",
348+
Path: "root-dir",
349+
Files: []FileListing{
350+
{Name: "root-one", Path: "root-one.onnx", Size: 100},
351+
{Name: "root-two", Path: "root-two.onnx", Size: 100},
352+
{Name: "root-meta", Path: "root-meta.json", Size: 100},
353+
},
354+
Subdirs: []DirectoryListing{
355+
{
356+
Name: "subdir-one",
357+
Path: "subdir-one",
358+
Files: []FileListing{
359+
{Name: "subdir-one-one", Path: "subdir-one/subdir-one-one.onnx", Size: 100},
360+
{Name: "subdir-one-two", Path: "subdir-one/subdir-one-two.onnx", Size: 100},
361+
{Name: "subdir-one-meta", Path: "subdir-one/subdir-one-meta.json", Size: 100},
362+
},
363+
Subdirs: []DirectoryListing{
364+
{
365+
Name: "subdir-two",
366+
Path: "subdir-one/subdir-two",
367+
Files: []FileListing{
368+
{Name: "subdir-two-one", Path: "subdir-one/subdir-two/subdir-two-one.onnx", Size: 100},
369+
{Name: "subdir-two-two", Path: "subdir-one/subdir-two/subdir-two-two.onnx", Size: 100},
370+
{Name: "subdir-two-meta", Path: "subdir-one/subdir-two/subdir-two-meta.json", Size: 100},
371+
},
372+
Subdirs: []DirectoryListing{
373+
{
374+
Name: "subdir-three",
375+
Path: "subdir-one/subdir-two/subdir-three",
376+
Files: []FileListing{
377+
{Name: "subdir-three-one", Path: "subdir-one/subdir-two/subdir-three/subdir-three-one.onnx", Size: 100},
378+
{Name: "subdir-three-two", Path: "subdir-one/subdir-two/subdir-three/subdir-three-two.onnx", Size: 100},
379+
{Name: "subdir-three-meta", Path: "subdir-one/subdir-two/subdir-three/subdir-three-meta.json", Size: 100},
380+
},
381+
},
382+
},
383+
},
384+
},
385+
},
386+
},
387+
}
388+
kitfile, err := GenerateKitfile(&testDirListing, nil, -1)
389+
assert.NoError(t, err)
390+
assert.Equal(t, "root-one.onnx", kitfile.Model.Path)
391+
assert.Len(t, kitfile.Model.Parts, 11)
392+
expectedModelParts := []artifact.ModelPart{
393+
{Path: "root-two.onnx"},
394+
{Path: "subdir-one/subdir-one-one.onnx"},
395+
{Path: "subdir-one/subdir-one-two.onnx"},
396+
{Path: "subdir-one/subdir-two/subdir-two-one.onnx"},
397+
{Path: "subdir-one/subdir-two/subdir-two-two.onnx"},
398+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-one.onnx"},
399+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-two.onnx"},
400+
{Path: "root-meta.json"},
401+
{Path: "subdir-one/subdir-one-meta.json"},
402+
{Path: "subdir-one/subdir-two/subdir-two-meta.json"},
403+
{Path: "subdir-one/subdir-two/subdir-three/subdir-three-meta.json"},
404+
}
405+
for _, expectedModelPart := range expectedModelParts {
406+
assert.Contains(t, kitfile.Model.Parts, expectedModelPart)
407+
}
408+
}

testing/testdata/kitfile-generation/test_directory-handling.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ expectedKitfile:
4444
code:
4545
- path: build/
4646
- path: lib/
47+
- path: mixed-contents/
4748
- path: pkg/
4849
- path: src/
49-
- path: mixed-contents/

testing/testdata/kitfile-generation/test_type-detect.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ files:
1717
# Generic code files that should be caught in a catch-all section
1818
- test.sh
1919
- bootstrap.sh
20+
- main.sh
21+
- common.sh
22+
- utility.sh
23+
- shared.sh
2024

2125
modelName: test-model-detect
2226
expectedKitfile:

0 commit comments

Comments
 (0)