@@ -22,6 +22,7 @@ package cli
2222import (
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
435442func 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