GitRoot
Craft your forge, Build your project, Grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package main
6
7import (
8 "embed"
9 "io/fs"
10 "testing"
11
12 "gitroot.dev/libs/golang/plugin/model"
13 "gitroot.dev/libs/golang/plugin/test"
14)
15
16//go:embed test_ressources/*
17var fakeFs embed.FS
18
19type dataTest struct {
20 conf string
21 inputFile []string
22 outputFile map[string]string
23 deleteFile []string
24 modFile []string
25}
26
27var data = []dataTest{
28 {
29 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "to": "test_ressources/board.md"}, {"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test2'", "to": "test_ressources/board2.md"}]}`,
30 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
31 outputFile: map[string]string{"test_ressources/board.md": "test_ressources/board.md", "test_ressources/board2.md": "test_ressources/board_empty.md"},
32 }, {
33
34 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "to": "test_ressources/board.md"}]}`,
35 deleteFile: []string{"test_ressources/issue.md"},
36 outputFile: map[string]string{"test_ressources/board.md": "test_ressources/board_delete.md"},
37 }, {
38
39 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "table", "to": "test_ressources/boardNotExisting.md", "description": "> Beautiful!!"]}`,
40 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
41 outputFile: map[string]string{"test_ressources/boardNotExisting.md": "test_ressources/board_table.md"},
42 }, {
43
44 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "table", "to": "test_ressources/boardNotExisting2.md", "description": "> Beautiful!!", "selects": ["priority: (\\d+)"], "tableHeader": "| | Long Header |"]}`,
45 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
46 outputFile: map[string]string{"test_ressources/boardNotExisting2.md": "test_ressources/board_table_select.md"},
47 }, {
48
49 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "table", "to": "test_ressources/boardNotExisting3.md", "description": "> Beautiful!!", "selects": ["priority2: (\\d+)"]]}`,
50 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
51 outputFile: map[string]string{"test_ressources/boardNotExisting3.md": "test_ressources/board_table_select_inexistent.md"},
52 }, {
53
54 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "task", "to": "test_ressources/boardNotExisting4.md", "description": "> Beautiful!!", "selects": ["(priority:) (\\d+)"]]}`,
55 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
56 outputFile: map[string]string{"test_ressources/boardNotExisting4.md": "test_ressources/board_task.md"},
57 }, {
58
59 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "embed", "to": "test_ressources/boardNotExisting5.md", "description": "> Beautiful!!", "selects": ["(priority:) (\\d+)"]]}`,
60 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
61 outputFile: map[string]string{"test_ressources/boardNotExisting5.md": "test_ressources/board_embed.md"},
62 }, {
63
64 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "to": "test_ressources/board.md"}, {"title": "Roadmaps", "for": "**/close/*.md", "where": "type: 'test'", "to": "test_ressources/close.md"}]}`,
65 modFile: []string{"test_ressources/issue2.md", "test_ressources/close/issue2.md"},
66 outputFile: map[string]string{"test_ressources/board.md": "test_ressources/board_empty.md", "test_ressources/close.md": "test_ressources/board_close.md"},
67 },
68 {
69 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "alphabetic", "to": "test_ressources/boardNotExisting6.md", "description": "> Beautiful!!", "selects": ["group: \"(.+)\""]]}`,
70 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
71 outputFile: map[string]string{"test_ressources/boardNotExisting6.md": "test_ressources/board_alpha.md"},
72 },
73 {
74 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "alphabetic", "to": "test_ressources/silo_dynamicName_$select[0].md", "description": "> Beautiful!!", "selects": ["group: \"(.+)\""], "groupBy": "title"]}`,
75 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
76 outputFile: map[string]string{
77 "test_ressources/silo_dynamicName_C2.md": "test_ressources/dynamicName_C2.md",
78 "test_ressources/silo_dynamicName_R3.md": "test_ressources/dynamicName_R3.md",
79 },
80 },
81 {
82 conf: `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "sort": "select[1]", "sortOrder": "desc", "format": "custom", "to": "test_ressources/silo_customNotExisting.html", "description": "> Beautiful!!", "selects": ["group: \"(.+)\"", "date: (.+)", "og:image: (.+)"], "metadata": {"tag": "roro"}, "customLayout":"<section><header><h1>{{title}}<small>{{tag}}</small>{{select[1]}}</h1></header>{{select[0]}}<img src=\"{{select[2]}}\"><footer>{{pathHtml}}</footer></section>"]}`,
83 inputFile: []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
84 outputFile: map[string]string{
85 "test_ressources/silo_customNotExisting.html": "test_ressources/silo_custom.html",
86 },
87 },
88}
89
90func TestIssue(t *testing.T) {
91 calledModifyContent := make(map[string]string)
92 s := test.NewFakeServer(t)
93 s.SetOverride(test.Override{
94 Worktree: func() *model.GrFs {
95 return model.NewGrFsWithFs(model.WORKTREE, s, fakeFs)
96 },
97 WriteContent: func(fsBase model.FsBase, path, content string) error {
98 if fsBase == model.WORKTREE {
99 calledModifyContent[path] = content
100 return nil
101 }
102 return test.ErrTryOther
103 },
104 })
105 for _, d := range data {
106 p := Build(s)
107 p.Init("repoName", model.InitKindDiff, d.conf)
108 if d.inputFile != nil {
109 for _, fileInput := range d.inputFile {
110 p.AddFile(model.File{Path: fileInput})
111 }
112 }
113 if d.modFile != nil {
114 p.ModFile(model.File{OldPath: d.modFile[0], Path: d.modFile[1]})
115 }
116 if d.deleteFile != nil {
117 for _, fileInput := range d.deleteFile {
118 p.DelFile(model.File{Path: fileInput})
119 }
120 }
121 p.Finish()
122 for outputPath, outputFile := range d.outputFile {
123 res, err := fs.ReadFile(fakeFs, outputFile)
124 if err != nil {
125 t.Fatal(err)
126 }
127 if calledModifyContent[outputPath] != string(res) {
128 for p := range calledModifyContent {
129 t.Error(p)
130 }
131 t.Errorf("%s= \n---\n%s\n---\n\nwant\n\n---\n%s\n---", outputFile, calledModifyContent[outputPath], string(res))
132 }
133 }
134 }
135}