Skip to content

Commit d184016

Browse files
committed
newt: Add possibility to run newt tests under Valgrind
This adds flag --valgrind to newt test command. When used the tests are executed in Valgrind.
1 parent cbaa907 commit d184016

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

newt/builder/selftest.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (t *TargetBuilder) SelfTestCreateExe() error {
7777
return nil
7878
}
7979

80-
func (t *TargetBuilder) SelfTestExecute() error {
80+
func (t *TargetBuilder) SelfTestExecute(valgrind bool) error {
8181
if err := t.SelfTestCreateExe(); err != nil {
8282
return err
8383
}
@@ -87,7 +87,7 @@ func (t *TargetBuilder) SelfTestExecute() error {
8787
return err
8888
}
8989

90-
if err := t.AppBuilder.SelfTestExecute(testRpkg); err != nil {
90+
if err := t.AppBuilder.SelfTestExecute(testRpkg, valgrind); err != nil {
9191
return err
9292
}
9393

@@ -140,7 +140,7 @@ func (b *Builder) testOwner(bpkg *BuildPackage) *BuildPackage {
140140
}
141141
}
142142

143-
func (b *Builder) SelfTestExecute(testRpkg *resolve.ResolvePackage) error {
143+
func (b *Builder) SelfTestExecute(testRpkg *resolve.ResolvePackage, valgrind bool) error {
144144
testPath := b.TestExePath()
145145
if err := os.Chdir(filepath.Dir(testPath)); err != nil {
146146
return err
@@ -149,11 +149,16 @@ func (b *Builder) SelfTestExecute(testRpkg *resolve.ResolvePackage) error {
149149
util.StatusMessage(util.VERBOSITY_DEFAULT, "Executing test: %s\n",
150150
testPath)
151151
cmd := []string{testPath}
152-
if _, err := util.ShellCommand(cmd, nil); err != nil {
152+
if valgrind {
153+
cmd = append([]string{"valgrind", "--error-exitcode=1"}, cmd...)
154+
}
155+
if output, err := util.ShellCommand(cmd, nil); err != nil {
153156
newtError := err.(*util.NewtError)
154157
newtError.Text = fmt.Sprintf("Test failure (%s):\n%s",
155158
testRpkg.Lpkg.Name(), newtError.Text)
156159
return newtError
160+
} else if valgrind {
161+
util.StatusMessage(util.VERBOSITY_DEFAULT, "%s", output)
157162
}
158163

159164
return nil

newt/cli/build_cmds.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package cli
2222
import (
2323
"fmt"
2424
"os"
25+
"os/exec"
2526
"path/filepath"
2627
"strings"
2728

@@ -235,11 +236,17 @@ func pkgnames(pkgs []*pkg.LocalPackage) string {
235236
return s
236237
}
237238

238-
func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell bool) {
239+
func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell bool, valgrind bool) {
239240
if len(args) < 1 {
240241
NewtUsage(cmd, nil)
241242
}
242243

244+
_, err := exec.LookPath("valgrind")
245+
if err != nil {
246+
NewtUsage(nil, util.FmtNewtError("Valgrind is not installed."+
247+
"Please install it before running tests with --valgrind flag."))
248+
}
249+
243250
util.ExecuteShell = executeShell
244251

245252
proj := TryGetProject()
@@ -320,7 +327,7 @@ func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell
320327
util.StatusMessage(util.VERBOSITY_DEFAULT, "Testing package %s\n",
321328
pack.FullName())
322329

323-
err = b.SelfTestExecute()
330+
err = b.SelfTestExecute(valgrind)
324331
if err == nil {
325332
passedPkgs = append(passedPkgs, pack)
326333
} else {
@@ -435,6 +442,7 @@ func sizeRunCmd(cmd *cobra.Command, args []string, ram bool, flash bool, section
435442
func AddBuildCommands(cmd *cobra.Command) {
436443
var printShellCmds bool
437444
var executeShell bool
445+
var valgrind bool
438446

439447
buildCmd := &cobra.Command{
440448
Use: "build <target-name> [target-names...]",
@@ -474,12 +482,14 @@ func AddBuildCommands(cmd *cobra.Command) {
474482
Use: "test <package-name> [package-names...] | all",
475483
Short: "Executes unit tests for one or more packages",
476484
Run: func(cmd *cobra.Command, args []string) {
477-
testRunCmd(cmd, args, exclude, executeShell)
485+
testRunCmd(cmd, args, exclude, executeShell, valgrind)
478486
},
479487
}
480488
testCmd.Flags().StringVarP(&exclude, "exclude", "e", "", "Comma separated list of packages to exclude")
481489
testCmd.Flags().BoolVar(&executeShell, "executeShell", false,
482490
"Execute build command using /bin/sh (Linux and MacOS only)")
491+
testCmd.Flags().BoolVar(&valgrind, "valgrind", false,
492+
"Run test executables under Valgrind")
483493
cmd.AddCommand(testCmd)
484494
AddTabCompleteFn(testCmd, func() []string {
485495
return append(testablePkgList(), "all", "allexcept")

0 commit comments

Comments
 (0)