-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal_test.go
75 lines (65 loc) · 1.74 KB
/
local_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package exec
import (
"context"
"io"
"os"
"path"
"path/filepath"
"testing"
"github.com/digineo/texd/tex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestLocalExec_Run(t *testing.T) {
t.Parallel()
require := require.New(t)
// create tempdir
tmpDir, err := os.MkdirTemp("/tmp", "texd")
require.NoError(err)
defer os.RemoveAll(tmpDir)
// fill tempdir
err = os.WriteFile(path.Join(tmpDir, "main.tex"), []byte("hello world"), 0o600)
require.NoError(err)
latexmkPath, err := filepath.Abs("testdata/latexmk")
require.NoError(err)
tests := []struct {
doc Document
path string
expectedErr string
expectedOutput string
}{
{
doc: &mockDocument{tmpDir, nil, "main.tex", nil},
path: "/bin/true",
},
{
doc: &mockDocument{"", io.ErrClosedPipe, "main.tex", nil},
path: "/bin/false",
expectedErr: "invalid document: io: read/write on closed pipe",
},
{
doc: &mockDocument{tmpDir, nil, "main.tex", nil},
path: latexmkPath,
expectedErr: "compilation failed: exit status 23",
expectedOutput: tmpDir + " -cd -silent -pv- -pvc- -pdfxe main.tex\n",
},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
// create local exec
exec := LocalExec(tt.doc).(*localExec) //nolint:forcetypeassert
exec.path = tt.path
err := exec.Run(context.Background(), zap.NewNop())
if tt.expectedErr == "" {
assert.NoError(t, err)
} else if assert.EqualError(t, err, tt.expectedErr) {
cErr := err.(*tex.ErrWithCategory) //nolint:forcetypeassert
if tt.expectedOutput != "" {
assert.Equal(t, tt.expectedOutput, cErr.Extra()["output"])
}
}
})
}
require.NoError(err)
}