Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop 46103ff05 -> fbfdff657


newt - Allow syscfg to be displayed for unittests

Usage: newt target config <unittest-package-name>


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/fbfdff65
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/fbfdff65
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/fbfdff65

Branch: refs/heads/develop
Commit: fbfdff657446851578551dc17572a335b4f9f5d1
Parents: 46103ff
Author: Christopher Collins <ccoll...@apache.org>
Authored: Thu Dec 1 19:10:17 2016 -0800
Committer: Christopher Collins <ccoll...@apache.org>
Committed: Thu Dec 1 19:10:35 2016 -0800

----------------------------------------------------------------------
 newt/cli/build_cmds.go   | 30 +++-----------------
 newt/cli/complete_cmd.go | 64 ++++++++++++++++++++++++++-----------------
 newt/cli/target_cmds.go  | 41 ++++++++++++++++++++++-----
 newt/cli/util.go         | 32 ++++++++++++++++++++++
 4 files changed, 109 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fbfdff65/newt/cli/build_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go
index e688d16..112e9a3 100644
--- a/newt/cli/build_cmds.go
+++ b/newt/cli/build_cmds.go
@@ -259,31 +259,9 @@ func testRunCmd(cmd *cobra.Command, args []string) {
                        NewtUsage(nil, err)
                }
 
