summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2021-09-05 20:37:19 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2021-09-05 20:37:19 -0500
commitf5b60238e05b124eb40f805eb4a0bbfc0b043da5 (patch)
treef461bff108f5ddafc4078aa7394d7bf2a6309cc9 /cmd
parent8f22bd4f5b4eb6996c524bcb6948d36cef0ac822 (diff)
parentfd66fb134967067ed7e1c3182325f646b73c730b (diff)
downloaddeb-planr-f5b60238e05b124eb40f805eb4a0bbfc0b043da5.tar.xz
deb-planr-f5b60238e05b124eb40f805eb4a0bbfc0b043da5.zip
Merge branch 'upstream' into ppa
Merge v0.1.0
Diffstat (limited to 'cmd')
-rw-r--r--cmd/planr/main.go77
-rw-r--r--cmd/planr/sub/build.go16
-rw-r--r--cmd/planr/sub/clean.go7
-rw-r--r--cmd/planr/sub/cli.go60
-rw-r--r--cmd/planr/sub/common.go15
-rw-r--r--cmd/planr/sub/config.go28
-rw-r--r--cmd/planr/sub/evaluate.go89
7 files changed, 215 insertions, 77 deletions
diff --git a/cmd/planr/main.go b/cmd/planr/main.go
index e2a85ed..2243203 100644
--- a/cmd/planr/main.go
+++ b/cmd/planr/main.go
@@ -1,24 +1,28 @@
package main
-import (
+import ( "flag"
"fmt"
"io"
"log"
"os"
+ "runtime/pprof"
+ "golang.flu0r1ne.net/planr"
+ "golang.flu0r1ne.net/planr/adapters/gtest"
+ "golang.flu0r1ne.net/planr/adapters/bash"
"golang.flu0r1ne.net/planr/cmd/planr/sub"
)
-const (
- VERSION = "0.0.3"
-)
-
func printUsage(w io.Writer) {
fmt.Fprintf (w, "usage: %s command args ... \n", os.Args[0])
fmt.Fprintln(w, " help ")
fmt.Fprintln(w, " version ")
fmt.Fprintln(w, " build ")
fmt.Fprintln(w, " evaluate ")
+ fmt.Fprintln(w, " evaluate <test0> <test1> ... ")
+ fmt.Fprintln(w, " evaluate -json ")
+ fmt.Fprintln(w, " clean ")
+ fmt.Fprintln(w, " config <key> ")
}
func dieUsage() {
@@ -26,25 +30,74 @@ func dieUsage() {
os.Exit(1)
}
+var src = flag.String("srcdir", "", "source directory")
+var config = flag.String("configdir", "", "config directory")
+var build = flag.String("builddir", "", "build directory")
+var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
+
+func getConfiguredRunner() planr.Runner {
+ r := planr.ConfigureRunner()
+ r = planr.RegisterAdapter(r, gtest.NewAdapter())
+ r = planr.RegisterAdapter(r, bash.NewAdapter())
+
+ if wd, err := os.Getwd(); err == nil {
+ r = planr.SetConfigDirFromTree(r, wd)
+ }
+
+ if *src != "" {
+ r = planr.SetSrcDir(r, *src)
+ }
+
+ if *config != "" {
+ r = planr.SetConfigDir(r, *config)
+ }
+
+ if *build != "" {
+ r = planr.SetBuildDir(r, *build)
+ }
+
+ return r.New()
+}
+
func main() {
+ flag.Parse()
+
+ if *cpuprofile != "" {
+ f, err := os.Create(*cpuprofile)
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ pprof.StartCPUProfile(f)
+ defer pprof.StopCPUProfile()
+ }
log.SetFlags(log.Llongfile | log.Lmsgprefix)
log.SetPrefix("planr: ")
- if len(os.Args) < 2 {
+ if flag.NArg() < 1 {
dieUsage()
}
- subcommand := os.Args[1]
- subargs := os.Args[2:]
+ runner := getConfiguredRunner()
+
+ cfg := planr.DecodeConfig(runner.ConfigDir())
+
+ subcommand := flag.Arg(0)
+ subargs := flag.Args()[1:]
switch subcommand {
case "version":
- fmt.Printf("%s\n", VERSION)
+ fmt.Printf("%s\n", planr.VERSION)
case "build":
- sub.Build(subargs)
- case "evaluate","eval":
- sub.Evaluate(subargs)
+ sub.Build(runner, subargs, cfg)
+ case "evaluate", "eval":
+ sub.Evaluate(runner, subargs, cfg)
+ case "clean":
+ sub.Clean(runner, subargs)
+ case "config":
+ sub.Config(runner, subargs)
case "help", "-h", "-help", "--help":
printUsage(os.Stdout)
default:
diff --git a/cmd/planr/sub/build.go b/cmd/planr/sub/build.go
index 58c3a38..e93e0cb 100644
--- a/cmd/planr/sub/build.go
+++ b/cmd/planr/sub/build.go
@@ -2,18 +2,10 @@ package sub
import (
"golang.flu0r1ne.net/planr"
- "golang.flu0r1ne.net/planr/adapters/gtest"
)
-func Runner() planr.Runner {
- r := planr.Runner {}
- r.RegisterAdapter(&gtest.GtestAdapter{})
- return r
-}
-
-func Build(params []string) {
-
- rd := planr.RubricDir()
-
- Runner().Build(rd)
+func Build(runner planr.Runner, params []string, cfg * planr.Config) {
+ dieIncompatibleVersion(cfg)
+ tcs := runner.CollectCases()
+ runner.Build(tcs)
}
diff --git a/cmd/planr/sub/clean.go b/cmd/planr/sub/clean.go
new file mode 100644
index 0000000..d658c10
--- /dev/null
+++ b/cmd/planr/sub/clean.go
@@ -0,0 +1,7 @@
+package sub
+
+import "golang.flu0r1ne.net/planr"
+
+func Clean(runner planr.Runner, params []string) {
+ runner.Clean()
+}
diff --git a/cmd/planr/sub/cli.go b/cmd/planr/sub/cli.go
index 0e6a942..e6f2256 100644
--- a/cmd/planr/sub/cli.go
+++ b/cmd/planr/sub/cli.go
@@ -14,25 +14,23 @@ var (
col_label = color.New(color.FgCyan)
);
-func tcTitle(tc planr.TestCase) string {
- title := tc.Cname
+func tcTitle(tr planr.TestResult) string {
+ title := tr.Tc.Cname
- if tc.Config.Title != nil {
- title = *tc.Config.Title
+ if tr.Tc.Config.Title != nil {
+ title = *tr.Tc.Config.Title
}
return title
}
-func tcStatus(tc planr.TestCase) string {
+func tcStatus(tc planr.TestResult) string {
status := "SILENT"
- if tc.Result != nil {
- if tc.Result.Status == planr.PASSING {
- status = "PASS"
- } else {
- status = "FAIL"
- }
+ if tc.Status == planr.PASSING {
+ status = "PASS"
+ } else {
+ status = "FAIL"
}
return status
@@ -59,9 +57,9 @@ func pprintFenced(title, value string) {
fmt.Println(fence)
}
-func tcStatusLine(tc planr.TestCase) {
- title := tcTitle(tc)
- status := tcStatus(tc)
+func tcStatusLine(tr planr.TestResult) {
+ title := tcTitle(tr)
+ status := tcStatus(tr)
if status == "PASS" {
col_pass.Printf("[%s] ", status);
@@ -72,8 +70,10 @@ func tcStatusLine(tc planr.TestCase) {
col_title.Println(title);
}
-func tcPprint(tc planr.TestCase) {
- tcStatusLine(tc)
+func tcPprint(tr planr.TestResult) {
+ tcStatusLine(tr)
+
+ tc := tr.Tc
pprintLabeled("id", tc.Cname)
@@ -86,22 +86,20 @@ func tcPprint(tc planr.TestCase) {
pprintLabeled("description", *tc.Config.Description)
}
- res := tc.Result
-
- if res.Status == planr.COMPILATION_FAILURE {
+ if tr.Status == planr.COMPILATION_FAILURE {
- if res.DebugOutput != "" {
+ if tr.DebugOutput != "" {
fmt.Println()
- pprintFenced("compilation output", tc.Result.DebugOutput);
+ pprintFenced("compilation output", tr.DebugOutput);
} else {
fmt.Println("WARN: No debug output provided")
}
- } else if res.Status == planr.RUNTIME_FAILURE {
+ } else if tr.Status == planr.RUNTIME_FAILURE {
- if tc.Result.TestOutput != "" {
+ if tr.TestOutput != "" {
fmt.Println()
- pprintFenced("test output", tc.Result.TestOutput);
+ pprintFenced("test output", tr.TestOutput);
}
}
@@ -109,14 +107,16 @@ func tcPprint(tc planr.TestCase) {
fmt.Println()
}
-func printResults(passed, tc_total int, earned, points_total float64) {
+func printScoring(score planr.Scoring) {
col_title.Println("Final Results:")
- pprintLabeled("passed", fmt.Sprintf("%d/%d", passed, tc_total));
+ pprintLabeled("passed", fmt.Sprintf("%d/%d", score.Passed, score.Total));
- percent := earned / points_total * 100
+ percent := score.EarnedPoints / score.TotalPoints * 100
- pprintLabeled("score", fmt.Sprintf(
- "%.2f/%.2f ~= %.1f%%", earned, points_total, percent,
- ));
+ if score.TotalPoints != 0 {
+ pprintLabeled("score", fmt.Sprintf(
+ "%.2f/%.2f ~= %.1f%%", score.EarnedPoints, score.TotalPoints, percent,
+ ));
+ }
}
diff --git a/cmd/planr/sub/common.go b/cmd/planr/sub/common.go
new file mode 100644
index 0000000..9126f3c
--- /dev/null
+++ b/cmd/planr/sub/common.go
@@ -0,0 +1,15 @@
+package sub
+
+import (
+ "golang.flu0r1ne.net/planr"
+ "os"
+ "fmt"
+)
+
+func dieIncompatibleVersion(cfg *planr.Config) {
+ if cfg != nil && cfg.IncompatibleWithVersion() {
+ fmt.Fprintf(os.Stderr, "This version of PlanR (%v) is incompatible with config version %s\n", planr.VERSION, cfg.Version)
+ fmt.Fprintf(os.Stderr, "Please upgrade to version %s or greater\n", cfg.Version)
+ os.Exit(1)
+ }
+}
diff --git a/cmd/planr/sub/config.go b/cmd/planr/sub/config.go
new file mode 100644
index 0000000..ee372c8
--- /dev/null
+++ b/cmd/planr/sub/config.go
@@ -0,0 +1,28 @@
+package sub
+
+import (
+ "golang.flu0r1ne.net/planr"
+ "fmt"
+ "os"
+)
+
+
+func Config(runner planr.Runner, params []string) {
+ if len(params) != 1 {
+ fmt.Fprintf(os.Stderr, "Usage: planr config <parameter>\n")
+ os.Exit(1)
+ }
+
+ key := params[0]
+
+ switch key {
+ case "builddir":
+ fmt.Printf("%s\n", runner.BuildDir())
+ case "configdir":
+ fmt.Printf("%s\n", runner.ConfigDir())
+ case "srcdir":
+ fmt.Printf("%s\n", runner.SrcDir())
+ default:
+ fmt.Fprintf(os.Stderr, "\"%s\" not found in configuration\n", key)
+ }
+}
diff --git a/cmd/planr/sub/evaluate.go b/cmd/planr/sub/evaluate.go
index 8ce4d81..30d30d2 100644
--- a/cmd/planr/sub/evaluate.go
+++ b/cmd/planr/sub/evaluate.go
@@ -1,38 +1,81 @@
package sub
import (
- "golang.flu0r1ne.net/planr"
+ "encoding/json"
+ "fmt"
+ "log"
+ "flag"
+
+ "golang.flu0r1ne.net/planr"
)
-func Evaluate(params []string) {
- rd := planr.RubricDir()
+type gradingResults struct {
+ TestResults []planr.TestResult
+ Score planr.Scoring
+}
+
+func prettyPrint(results gradingResults, summarize bool) {
+ for _, tr := range results.TestResults {
+ tcPprint(tr)
+ }
+
+ if summarize {
+ printScoring(results.Score)
+ }
+}
- tcs := Runner().Evaluate(rd)
+func jsonPrint(results gradingResults) {
+ res, err := json.Marshal(results)
- earned := 0.0
- total := 0.0
- passed := 0
- for _, tc := range tcs {
- cfg := tc.Config
+ if err != nil {
+ log.Fatalf("Error printing JSON: %v\n", err)
+ }
- if cfg.Points != nil {
- points := float64(*cfg.Points)
+ fmt.Println(string(res))
+}
- total += points
+func Evaluate(runner planr.Runner, params []string, cfg *planr.Config) {
+ f := flag.NewFlagSet("evaluate", flag.ExitOnError)
- if tc.Result.Status == planr.PASSING {
- earned += points
- passed++
- }
+ jsonOutput := f.Bool("json", false, "print json output")
+
+ dieIncompatibleVersion(cfg)
+
+ f.Parse(params)
+
+ tcs := runner.CollectCases()
+
+ // Filter those tests which patch IDs in params
+ filteredTcs := make([]planr.TestCase, 0)
+ summarizeScore := false
+ if f.NArg() > 0 {
+ ids := f.Args()
+
+ membershipFun := make(map[string] bool, 0)
+ for _, id := range ids {
+ membershipFun[id] = true
}
- tcPprint(tc)
+ for i := range tcs {
+ if membershipFun[tcs[i].Cname] {
+ filteredTcs = append(filteredTcs, tcs[i])
+ }
+ }
+ } else {
+ summarizeScore = true
+ filteredTcs = tcs
}
- printResults(
- passed,
- len(tcs),
- earned,
- total,
- );
+ trs := runner.Evaluate(filteredTcs)
+
+ results := gradingResults {
+ TestResults: trs,
+ Score: planr.Score(trs),
+ }
+
+ if *jsonOutput {
+ jsonPrint(results)
+ } else {
+ prettyPrint(results, summarizeScore)
+ }
}