11package trust
22
33import (
4+ "bufio"
45 "context"
56 "encoding/json"
67 "flag"
78 "os"
89 "regexp"
9- "strings"
1010 "testing"
1111
1212 "github.com/anchordotdev/cli"
@@ -45,6 +45,10 @@ func TestTrust(t *testing.T) {
4545 t .Fatal (err )
4646 }
4747
48+ headerPattern := regexp .MustCompile (`Installing "[^"]+ - AnchorCA" \w+ \([a-z0-9]+\) certificate:$` )
49+ installPattern := regexp .MustCompile (` - installed in the mock store.$` )
50+ skipPattern := regexp .MustCompile (` - skipped awaiting broader support.$` )
51+
4852 t .Run ("default to personal org and localhost realm" , func (t * testing.T ) {
4953 cmd := & Command {
5054 Config : cfg ,
@@ -55,15 +59,24 @@ func TestTrust(t *testing.T) {
5559 t .Fatal (err )
5660 }
5761
58- msgPattern := regexp .MustCompile (` - AnchorCA" \w+ cert \([a-z0-9]+\) installed in the mock store$` )
62+ scanner := bufio .NewScanner (buf )
63+ if ! scanner .Scan () {
64+ t .Fatalf ("want sudo warning line got %q %v (nil is EOF)" , scanner .Err (), scanner .Err ())
65+ }
66+ if line := scanner .Text (); line != sudoWarning {
67+ t .Errorf ("want output %q to match %q" , line , sudoWarning )
68+ }
69+ for scanner .Scan () {
70+ if line := scanner .Text (); ! headerPattern .MatchString (line ) {
71+ t .Errorf ("want output %q to match %q" , line , headerPattern )
72+ }
5973
60- for _ , line := range strings .Split (buf .String (), "\n " ) {
61- if len (line ) == 0 {
62- continue
74+ if ! scanner .Scan () {
75+ t .Fatalf ("want detail line got %q %v (nil is EOF)" , scanner .Err (), scanner .Err ())
6376 }
6477
65- if ! msgPattern .MatchString (line ) {
66- t .Errorf ("want output %q to match %q" , line , msgPattern )
78+ if line := scanner . Text (); ! ( installPattern .MatchString (line ) || skipPattern . MatchString ( line ) ) {
79+ t .Errorf ("want output %q to match %q or %q " , line , installPattern , skipPattern )
6780 }
6881 }
6982 })
@@ -81,15 +94,24 @@ func TestTrust(t *testing.T) {
8194 t .Fatal (err )
8295 }
8396
84- msgPattern := regexp .MustCompile (` - AnchorCA" \w+ cert \([a-z0-9]+\) installed in the mock store$` )
97+ scanner := bufio .NewScanner (buf )
98+ if ! scanner .Scan () {
99+ t .Fatalf ("want sudo warning line got %q %v (nil is EOF)" , scanner .Err (), scanner .Err ())
100+ }
101+ if line := scanner .Text (); line != sudoWarning {
102+ t .Errorf ("want output %q to match %q" , line , sudoWarning )
103+ }
104+ for scanner .Scan () {
105+ if line := scanner .Text (); ! headerPattern .MatchString (line ) {
106+ t .Errorf ("want output %q to match %q" , line , headerPattern )
107+ }
85108
86- for _ , line := range strings .Split (buf .String (), "\n " ) {
87- if len (line ) == 0 {
88- continue
109+ if ! scanner .Scan () {
110+ t .Fatalf ("want detail line got %q %v (nil is EOF)" , scanner .Err (), scanner .Err ())
89111 }
90112
91- if ! msgPattern .MatchString (line ) {
92- t .Errorf ("want output %q to match %q" , line , msgPattern )
113+ if line := scanner . Text (); ! ( installPattern .MatchString (line ) || skipPattern . MatchString ( line ) ) {
114+ t .Errorf ("want output %q to match %q or %q " , line , installPattern , skipPattern )
93115 }
94116 }
95117 })
0 commit comments