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)
64	s.SetOverride(test.Override{
65		Worktree: func() *model.GrFs {
66			return model.NewGrFsWithFs(model.FS_BASE_WORKTREE, s, fakeFs)
67		},
68		ModifyContent: func(path, content string) {
69			calledModifyContent[path] = content
70		},
71	})
72	for _, d := range data {
73		p := Build(s)
74		p.Init("repoName", false, d.conf)
75		if d.inputFile != nil {
76			for _, fileInput := range d.inputFile {
77				p.AddFile(model.File{Path: fileInput})
78			}
79		}
80		if d.modFile != nil {
81			p.ModFile(model.File{OldPath: d.modFile[0], Path: d.modFile[1]})
82		}
83		if d.deleteFile != nil {
84			for _, fileInput := range d.deleteFile {
85				p.DelFile(model.File{Path: fileInput})
86			}
87		}
88		p.Finish()
89		for outputPath, outputFile := range d.outputFile {
90			res, _ := fs.ReadFile(fakeFs, outputFile)
91			if calledModifyContent[outputPath] != string(res) {
92				t.Errorf("%s= \n---\n%s\n---\n\nwant\n\n---\n%s\n---", outputFile, calledModifyContent[outputPath], string(res))
93			}
94		}
95	}
96}