Skip to content

Commit 10a4443

Browse files
committed
feat: Link the instrumented binary and run it and further check output content
Signed-off-by: yuluo-yx <[email protected]>
1 parent 696aafd commit 10a4443

File tree

1 file changed

+17
-72
lines changed

1 file changed

+17
-72
lines changed

tool/internal/instrument/instrument_test.go

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,23 @@ func TestInstrument(t *testing.T) {
6262
} else {
6363
require.NoError(t, err)
6464
}
65-
// TODO: Link the instrumented binary and run it and further check
66-
// output content
65+
// Build the instrumented binary and run it, then verify output
66+
binPath := filepath.Join(tempDir, "app")
67+
buildCmd := exec.Command("go", "build", "-o", binPath, ".")
68+
buildCmd.Dir = tempDir
69+
buildCmd.Env = append(os.Environ(), "GO111MODULE=off")
70+
out, err := buildCmd.CombinedOutput()
71+
require.NoErrorf(t, err, "go build failed: %s", string(out))
72+
73+
runCmd := exec.Command(binPath)
74+
runtimeOut, err := runCmd.CombinedOutput()
75+
require.NoErrorf(t, err, "running app failed: %s", string(runtimeOut))
76+
77+
output := string(runtimeOut)
78+
// Base output from source.go
79+
require.Contains(t, output, "Hello, World!")
80+
// Output from added raw rule newfile.go(func2)
81+
require.Contains(t, output, "func2")
6782
})
6883
}
6984
}
@@ -112,59 +127,6 @@ func createTestRuleJSON(mainGoFile string) ([]byte, error) {
112127
{
113128
PackageName: "main",
114129
ModulePath: "main",
115-
FuncRules: map[string][]*rule.InstFuncRule{
116-
mainGoFile: {
117-
{
118-
InstBaseRule: rule.InstBaseRule{
119-
Name: "hook_func",
120-
Target: "main",
121-
},
122-
Path: filepath.Join(".", "testdata"),
123-
Func: "Func1",
124-
Before: "H1Before",
125-
After: "H1After",
126-
},
127-
{
128-
InstBaseRule: rule.InstBaseRule{
129-
Name: "hook_same_func",
130-
Target: "main",
131-
},
132-
Path: filepath.Join(".", "testdata"),
133-
Func: "Func1",
134-
Before: "H2Before",
135-
After: "H2After",
136-
},
137-
{
138-
InstBaseRule: rule.InstBaseRule{
139-
Name: "hook_func_with_recv",
140-
Target: "main",
141-
},
142-
Path: filepath.Join(".", "testdata"),
143-
Func: "Func1",
144-
Recv: "*T",
145-
Before: "H3Before",
146-
},
147-
{
148-
InstBaseRule: rule.InstBaseRule{
149-
Name: "hook_func_no_before",
150-
Target: "main",
151-
},
152-
Path: filepath.Join(".", "testdata"),
153-
Func: "Func1",
154-
Recv: "*T",
155-
After: "H3After",
156-
},
157-
{
158-
InstBaseRule: rule.InstBaseRule{
159-
Name: "underscore_param",
160-
Target: "main",
161-
},
162-
Path: filepath.Join(".", "testdata"),
163-
Func: "Func2",
164-
Before: "H4Before",
165-
},
166-
},
167-
},
168130
RawRules: map[string][]*rule.InstRawRule{
169131
mainGoFile: {
170132
{
@@ -177,23 +139,6 @@ func createTestRuleJSON(mainGoFile string) ([]byte, error) {
177139
},
178140
},
179141
},
180-
StructRules: map[string][]*rule.InstStructRule{
181-
mainGoFile: {
182-
{
183-
InstBaseRule: rule.InstBaseRule{
184-
Name: "add_new_field",
185-
Target: "main",
186-
},
187-
Struct: "T",
188-
NewField: []*rule.InstStructField{
189-
{
190-
Name: "NewField",
191-
Type: "string",
192-
},
193-
},
194-
},
195-
},
196-
},
197142
FileRules: []*rule.InstFileRule{
198143
{
199144
InstBaseRule: rule.InstBaseRule{

0 commit comments

Comments
 (0)