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