mirror of
https://github.com/kovetskiy/mark.git
synced 2025-06-02 03:32:42 +08:00
add batch test, test function for continue-on-error flag
This commit is contained in:
parent
7f5144a1d1
commit
d88b81a6b8
@ -1,7 +1,10 @@
|
|||||||
package mark
|
package mark
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -122,3 +125,89 @@ func TestCompileMarkdownStripNewlines(t *testing.T) {
|
|||||||
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
|
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarkBinaryContinueOnError(t *testing.T) {
|
||||||
|
var batchTestsDir string
|
||||||
|
var markExePath string
|
||||||
|
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to get working directory: %v", err)
|
||||||
|
}
|
||||||
|
if filepath.Base(wd) == "markdown" { // when running individual test, "go test -v ./markdown -run TestMarkBinaryContinueOnError" - move up to root project directory
|
||||||
|
wd = filepath.Dir(wd)
|
||||||
|
}
|
||||||
|
|
||||||
|
batchTestsDir = filepath.Join(wd, "testdata/batch-tests")
|
||||||
|
|
||||||
|
markExePath = filepath.Join(wd, "mark-temp")
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
markExePath += ".exe" // add .exe extension on Windows
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("Building temporary mark executable...")
|
||||||
|
buildCmd := exec.Command("go", "build", "-o", markExePath)
|
||||||
|
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test := assert.New(t)
|
||||||
|
test.EqualValues(3, len(errorLines))
|
||||||
|
test.Contains(errorLines[0], "ERROR")
|
||||||
|
test.Contains(errorLines[1], "ERROR")
|
||||||
|
test.Contains(errorLines[2], "ERROR")
|
||||||
|
}
|
6
testdata/batch-tests/errord-test.md
vendored
Normal file
6
testdata/batch-tests/errord-test.md
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
## Foo
|
||||||
|
|
||||||
|
> **TL;DR:** Thingy!
|
||||||
|
> More stuff
|
||||||
|
|
||||||
|
Foo
|
Loading…
x
Reference in New Issue
Block a user