change COE flag testing logic to use internal functions

This commit is contained in:
iyz 2025-03-08 17:42:08 -05:00 committed by Manuel Rüger
parent d88b81a6b8
commit 87160e8dd6

View File

@ -1,10 +1,7 @@
package mark package mark
import ( import (
"bufio"
"bytes"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -13,6 +10,8 @@ import (
"github.com/kovetskiy/mark/stdlib" "github.com/kovetskiy/mark/stdlib"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
) )
func loadData(t *testing.T, filename, variant string) ([]byte, string, []byte) { func loadData(t *testing.T, filename, variant string) ([]byte, string, []byte) {
@ -126,88 +125,48 @@ func TestCompileMarkdownStripNewlines(t *testing.T) {
} }
} }
func TestMarkBinaryContinueOnError(t *testing.T) { func TestContinueOnError(t *testing.T) {
var batchTestsDir string const (
var markExePath string markFileName = "temp-mark"
)
wd, err := os.Getwd() var flags = []cli.Flag{
if err != nil { altsrc.NewStringFlag(&cli.StringFlag{
t.Fatalf("Failed to get working directory: %v", err) Name: "files",
} Aliases: []string{"f"},
if filepath.Base(wd) == "markdown" { // when running individual test, "go test -v ./markdown -run TestMarkBinaryContinueOnError" - move up to root project directory Value: "",
wd = filepath.Dir(wd) 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"},
}),
} }
batchTestsDir = filepath.Join(wd, "testdata/batch-tests") app := &cli.App{
Name: markFileName,
markExePath = filepath.Join(wd, "mark-temp") Flags: flags,
if runtime.GOOS == "windows" { EnableBashCompletion: true,
markExePath += ".exe" // add .exe extension on Windows HideHelpCommand: true,
Action: RunMark,
} }
t.Log("Building temporary mark executable...") filePath := filepath.Join("testdata", "batch-tests", "*.md")
buildCmd := exec.Command("go", "build", "-o", markExePath) argList := []string{
"--compile-only", "--continue-on-error", "-files", filePath,
var buildOutput bytes.Buffer
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build mark executable: %v\nOutput: %s", err, buildOutput.String())
}
if _, err := os.Stat(markExePath); err != nil {
t.Fatalf("Test executable not found at %s: %v", markExePath, err)
}
if runtime.GOOS != "windows" {
if err := os.Chmod(markExePath, 0755); err != nil {
t.Fatalf("Failed to make test executable executable: %v", err)
}
}
defer os.Remove(markExePath) // remove created temporary executable at end of test
t.Logf("Using temporary executable: %s", markExePath)
t.Logf("Using batch tests directory: %s", batchTestsDir)
filePath := filepath.Join(batchTestsDir, "*.md")
cmd := exec.Command(markExePath, "--compile-only", "--continue-on-error", "-files", filePath)
t.Logf("Using file pattern: %s", filePath)
t.Logf("Command: %s %s", cmd.Path, strings.Join(cmd.Args[1:], " "))
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
t.Log("Running command...")
err = cmd.Run()
if err != nil {
t.Logf("Command exited with error: %v", err)
if _, ok := err.(*exec.ExitError); !ok {
t.Fatalf("Failed to run mark binary: %v", err)
}
}
combinedOutput := stdout.String() + stderr.String()
var errorLines []string
processedFiles := 0
t.Log("Complete output:")
t.Log(combinedOutput)
scanner := bufio.NewScanner(strings.NewReader(combinedOutput))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "ERROR") {
errorLines = append(errorLines, line)
}
if strings.Contains(line, "processing") {
processedFiles++;
}
} }
err := app.Run(argList)
test := assert.New(t) test := assert.New(t)
test.EqualValues(3, len(errorLines)) test.NoError(err, "App should run without errors when continue-on-error is enabled")
test.Contains(errorLines[0], "ERROR")
test.Contains(errorLines[1], "ERROR")
test.Contains(errorLines[2], "ERROR")
} }