-
Notifications
You must be signed in to change notification settings - Fork 2
/
spy.go
120 lines (104 loc) · 3.8 KB
/
spy.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package gobmock
import (
"fmt"
"github.com/tonnerre/golang-text"
)
const unconditionalCallthrough = "📣"
// Produces a bash function with a given name.
// The function will report it's arguments
// as well as any data passed into it via STDIN.
// All reporting messages are sent to STDERR.
// The function is exported for use in child processes
func Spy(name string) Gob {
return &spy{name: name, callThroughCondition: "", shouldExport: true}
}
// Produces a bash function with a given name.
// The function will report it's arguments
// as well as any data passed into it via STDIN.
// All reporting messages are sent to STDERR.
func ShallowSpy(name string) Gob {
return &spy{name: name, callThroughCondition: "", shouldExport: false}
}
// Produces a bash function with a given name.
// The function will report it's arguments
// as well as any data passed into it via STDIN.
// All reporting messages are sent to STDERR.
// This function will also call through to the
// original executable
// The function is exported for use in child processes
func SpyAndCallThrough(name string) Gob {
return &spy{name: name, callThroughCondition: unconditionalCallthrough, shouldExport: true}
}
// Produces a bash function with a given name.
// The function will report it's arguments
// as well as any data passed into it via STDIN.
// All reporting messages are sent to STDERR.
// This function will also call through to the
// original executable
func ShallowSpyAndCallThrough(name string) Gob {
return &spy{name: name, callThroughCondition: unconditionalCallthrough, shouldExport: false}
}
// Produces a bash function with a given name.
// The function will report it's arguments
// as well as any data passed into it via STDIN.
// All reporting messages are sent to STDERR.
// This function will also call through to the
// original executable when a supplied condition is met
// The function is exported for use in child processes
func SpyAndConditionallyCallThrough(name string, callThroughCondition string) Gob {
return &spy{name: name, callThroughCondition: callThroughCondition, shouldExport: true}
}
// Produces a bash function with a given name.
// The function will report it's arguments
// as well as any data passed into it via STDIN.
// All reporting messages are sent to STDERR.
// This function will also call through to the
// original executable when a supplied condition is met
// The function is exported for use in child processes
func ShallowSpyAndConditionallyCallThrough(name string, callThroughCondition string) Gob {
return &spy{name: name, callThroughCondition: callThroughCondition, shouldExport: false}
}
type spy struct {
name string
callThroughCondition string
shouldExport bool
shouldSkipReading bool
}
func (s *spy) MockContents() string {
if s.shouldExport {
return s.spyFunction() + s.spyExport()
}
return s.spyFunction()
}
// Produces a spy with the reading portion disabled.
// It will not consume any data passed into it via STDIN.
func (s *spy) WithoutReading() Gob {
return &spy{
callThroughCondition: s.callThroughCondition,
name: s.name,
shouldExport: s.shouldExport,
shouldSkipReading: true,
}
}
func (s *spy) spyExport() string {
return fmt.Sprintf(exportDefinition, s.name)
}
func (s *spy) spyFunction() string {
script := scriptStart + s.spyDefinition()
if s.callThroughCondition == unconditionalCallthrough {
script = script + callThroughDefinition
} else if s.callThroughCondition != "" {
script = script + s.conditionalCallThrough()
}
return fmt.Sprintf(script+scriptEnd, s.name)
}
func (s *spy) conditionalCallThrough() string {
return text.Indent("\nif "+s.callThroughCondition+"; then\n"+callThroughDefinition+"fi\n", " ")
}
func (s *spy) spyDefinition() string {
if s.shouldSkipReading {
return spyWithoutReadingDefinition
} else {
return spyDefinition
}
}