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/test"
13)
14
15//go:embed test_ressources/*
16var fakeFs embed.FS
17
18type dataTest struct {
19	conf       string
20	inputFile  []string
21	outputFile map[string]string
22	deleteFile []string
23	modFile    []string
24}
25
26var data = []dataTest{{
27	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"}]}`,
28	inputFile:  []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
29	outputFile: map[string]string{"test_ressources/board.md": "test_ressources/board.md", "test_ressources/board2.md": "test_ressources/board_empty.md"},
30}, {
31	conf:       `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "to": "test_ressources/board.md"}]}`,
32	deleteFile: []string{"test_ressources/issue.md"},
33	outputFile: map[string]string{"test_ressources/board.md": "test_ressources/board_delete.md"},
34}, {
35	conf:       `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "table", "to": "test_ressources/boardNotExisting.md", "description": "> Beautiful!!"]}`,
36	inputFile:  []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
37	outputFile: map[string]string{"test_ressources/boardNotExisting.md": "test_ressources/board_table.md"},
38}, {
39	conf:       `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "table", "to": "test_ressources/boardNotExisting2.md", "description": "> Beautiful!!", "selects": ["priority: (\\d+)"]]}`,
40	inputFile:  []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
41	outputFile: map[string]string{"test_ressources/boardNotExisting2.md": "test_ressources/board_table_select.md"},
42}, {
43	conf:       `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "table", "to": "test_ressources/boardNotExisting3.md", "description": "> Beautiful!!", "selects": ["priority2: (\\d+)"]]}`,
44	inputFile:  []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
45	outputFile: map[string]string{"test_ressources/boardNotExisting3.md": "test_ressources/board_table_select_inexistent.md"},
46}, {
47	conf:       `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "task", "to": "test_ressources/boardNotExisting4.md", "description": "> Beautiful!!", "selects": ["(priority:) (\\d+)"]]}`,
48	inputFile:  []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
49	outputFile: map[string]string{"test_ressources/boardNotExisting4.md": "test_ressources/board_task.md"},
50}, {
51	conf:       `{"boards": [{"title": "Roadmaps", "for": "*/*.md", "where": "type: 'test'", "format": "embed", "to": "test_ressources/boardNotExisting5.md", "description": "> Beautiful!!", "selects": ["(priority:) (\\d+)"]]}`,
52	inputFile:  []string{"test_ressources/issue.md", "test_ressources/issue2.md"},
53	outputFile: map[string]string{"test_ressources/boardNotExisting5.md": "test_ressources/board_embed.md"},
54}, {
55	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"}]}`,
56	modFile:    []string{"test_ressources/issue2.md", "test_ressources/close/issue2.md"},
57	outputFile: map[string]string{"test_ressources/board.md": "test_ressources/board_empty.md", "test_ressources/close.md": "test_ressources/board_close.md"},
58}}
59
60func TestIssue(t *testing.T) {
61	calledModifyContent := make(map[string]string)
62	s := test.NewFakeServer(t, test.Override{
63		Worktree: func() fs.FS {
64			return fakeFs
65		},
66		ModifyContent: func(path, content string) {
67			calledModifyContent[path] = content
68		},
69	})
70	for _, d := range data {
71		p := Build(s)
72		p.Init("repoName", false, d.conf)
73		if d.inputFile != nil {
74			for _, fileInput := range d.inputFile {
75				p.AddFile(fileInput)
76			}
77		}
78		if d.modFile != nil {
79			p.ModFile(d.modFile[0], d.modFile[1])
80		}
81		if d.deleteFile != nil {
82			for _, fileInput := range d.deleteFile {
83				p.DelFile(fileInput)
84			}
85		}
86		p.Finish()
87		for outputPath, outputFile := range d.outputFile {
88			res, _ := fs.ReadFile(fakeFs, outputFile)
89			if calledModifyContent[outputPath] != string(res) {
90				t.Errorf("%s= \n---\n%s\n---\n\nwant\n\n---\n%s\n---", outputFile, calledModifyContent[outputPath], string(res))
91			}
92		}
93	}
94}