mark/markdown/markdown_test.go

173 lines
4.1 KiB
Go
Raw Normal View History

2021-02-02 17:12:08 +03:00
package mark
import (
2023-01-03 18:54:04 +01:00
"os"
2024-09-26 15:24:39 +02:00
"path"
2021-02-02 17:12:08 +03:00
"path/filepath"
2024-09-26 15:24:39 +02:00
"runtime"
2021-02-02 17:12:08 +03:00
"strings"
"testing"
2024-09-26 15:24:39 +02:00
"github.com/kovetskiy/mark/stdlib"
2021-02-02 17:12:08 +03:00
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
2021-02-02 17:12:08 +03:00
)
2023-09-06 16:19:09 -07:00
func loadData(t *testing.T, filename, variant string) ([]byte, string, []byte) {
t.Helper()
basename := filepath.Base(filename)
testname := strings.TrimSuffix(basename, ".md")
htmlname := filepath.Join(filepath.Dir(filename), testname+variant+".html")
markdown, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
html, err := os.ReadFile(htmlname)
if err != nil {
panic(err)
}
return markdown, htmlname, html
}
2021-02-02 17:12:08 +03:00
func TestCompileMarkdown(t *testing.T) {
2024-09-26 15:24:39 +02:00
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "..")
err := os.Chdir(dir)
if err != nil {
panic(err)
}
2021-02-02 17:12:08 +03:00
test := assert.New(t)
testcases, err := filepath.Glob("testdata/*.md")
if err != nil {
panic(err)
}
for _, filename := range testcases {
2023-09-06 16:19:09 -07:00
lib, err := stdlib.New(nil)
2021-02-02 17:12:08 +03:00
if err != nil {
panic(err)
}
2023-09-06 16:19:09 -07:00
markdown, htmlname, html := loadData(t, filename, "")
actual, _ := CompileMarkdown(markdown, lib, filename, "", 1.0, false, false)
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
}
}
func TestCompileMarkdownDropH1(t *testing.T) {
2024-09-26 15:24:39 +02:00
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "..")
err := os.Chdir(dir)
if err != nil {
panic(err)
}
2023-09-06 16:19:09 -07:00
test := assert.New(t)
testcases, err := filepath.Glob("testdata/*.md")
2023-09-06 16:19:09 -07:00
if err != nil {
panic(err)
}
for _, filename := range testcases {
lib, err := stdlib.New(nil)
2021-02-02 17:12:08 +03:00
if err != nil {
panic(err)
}
2024-09-29 00:03:26 +02:00
var variant string
switch filename {
case "testdata/quotes.md", "testdata/header.md":
variant = "-droph1"
default:
variant = ""
}
markdown, htmlname, html := loadData(t, filename, variant)
2023-09-06 16:19:09 -07:00
actual, _ := CompileMarkdown(markdown, lib, filename, "", 1.0, true, false)
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
}
}
func TestCompileMarkdownStripNewlines(t *testing.T) {
2024-09-26 15:24:39 +02:00
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "..")
err := os.Chdir(dir)
if err != nil {
panic(err)
}
2023-09-06 16:19:09 -07:00
test := assert.New(t)
2021-02-02 17:12:08 +03:00
testcases, err := filepath.Glob("testdata/*.md")
2023-09-06 16:19:09 -07:00
if err != nil {
panic(err)
}
for _, filename := range testcases {
2021-02-02 17:12:08 +03:00
lib, err := stdlib.New(nil)
if err != nil {
panic(err)
}
2024-09-29 00:03:26 +02:00
var variant string
switch filename {
case "testdata/quotes.md", "testdata/codes.md", "testdata/newlines.md", "testdata/macro-include.md":
variant = "-stripnewlines"
default:
variant = ""
}
2024-09-29 00:03:26 +02:00
markdown, htmlname, html := loadData(t, filename, variant)
2023-09-06 16:19:09 -07:00
actual, _ := CompileMarkdown(markdown, lib, filename, "", 1.0, false, true)
2021-02-02 17:12:08 +03:00
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
}
}
func TestContinueOnError(t *testing.T) {
const (
markFileName = "temp-mark"
)
var flags = []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "files",
Aliases: []string{"f"},
Value: "",
Usage: "use specified markdown file(s) for converting to html. Supports file globbing patterns (needs to be quoted).",
TakesFile: true,
EnvVars: []string{"MARK_FILES"},
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "continue-on-error",
Value: false,
Usage: "don't exit if an error occurs while processing a file, continue processing remaining files.",
EnvVars: []string{"MARK_CONTINUE_ON_ERROR"},
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "compile-only",
Value: false,
Usage: "show resulting HTML and don't update Confluence page content.",
EnvVars: []string{"MARK_COMPILE_ONLY"},
}),
}
app := &cli.App{
Name: markFileName,
Flags: flags,
EnableBashCompletion: true,
HideHelpCommand: true,
Action: RunMark,
}
filePath := filepath.Join("testdata", "batch-tests", "*.md")
argList := []string{
"--compile-only", "--continue-on-error", "-files", filePath,
}
err := app.Run(argList)
test := assert.New(t)
test.NoError(err, "App should run without errors when continue-on-error is enabled")
}