]> xenbits.xensource.com Git - people/gdunlap/xsatool/commitdiff
Testing: Add a testing "input indirection" layer
authorGeorge Dunlap <george.dunlap@citrix.com>
Thu, 11 May 2017 12:39:14 +0000 (13:39 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Wed, 17 May 2017 15:19:47 +0000 (16:19 +0100)
If called from main(), print the string and query the user.

Otherwise, look up the response in the index.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
global.go
main.go

index c591b95582f373ad2668e09b370f4d673f0372b1..6d8c12aaedc66109ca4c2c62756a7c96cb869c3d 100644 (file)
--- a/global.go
+++ b/global.go
@@ -1,10 +1,8 @@
 package main
 
 import (
-       "bufio"
        "fmt"
        "os"
-       "strings"
 )
 
 func MainGlobalUpdate(unused *XSAMeta, args []string) int {
@@ -26,15 +24,12 @@ func MainGlobalUpdate(unused *XSAMeta, args []string) int {
                        return 1
                }
 
-               fmt.Printf("Creating new global metadata\nOldest security-supported xen release: ")
-               input := bufio.NewScanner(os.Stdin)
-               if !input.Scan() {
-                       fmt.Printf("Input error\n")
+               fmt.Printf("Creating new global metadata\n")
+               s, err := Query("Oldest security-supported xen release: ")
+               if err != nil {
+                       fmt.Printf("Error: %v\n", err)
                        return 1
                }
-               s := input.Text()
-               // Get rid of newline at the end
-               s = strings.NewReplacer("\n", "").Replace(s)
 
                limit = XenVersion(s)
                if !limit.Check() {
diff --git a/main.go b/main.go
index a8263b1ce282e2c1b94cff125a834e713a715dab..3061ee7b3a758f9f43e12ff521854602a24f8e05 100644 (file)
--- a/main.go
+++ b/main.go
@@ -1,9 +1,11 @@
 package main
 
 import (
+       "bufio"
        "fmt"
        "os"
        "strconv"
+       "strings"
 )
 
 func OpenRepos() (err error) {
@@ -177,7 +179,39 @@ func XsaMain(args []string) int {
        return main(&xsa, args)
 }
 
+var Q struct {
+       real          bool
+       testResponses []string
+       trIndex       int
+}
+
+func Query(query string) (resp string, err error) {
+       if Q.real {
+               fmt.Print(query)
+               input := bufio.NewScanner(os.Stdin)
+               if !input.Scan() {
+                       err = fmt.Errorf("Input error\n")
+                       return
+               }
+               s := input.Text()
+               // Get rid of newline at the end
+               resp = strings.NewReplacer("\n", "").Replace(s)
+       } else {
+               // This will panic if we ask for a query beyond the index; that's what we want
+               resp = Q.testResponses[Q.trIndex]
+               Q.trIndex++
+       }
+       return
+}
+
+func QuerySetResponses(responses []string) {
+       // Copy the contents of the slice
+       Q.testResponses = append([]string{}, responses...)
+       Q.trIndex = 0
+}
+
 func main() {
+       Q.real = true
        os.Exit(XsaMain(os.Args))
 }