Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
m-gorecki committed Jun 27, 2025
commit 66f47dbfab19e806b3590f41b73917d814b89dc8
13 changes: 9 additions & 4 deletions newt/builder/selftest.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (t *TargetBuilder) SelfTestCreateExe() error {
return nil
}

func (t *TargetBuilder) SelfTestExecute() error {
func (t *TargetBuilder) SelfTestExecute(valgrind bool) error {
if err := t.SelfTestCreateExe(); err != nil {
return err
}
Expand All @@ -87,7 +87,7 @@ func (t *TargetBuilder) SelfTestExecute() error {
return err
}

if err := t.AppBuilder.SelfTestExecute(testRpkg); err != nil {
if err := t.AppBuilder.SelfTestExecute(testRpkg, valgrind); err != nil {
return err
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (b *Builder) testOwner(bpkg *BuildPackage) *BuildPackage {
}
}

func (b *Builder) SelfTestExecute(testRpkg *resolve.ResolvePackage) error {
func (b *Builder) SelfTestExecute(testRpkg *resolve.ResolvePackage, valgrind bool) error {
testPath := b.TestExePath()
if err := os.Chdir(filepath.Dir(testPath)); err != nil {
return err
Expand All @@ -149,11 +149,16 @@ func (b *Builder) SelfTestExecute(testRpkg *resolve.ResolvePackage) error {
util.StatusMessage(util.VERBOSITY_DEFAULT, "Executing test: %s\n",
testPath)
cmd := []string{testPath}
if _, err := util.ShellCommand(cmd, nil); err != nil {
if valgrind {
cmd = append([]string{"valgrind", "--error-exitcode=1"}, cmd...)
}
if output, err := util.ShellCommand(cmd, nil); err != nil {
newtError := err.(*util.NewtError)
newtError.Text = fmt.Sprintf("Test failure (%s):\n%s",
testRpkg.Lpkg.Name(), newtError.Text)
return newtError
} else if valgrind {
util.StatusMessage(util.VERBOSITY_DEFAULT, "%s", output)
}

return nil
Expand Down
18 changes: 15 additions & 3 deletions newt/cli/build_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cli
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -235,11 +236,19 @@ func pkgnames(pkgs []*pkg.LocalPackage) string {
return s
}

func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell bool) {
func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell bool, valgrind bool) {
if len(args) < 1 {
NewtUsage(cmd, nil)
}

if valgrind == true {
_, err := exec.LookPath("valgrind")
if err != nil {
NewtUsage(nil, util.FmtNewtError("Valgrind is not installed."+
"Please install it before running tests with --valgrind flag."))
}
}

util.ExecuteShell = executeShell

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

err = b.SelfTestExecute()
err = b.SelfTestExecute(valgrind)
if err == nil {
passedPkgs = append(passedPkgs, pack)
} else {
Expand Down Expand Up @@ -435,6 +444,7 @@ func sizeRunCmd(cmd *cobra.Command, args []string, ram bool, flash bool, section
func AddBuildCommands(cmd *cobra.Command) {
var printShellCmds bool
var executeShell bool
var valgrind bool

buildCmd := &cobra.Command{
Use: "build <target-name> [target-names...]",
Expand Down Expand Up @@ -474,12 +484,14 @@ func AddBuildCommands(cmd *cobra.Command) {
Use: "test <package-name> [package-names...] | all",
Short: "Executes unit tests for one or more packages",
Run: func(cmd *cobra.Command, args []string) {
testRunCmd(cmd, args, exclude, executeShell)
testRunCmd(cmd, args, exclude, executeShell, valgrind)
},
}
testCmd.Flags().StringVarP(&exclude, "exclude", "e", "", "Comma separated list of packages to exclude")
testCmd.Flags().BoolVar(&executeShell, "executeShell", false,
"Execute build command using /bin/sh (Linux and MacOS only)")
testCmd.Flags().BoolVar(&valgrind, "valgrind", false,
"Run test executables under Valgrind")
cmd.AddCommand(testCmd)
AddTabCompleteFn(testCmd, func() []string {
return append(testablePkgList(), "all", "allexcept")
Expand Down
Loading