-               // Each unit test package gets its own target.  This target is 
a copy
-               // of the base unit test package, just with an appropriate 
name.  The
-               // reason each test needs a unique target is: syscfg and 
sysinit are
-               // target-specific.  If each test package shares a target, they 
will
-               // overwrite these generated headers each time they are run.  
Worse, if
-               // two tests are run back-to-back, the timestamps may indicate 
that the
-               // headers have not changed between tests, causing build 
failures.
-               baseTarget := ResolveTarget(TARGET_TEST_NAME)
-               if baseTarget == nil {
-                       NewtUsage(nil, util.NewNewtError("Can't find unit test 
target: "+
-                               TARGET_TEST_NAME))
-               }
-
-               targetName := fmt.Sprintf("%s/%s/%s",
-                       TARGET_DEFAULT_DIR, TARGET_TEST_NAME,
-                       builder.TestTargetName(pack.Name()))
-
-               t := ResolveTarget(targetName)
-               if t == nil {
-                       targetName, err := ResolveNewTargetName(targetName)
-                       if err != nil {
-                               NewtUsage(nil, err)
-                       }
-
-                       t = baseTarget.Clone(proj.LocalRepo(), targetName)
+               t, err := ResolveUnittestTarget(pack.Name())
+               if err != nil {
+                       NewtUsage(nil, err)
                }
 
                b, err := builder.NewTargetTester(t, pack)
@@ -406,7 +384,7 @@ func AddBuildCommands(cmd *cobra.Command) {
                Short: "Executes unit tests for one or more packages",
                Run:   testRunCmd,
        }
-       testCmd.ValidArgs = append(packageList(), "all")
+       testCmd.ValidArgs = append(testablePkgList(), "all")
        cmd.AddCommand(testCmd)
 
        loadHelpText := "Load app image to target for <target-name>."

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fbfdff65/newt/cli/complete_cmd.go
----------------------------------------------------------------------
diff --git a/newt/cli/complete_cmd.go b/newt/cli/complete_cmd.go
index 1900586..4235851 100644
--- a/newt/cli/complete_cmd.go
+++ b/newt/cli/complete_cmd.go
@@ -28,36 +28,46 @@ import (
 
        "mynewt.apache.org/newt/newt/pkg"
        "mynewt.apache.org/newt/newt/project"
-       "mynewt.apache.org/newt/newt/target"
 
        "github.com/spf13/cobra"
        "github.com/spf13/pflag"
 )
 
-func targetList() []string {
-       targetNames := []string{}
+func pkgNameList(filterCb func(*pkg.LocalPackage) bool) []string {
+       names := []string{}
 
-       if _, err := project.TryGetProject(); err != nil {
-               return targetNames
+       proj, err := project.TryGetProject()
+       if err != nil {
+               return names
        }
 
-       for name, _ := range target.GetTargets() {
-               // Don't display the special unittest target; this is used
-               // internally by newt, so the user doesn't need to know about 
it.
-               // XXX: This is a hack; come up with a better solution for unit
-               // testing.
-               if !strings.HasSuffix(name, "/unittest") {
-                       targetNames = append(targetNames,
-                               strings.TrimPrefix(name, "targets/"))
+       for _, pack := range proj.PackagesOfType(-1) {
+               if filterCb(pack.(*pkg.LocalPackage)) {
+                       names = append(names, pack.FullName())
                }
        }
 
-       sort.Strings(targetNames)
+       sort.Strings(names)
+       return names
+}
+
+func targetList() []string {
+       targetNames := pkgNameList(func(pack *pkg.LocalPackage) bool {
+               return pack.Type() == pkg.PACKAGE_TYPE_TARGET &&
+                       !strings.HasSuffix(pack.Name(), "/unittest")
+       })
+
+       // Remove "targets/" prefix.
+       for i, _ := range targetNames {
+               targetNames[i] = strings.TrimPrefix(
+                       targetNames[i], TARGET_DEFAULT_DIR+"/")
+       }
+
        return targetNames
 }
 
 /* @return                      A slice of all testable package names. */
-func packageList() []string {
+func testablePkgList() []string {
        packs := testablePkgs()
        names := make([]string, 0, len(packs))
        for pack, _ := range packs {
@@ -69,20 +79,24 @@ func packageList() []string {
        return names
 }
 
-func mfgList() []string {
-       names := []string{}
+func unittestList() []string {
+       return pkgNameList(func(pack *pkg.LocalPackage) bool {
+               return pack.Type() == pkg.PACKAGE_TYPE_UNITTEST
+       })
+}
 
-       proj, err := project.TryGetProject()
-       if err != nil {
-               return names
-       }
+func mfgList() []string {
+       targetNames := pkgNameList(func(pack *pkg.LocalPackage) bool {
+               return pack.Type() == pkg.PACKAGE_TYPE_MFG
+       })
 
-       for _, pack := range proj.PackagesOfType(pkg.PACKAGE_TYPE_MFG) {
-               names = append(names, strings.TrimPrefix(pack.Name(), "mfgs/"))
+       // Remove "targets/" prefix.
+       for i, _ := range targetNames {
+               targetNames[i] = strings.TrimPrefix(
+                       targetNames[i], MFG_DEFAULT_DIR+"/")
        }
 
-       sort.Strings(names)
-       return names
+       return targetNames
 }
 
 func completeRunCmd(cmd *cobra.Command, args []string) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fbfdff65/newt/cli/target_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/target_cmds.go b/newt/cli/target_cmds.go
index b931607..09cd881 100644
--- a/newt/cli/target_cmds.go
+++ b/newt/cli/target_cmds.go
@@ -469,14 +469,41 @@ func targetConfigCmd(cmd *cobra.Command, args []string) {
 
        InitProject()
 
+       var b *builder.TargetBuilder
+
+       // Argument can specify either a target or a unittest package.  
Determine
+       // which type the package is and construct a target builder 
appropriately.
        t, err := resolveExistingTargetArg(args[0])
-       if err != nil {
-               NewtUsage(cmd, err)
-       }
+       if err == nil {
+               b, err = builder.NewTargetBuilder(t)
+               if err != nil {
+                       NewtUsage(nil, err)
+               }
+       } else {
+               proj := InitProject()
 
-       b, err := builder.NewTargetBuilder(t)
-       if err != nil {
-               NewtUsage(nil, err)
+               pack, err := proj.ResolvePackage(proj.LocalRepo(), args[0])
+               if err != nil {
+                       NewtUsage(cmd, util.FmtNewtError(
+                               "Could not resolve target or unittest \"%s\"", 
args[0]))
+               }
+
+               if pack.Type() != pkg.PACKAGE_TYPE_UNITTEST {
+                       NewtUsage(cmd, util.FmtNewtError(
+                               "Package \"%s\" is of type %s; "+
+                                       "must be target or unittest", args[0],
+                               pkg.PackageTypeNames[pack.Type()]))
+               }
+
+               t, err = ResolveUnittestTarget(pack.Name())
+               if err != nil {
+                       NewtUsage(nil, err)
+               }
+
+               b, err = builder.NewTargetTester(t, pack)
+               if err != nil {
+                       NewtUsage(nil, err)
+               }
        }
 
        cfgResolution, err := b.ExportCfg()
@@ -593,7 +620,7 @@ func AddTargetCommands(cmd *cobra.Command) {
                Short:     "View target system configuration",
                Long:      configHelpText,
                Run:       targetConfigCmd,
-               ValidArgs: targetList(),
+               ValidArgs: append(targetList(), unittestList()...),
        }
 
        targetCmd.AddCommand(configCmd)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fbfdff65/newt/cli/util.go
----------------------------------------------------------------------
diff --git a/newt/cli/util.go b/newt/cli/util.go
index 46ee9b0..afc5fc2 100644
--- a/newt/cli/util.go
+++ b/newt/cli/util.go
@@ -30,6 +30,7 @@ import (
        log "github.com/Sirupsen/logrus"
        "github.com/spf13/cobra"
 
+       "mynewt.apache.org/newt/newt/builder"
        "mynewt.apache.org/newt/newt/newtutil"
        "mynewt.apache.org/newt/newt/pkg"
        "mynewt.apache.org/newt/newt/project"
@@ -212,3 +213,34 @@ func InitProject() *project.Project {
 
        return p
 }
+
+func ResolveUnittestTarget(pkgName string) (*target.Target, error) {
+       // Each unit test package gets its own target.  This target is a copy
+       // of the base unit test package, just with an appropriate name.  The
+       // reason each test needs a unique target is: syscfg and sysinit are
+       // target-specific.  If each test package shares a target, they will
+       // overwrite these generated headers each time they are run.  Worse, if
+       // two tests are run back-to-back, the timestamps may indicate that the
+       // headers have not changed between tests, causing build failures.
+       baseTarget := ResolveTarget(TARGET_TEST_NAME)
+       if baseTarget == nil {
+               return nil, util.FmtNewtError("Can't find unit test target: %s",
+                       TARGET_TEST_NAME)
+       }
+
+       targetName := fmt.Sprintf("%s/%s/%s",
+               TARGET_DEFAULT_DIR, TARGET_TEST_NAME,
+               builder.TestTargetName(pkgName))
+
+       t := ResolveTarget(targetName)
+       if t == nil {
+               targetName, err := ResolveNewTargetName(targetName)
+               if err != nil {
+                       return nil, err
+               }
+
+               t = baseTarget.Clone(InitProject().LocalRepo(), targetName)
+       }
+
+       return t, nil
+}

Reply via email to