-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock_test.go
92 lines (71 loc) · 2.46 KB
/
mock_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package exec
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestMock(t *testing.T) {
doc := &mockDocument{"/", nil, "a", nil}
subject := Mock(true, "content")(doc)
mock, ok := subject.(*MockExec)
require.True(t, ok)
assert.True(t, mock.ShouldFail)
assert.Equal(t, "content", mock.ResultContents)
assert.Equal(t, doc, mock.baseExec.doc)
}
func TestMock_Run_extractError(t *testing.T) {
doc := &mockDocument{"/", io.ErrClosedPipe, "a", nil}
subject := Mock(true, "content")(doc)
err := subject.Run(bg, zap.NewNop())
require.EqualError(t, err, "invalid document: "+io.ErrClosedPipe.Error())
}
func TestMock_Run_invalidMainfilePanics(t *testing.T) {
doc := &mockDocument{"/", nil, "a", nil} // doc.main is malformed
subject := Mock(true, "content")(doc)
require.PanicsWithValue(t, "malformed input file: missing extension",
func() { _ = subject.Run(bg, zap.NewNop()) })
}
func TestMock_Run_noAddFilePanics(t *testing.T) {
doc := &mockDocument{"/", nil, "a.tex", nil} // doesn't implement AddFile
subject := Mock(true, "content")(doc)
require.PanicsWithValue(t, "can't add files to document",
func() { _ = subject.Run(bg, zap.NewNop()) })
}
type mockDockumentWithAddFile struct {
mock.Mock
*mockDocument
}
func (m *mockDockumentWithAddFile) AddFile(name, contents string) error {
args := m.Called(name, contents)
return args.Error(0)
}
func TestMock_Run_errorOnAddFilePanics(t *testing.T) {
doc := &mockDockumentWithAddFile{
mockDocument: &mockDocument{"/", nil, "a.tex", nil},
}
subject := Mock(true, "content")(doc)
doc.On("AddFile", "a.log", "content").Return(io.ErrClosedPipe)
require.PanicsWithError(t, "failed to store result file: "+io.ErrClosedPipe.Error(),
func() { _ = subject.Run(bg, zap.NewNop()) })
}
func TestMock_Run_shouldFailCapturesLog(t *testing.T) {
doc := &mockDockumentWithAddFile{
mockDocument: &mockDocument{"/", nil, "a.tex", nil},
}
subject := Mock(true, "content")(doc)
doc.On("AddFile", "a.log", "content").Return(nil)
err := subject.Run(bg, zap.NewNop())
require.EqualError(t, err, "compilation failed")
}
func TestMock_Run_shouldFailCapturesResult(t *testing.T) {
doc := &mockDockumentWithAddFile{
mockDocument: &mockDocument{"/", nil, "a.tex", nil},
}
subject := Mock(false, "%PDF/1.5")(doc)
doc.On("AddFile", "a.pdf", "%PDF/1.5").Return(nil)
err := subject.Run(bg, zap.NewNop())
require.NoError(t, err)
}