github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/util/testutil/testdiagsink.go (about) 1 // Copyright 2016-2018, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testutil 16 17 import ( 18 "io/ioutil" 19 20 "github.com/pulumi/pulumi/sdk/v3/go/common/diag" 21 "github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors" 22 ) 23 24 // TestDiagSink suppresses message output, but captures them, so that they can be compared to expected results. 25 type TestDiagSink struct { 26 Pwd string 27 sink diag.Sink 28 messages map[diag.Severity][]string 29 } 30 31 func NewTestDiagSink(pwd string) *TestDiagSink { 32 return &TestDiagSink{ 33 Pwd: pwd, 34 sink: diag.DefaultSink(ioutil.Discard, ioutil.Discard, diag.FormatOptions{ 35 Color: colors.Never, 36 Pwd: pwd, 37 }), 38 messages: make(map[diag.Severity][]string), 39 } 40 } 41 42 func (d *TestDiagSink) DebugMsgs() []string { return d.messages[diag.Debug] } 43 func (d *TestDiagSink) InfoMsgs() []string { return d.messages[diag.Info] } 44 func (d *TestDiagSink) ErrorMsgs() []string { return d.messages[diag.Error] } 45 func (d *TestDiagSink) WarningMsgs() []string { return d.messages[diag.Warning] } 46 47 func (d *TestDiagSink) Logf(sev diag.Severity, dia *diag.Diag, args ...interface{}) { 48 d.messages[sev] = append(d.messages[sev], d.combine(sev, dia, args...)) 49 } 50 51 func (d *TestDiagSink) Debugf(dia *diag.Diag, args ...interface{}) { 52 d.messages[diag.Debug] = append(d.messages[diag.Debug], d.combine(diag.Debug, dia, args...)) 53 } 54 55 func (d *TestDiagSink) Infof(dia *diag.Diag, args ...interface{}) { 56 d.messages[diag.Info] = append(d.messages[diag.Info], d.combine(diag.Info, dia, args...)) 57 } 58 59 func (d *TestDiagSink) Errorf(dia *diag.Diag, args ...interface{}) { 60 d.messages[diag.Error] = append(d.messages[diag.Error], d.combine(diag.Error, dia, args...)) 61 } 62 63 func (d *TestDiagSink) Warningf(dia *diag.Diag, args ...interface{}) { 64 d.messages[diag.Warning] = append(d.messages[diag.Warning], d.combine(diag.Warning, dia, args...)) 65 } 66 67 func (d *TestDiagSink) Stringify(sev diag.Severity, dia *diag.Diag, args ...interface{}) (string, string) { 68 return d.sink.Stringify(sev, dia, args...) 69 } 70 71 func (d *TestDiagSink) combine(sev diag.Severity, dia *diag.Diag, args ...interface{}) string { 72 p, s := d.sink.Stringify(sev, dia, args...) 73 return p + s 74 }