You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/12/09 19:23:25 UTC

[3/4] incubator-mynewt-newt git commit: newt - Clean up; move selftest code to new file.

newt - Clean up; move selftest code to new file.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/8aa98e55
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/8aa98e55
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/8aa98e55

Branch: refs/heads/develop
Commit: 8aa98e55c03245a946327dfc8728828e17597444
Parents: 8825cd2
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Dec 9 11:04:37 2016 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Fri Dec 9 11:14:51 2016 -0800

----------------------------------------------------------------------
 newt/builder/build.go       |  70 ++------------------
 newt/builder/load.go        |  18 -----
 newt/builder/paths.go       |  29 ++++++--
 newt/builder/selftest.go    | 138 +++++++++++++++++++++++++++++++++++++++
 newt/builder/targetbuild.go |  24 -------
 newt/cli/build_cmds.go      |   2 +-
 newt/cli/run_cmds.go        |   4 +-
 newt/newtutil/newtutil.go   |   2 +-
 8 files changed, 168 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/builder/build.go
----------------------------------------------------------------------
diff --git a/newt/builder/build.go b/newt/builder/build.go
index 1f2a5c2..af6a791 100644
--- a/newt/builder/build.go
+++ b/newt/builder/build.go
@@ -28,7 +28,6 @@ import (
 
 	"mynewt.apache.org/newt/newt/image"
 	"mynewt.apache.org/newt/newt/pkg"
-	"mynewt.apache.org/newt/newt/project"
 	"mynewt.apache.org/newt/newt/repo"
 	"mynewt.apache.org/newt/newt/symbol"
 	"mynewt.apache.org/newt/newt/syscfg"
@@ -493,71 +492,6 @@ func (b *Builder) pkgWithPath(path string) *BuildPackage {
 	return nil
 }
 
-func (b *Builder) testOwner(p *BuildPackage) *BuildPackage {
-	if p.Type() != pkg.PACKAGE_TYPE_UNITTEST {
-		panic("Expected unittest package; got: " + p.Name())
-	}
-
-	curPath := p.BasePath()
-
-	for {
-		parentPath := filepath.Dir(curPath)
-		if parentPath == project.GetProject().BasePath || parentPath == "." {
-			return nil
-		}
-
-		parentPkg := b.pkgWithPath(parentPath)
-		if parentPkg != nil && parentPkg.Type() != pkg.PACKAGE_TYPE_UNITTEST {
-			return parentPkg
-		}
-
-		curPath = parentPath
-	}
-}
-
-// @return string               Path of generated test executable.
-func (b *Builder) BuildTest(p *pkg.LocalPackage) (string, error) {
-	// Build the packages alphabetically to ensure a consistent order.
-	bpkgs := b.sortedBuildPackages()
-	for _, bpkg := range bpkgs {
-		if err := b.buildPackage(bpkg); err != nil {
-			return "", err
-		}
-	}
-
-	testBpkg := b.PkgMap[p]
-	testPath := b.TestExePath(testBpkg)
-	if err := b.link(testPath, nil, nil); err != nil {
-		return "", err
-	}
-
-	return testPath, nil
-}
-
-func (b *Builder) Test(p *pkg.LocalPackage) error {
-	testPath, err := b.BuildTest(p)
-	if err != nil {
-		return err
-	}
-
-	// Run the tests.
-	if err := os.Chdir(filepath.Dir(testPath)); err != nil {
-		return err
-	}
-
-	util.StatusMessage(util.VERBOSITY_DEFAULT, "Executing test: %s\n",
-		testPath)
-	cmd := []string{testPath}
-	if _, err := util.ShellCommand(cmd, nil); err != nil {
-		newtError := err.(*util.NewtError)
-		newtError.Text = fmt.Sprintf("Test failure (%s):\n%s", p.Name(),
-			newtError.Text)
-		return newtError
-	}
-
-	return nil
-}
-
 func (b *Builder) FetchSymbolMap() (error, *symbol.SymbolMap) {
 	loader_sm := symbol.NewSymbolMap()
 
@@ -686,6 +620,10 @@ func (b *Builder) CreateImage(version string,
 //     <app>.elf.bin
 //     manifest.json
 func (b *Builder) CleanArtifacts() {
+	if b.appPkg == nil {
+		return
+	}
+
 	paths := []string{
 		b.AppImgPath(),
 		b.AppBinPath(),

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/builder/load.go
----------------------------------------------------------------------
diff --git a/newt/builder/load.go b/newt/builder/load.go
index 0096d96..1cf9a84 100644
--- a/newt/builder/load.go
+++ b/newt/builder/load.go
@@ -161,14 +161,6 @@ func (t *TargetBuilder) Debug(extraJtagCmd string, reset bool, noGDB bool) error
 	return t.LoaderBuilder.Debug(extraJtagCmd, reset, noGDB)
 }
 
-func (t *TargetBuilder) DebugTest() error {
-	if err := t.PrepBuild(); err != nil {
-		return err
-	}
-
-	return t.AppBuilder.DebugTest(t.GetTestPkg())
-}
-
 func (b *Builder) debugBin(binPath string, extraJtagCmd string, reset bool,
 	noGDB bool) error {
 	/*
@@ -220,13 +212,3 @@ func (b *Builder) Debug(extraJtagCmd string, reset bool, noGDB bool) error {
 
 	return b.debugBin(b.AppBinBasePath(), extraJtagCmd, reset, noGDB)
 }
-
-func (b *Builder) DebugTest(lpkg *pkg.LocalPackage) error {
-	bpkg := b.PkgMap[lpkg]
-	if bpkg == nil {
-		panic("internal error: local package \"" + lpkg.FullName() +
-			"\" not built")
-	}
-	return b.debugBin(strings.TrimSuffix(b.TestExePath(bpkg), ".elf"),
-		"", false, false)
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/builder/paths.go
----------------------------------------------------------------------
diff --git a/newt/builder/paths.go b/newt/builder/paths.go
index 58d1880..78e6229 100644
--- a/newt/builder/paths.go
+++ b/newt/builder/paths.go
@@ -22,6 +22,7 @@ package builder
 import (
 	"path/filepath"
 
+	"mynewt.apache.org/newt/newt/interfaces"
 	"mynewt.apache.org/newt/newt/pkg"
 	"mynewt.apache.org/newt/newt/project"
 )
@@ -65,6 +66,17 @@ func FileBinDir(targetName string, buildName string, pkgName string) string {
 	return BinDir(targetName, buildName) + "/" + pkgName
 }
 
+func PkgBinDir(targetName string, buildName string, pkgName string,
+	pkgType interfaces.PackageType) string {
+
+	switch pkgType {
+	case pkg.PACKAGE_TYPE_GENERATED:
+		return GeneratedBinDir(targetName)
+	default:
+		return FileBinDir(targetName, buildName, pkgName)
+	}
+}
+
 func AppElfPath(targetName string, buildName string, appName string) string {
 	return FileBinDir(targetName, buildName, appName) + "/" +
 		filepath.Base(appName) + ".elf"
@@ -74,6 +86,13 @@ func AppBinPath(targetName string, buildName string, appName string) string {
 	return AppElfPath(targetName, buildName, appName) + ".bin"
 }
 
+func TestExePath(targetName string, buildName string, pkgName string,
+	pkgType interfaces.PackageType) string {
+
+	return PkgBinDir(targetName, buildName, pkgName, pkgType) + "/" +
+		TestTargetName(pkgName) + ".elf"
+}
+
 func ManifestPath(targetName string, buildName string, pkgName string) string {
 	return FileBinDir(targetName, buildName, pkgName) + "/manifest.json"
 }
@@ -100,12 +119,7 @@ func (b *Builder) FileBinDir(pkgName string) string {
 }
 
 func (b *Builder) PkgBinDir(bpkg *BuildPackage) string {
-	switch bpkg.Type() {
-	case pkg.PACKAGE_TYPE_GENERATED:
-		return GeneratedBinDir(b.targetPkg.Name())
-	default:
-		return b.FileBinDir(bpkg.Name())
-	}
+	return PkgBinDir(b.targetPkg.Name(), b.buildName, bpkg.Name(), bpkg.Type())
 }
 
 // Generates the path+filename of the specified package's .a file.
@@ -141,7 +155,8 @@ func (b *Builder) AppPath() string {
 }
 
 func (b *Builder) TestExePath(bpkg *BuildPackage) string {
-	return b.PkgBinDir(bpkg) + "/" + TestTargetName(bpkg.Name()) + ".elf"
+	return TestExePath(b.targetPkg.Name(), b.buildName, bpkg.Name(),
+		bpkg.Type())
 }
 
 func (b *Builder) ManifestPath() string {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/builder/selftest.go
----------------------------------------------------------------------
diff --git a/newt/builder/selftest.go b/newt/builder/selftest.go
new file mode 100644
index 0000000..36781b7
--- /dev/null
+++ b/newt/builder/selftest.go
@@ -0,0 +1,138 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package builder
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"mynewt.apache.org/newt/newt/pkg"
+	"mynewt.apache.org/newt/newt/project"
+	"mynewt.apache.org/newt/util"
+)
+
+func (b *Builder) SelfTestLink(p *pkg.LocalPackage) error {
+	testBpkg := b.PkgMap[p]
+	testPath := b.TestExePath(testBpkg)
+	if err := b.link(testPath, nil, nil); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (t *TargetBuilder) SelfTestCreateExe() error {
+	if err := t.PrepBuild(); err != nil {
+		return err
+	}
+
+	if err := t.AppBuilder.Build(); err != nil {
+		return err
+	}
+
+	if err := t.AppBuilder.SelfTestLink(t.testPkg); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (t *TargetBuilder) SelfTestExecute() error {
+	if err := t.SelfTestCreateExe(); err != nil {
+		return err
+	}
+
+	if err := t.AppBuilder.SelfTestExecute(t.testPkg); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (t *TargetBuilder) SelfTestDebug() error {
+	if err := t.PrepBuild(); err != nil {
+		return err
+	}
+
+	lpkg := t.GetTestPkg()
+	if lpkg == nil {
+		panic("internal error: attempt to debug target builder with no test " +
+			"package")
+	}
+
+	bpkg := t.AppBuilder.PkgMap[lpkg]
+	if bpkg == nil {
+		panic("internal error: local package \"" + lpkg.FullName() +
+			"\" not built")
+	}
+
+	return t.AppBuilder.debugBin(
+		strings.TrimSuffix(t.AppBuilder.TestExePath(bpkg), ".elf"),
+		"", false, false)
+}
+
+func (b *Builder) testOwner(p *BuildPackage) *BuildPackage {
+	if p.Type() != pkg.PACKAGE_TYPE_UNITTEST {
+		panic("Expected unittest package; got: " + p.Name())
+	}
+
+	curPath := p.BasePath()
+
+	for {
+		parentPath := filepath.Dir(curPath)
+		if parentPath == project.GetProject().BasePath || parentPath == "." {
+			return nil
+		}
+
+		parentPkg := b.pkgWithPath(parentPath)
+		if parentPkg != nil && parentPkg.Type() != pkg.PACKAGE_TYPE_UNITTEST {
+			return parentPkg
+		}
+
+		curPath = parentPath
+	}
+}
+
+func (b *Builder) SelfTestExecute(p *pkg.LocalPackage) error {
+	testBpkg := b.PkgMap[p]
+	if testBpkg == nil {
+		panic("internal error; package-under-test \"" + p.FullName() +
+			"\" not in builder")
+	}
+
+	testPath := b.TestExePath(testBpkg)
+	if err := os.Chdir(filepath.Dir(testPath)); err != nil {
+		return err
+	}
+
+	util.StatusMessage(util.VERBOSITY_DEFAULT, "Executing test: %s\n",
+		testPath)
+	cmd := []string{testPath}
+	if _, err := util.ShellCommand(cmd, nil); err != nil {
+		newtError := err.(*util.NewtError)
+		newtError.Text = fmt.Sprintf("Test failure (%s):\n%s", p.Name(),
+			newtError.Text)
+		return newtError
+	}
+
+	return nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/builder/targetbuild.go
----------------------------------------------------------------------
diff --git a/newt/builder/targetbuild.go b/newt/builder/targetbuild.go
index b0bd2f2..37c06eb 100644
--- a/newt/builder/targetbuild.go
+++ b/newt/builder/targetbuild.go
@@ -524,30 +524,6 @@ func (t *TargetBuilder) RelinkLoader() (error, map[string]bool,
 	return err, commonPkgs, smMatch
 }
 
-func (t *TargetBuilder) BuildTest() error {
-	if err := t.PrepBuild(); err != nil {
-		return err
-	}
-
-	if _, err := t.AppBuilder.BuildTest(t.testPkg); err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (t *TargetBuilder) Test() error {
-	if err := t.BuildTest(); err != nil {
-		return err
-	}
-
-	if err := t.AppBuilder.Test(t.testPkg); err != nil {
-		return err
-	}
-
-	return nil
-}
-
 func (t *TargetBuilder) GetTarget() *target.Target {
 	return t.target
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/cli/build_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go
index d299367..ddf2c45 100644
--- a/newt/cli/build_cmds.go
+++ b/newt/cli/build_cmds.go
@@ -273,7 +273,7 @@ func testRunCmd(cmd *cobra.Command, args []string) {
 		util.StatusMessage(util.VERBOSITY_DEFAULT, "Testing package %s\n",
 			pack.FullName())
 
-		err = b.Test()
+		err = b.SelfTestExecute()
 		if err == nil {
 			passedPkgs = append(passedPkgs, pack)
 		} else {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/cli/run_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/run_cmds.go b/newt/cli/run_cmds.go
index b30d5a8..3555ed5 100644
--- a/newt/cli/run_cmds.go
+++ b/newt/cli/run_cmds.go
@@ -41,10 +41,10 @@ func runRunCmd(cmd *cobra.Command, args []string) {
 	testPkg := b.GetTestPkg()
 	if testPkg != nil {
 		b.InjectSetting("TESTUTIL_SYSTEM_ASSERT", "1")
-		if err := b.BuildTest(); err != nil {
+		if err := b.SelfTestCreateExe(); err != nil {
 			NewtUsage(nil, err)
 		}
-		if err := b.DebugTest(); err != nil {
+		if err := b.SelfTestDebug(); err != nil {
 			NewtUsage(nil, err)
 		}
 	} else {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8aa98e55/newt/newtutil/newtutil.go
----------------------------------------------------------------------
diff --git a/newt/newtutil/newtutil.go b/newt/newtutil/newtutil.go
index 2c433fd..835b0a3 100644
--- a/newt/newtutil/newtutil.go
+++ b/newt/newtutil/newtutil.go
@@ -93,7 +93,7 @@ func GetSliceFeatures(v *viper.Viper, features map[string]bool,
 
 	// Process the features in alphabetical order to ensure consistent
 	// results across repeated runs.
-	var featureKeys []string
+	featureKeys := make([]string, 0, len(features))
 	for feature, _ := range features {
 		featureKeys = append(featureKeys, feature)
 	}