github.com/developest/gtm-enhanced@v1.0.4-0.20220111132249-cc80a3372c3f/command/verify_test.go (about) 1 // Copyright 2016 Michael Schenk. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package command 6 7 import ( 8 "bytes" 9 "testing" 10 11 "github.com/mitchellh/cli" 12 ) 13 14 func TestCheck(t *testing.T) { 15 cases := []struct { 16 input string 17 cmd VerifyCmd 18 valid bool 19 err bool 20 }{ 21 {">= 1.2", VerifyCmd{Version: "1.0.0"}, false, false}, 22 {">= 1.0.0", VerifyCmd{Version: "v1.0.0"}, true, false}, 23 {">= 1.0.0", VerifyCmd{Version: "V1.0.0"}, true, false}, 24 {">= 1.0.0", VerifyCmd{Version: "1.0.0"}, true, false}, 25 {">= 1.0-beta.5", VerifyCmd{Version: "v1.0-beta.5"}, true, false}, 26 {">= 1.0.0", VerifyCmd{Version: "1.0.xxx"}, false, true}, 27 } 28 29 for _, tc := range cases { 30 valid, err := tc.cmd.check(tc.input) 31 if tc.err && err == nil { 32 t.Fatalf("expected error for input: '%s' Version: %s", tc.input, tc.cmd.Version) 33 } else if !tc.err && err != nil { 34 t.Fatalf("error for for input: '%s' Version: %s: %s", tc.input, tc.cmd.Version, err) 35 } 36 if valid != tc.valid { 37 t.Fatalf("input: '%s' Version: %s\nexpected %t\nactual: %t", 38 tc.input, tc.cmd.Version, tc.valid, valid) 39 } 40 } 41 42 } 43 44 func TestVerify(t *testing.T) { 45 ui := new(cli.MockUi) 46 c := VerifyCmd{UI: ui, Version: "1.0.0", Out: new(bytes.Buffer)} 47 48 args := []string{">= 1.0.0"} 49 rc := c.Run(args) 50 51 if rc != 0 { 52 t.Errorf("gtm verify(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String()) 53 } 54 55 want := "true" 56 if want != c.Out.String() { 57 t.Errorf("gtm verify(%+v), want '%s' got '%s', %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String()) 58 } 59 }