-
Notifications
You must be signed in to change notification settings - Fork 61
/
test_c.go
98 lines (77 loc) · 2.08 KB
/
test_c.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
//
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
package integration
import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/newrelic/newrelic-php-agent/daemon/internal/newrelic/log"
)
func (t *Test) IsC() bool {
_, ext := splitExt(t.Path)
return ext == ".c" || ext == ".cpp"
}
func makeCommand() string {
cmd := os.Getenv("MAKE")
if len(cmd) == 0 {
cmd = "make"
}
return cmd
}
func splitExt(s string) (string, string) {
ext := filepath.Ext(s)
base := s[:len(s)-len(ext)]
return base, ext
}
func compileTestFile(test *Test) error {
testname, _ := splitExt(test.Path)
config := strings.Replace(test.Config, "\n", "", -1)
config = strings.Replace(config, "\"", "\\\"", -1)
cmd := exec.Command(makeCommand(), testname)
cmd.Dir = filepath.Dir(test.Path)
cmd.Env = append(os.Environ(),
fmt.Sprintf("NEW_RELIC_DAEMON_TESTNAME=%s", test.Name),
fmt.Sprintf("NEW_RELIC_CONFIG=%s", config))
cmd.Stderr = os.Stderr
return cmd.Run()
}
func parseCTestFile(test *Test) *Test {
// C test files are parsed the same way as PHP test files are. In
// addition, the file is compiled.
test = parsePHPTestFile(test)
if err := compileTestFile(test); err != nil {
test.Fatal(fmt.Errorf("compilation failure: %v", err))
}
return test
}
type CCLI struct {
CLI
}
func CTx(src Script, env, settings map[string]string, headers http.Header, ctx *Context) (Tx, error) {
env["NEW_RELIC_DAEMON_SOCKET"] = strings.Trim(settings["newrelic.daemon.port"], "\"")
env["NEW_RELIC_LICENSE_KEY"] = settings["newrelic.license"]
path := src.Name()
base, _ := splitExt(path)
return &CCLI{
CLI: CLI{
Path: base,
Dir: filepath.Dir(path),
Env: flatten(env),
},
}, nil
}
func (tx *CCLI) Execute() (http.Header, []byte, error) {
header, output, err := tx.CLI.Execute()
// Clean up
cleanCmd := exec.Command(makeCommand())
cleanCmd.Args = append(cleanCmd.Args, fmt.Sprintf("clean_%s", filepath.Base(tx.Path)))
cleanCmd.Dir = tx.Dir
log.Debugf("command: %v", cleanCmd)
cleanCmd.Run()
return header, output, err
}