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