github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/pkg/log/log_test.go (about)

     1  // Copyright 2016 The rkt Authors
     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 log
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/hashicorp/errwrap"
    24  )
    25  
    26  const (
    27  	er1Msg          = "this is the first message"
    28  	er2Msg          = "this is the second message"
    29  	accompanyingMsg = "the accompanying message"
    30  	prefix          = "prefix"
    31  )
    32  
    33  var outputTests = []struct {
    34  	debug  bool
    35  	expect string
    36  }{
    37  	{
    38  		false,
    39  		fmt.Sprintf("%s: %s: %s\n", prefix, accompanyingMsg, er2Msg),
    40  	},
    41  	{
    42  		true,
    43  		fmt.Sprintf("%s: %s\n  └─%s\n    └─%s\n", prefix, accompanyingMsg, er1Msg, er2Msg),
    44  	},
    45  }
    46  
    47  func genNestedError() error {
    48  	e1 := errors.New(er1Msg)
    49  	e2 := errors.New(er2Msg)
    50  	err := errwrap.Wrap(e1, e2)
    51  
    52  	return err
    53  }
    54  
    55  func TestLogOutput(t *testing.T) {
    56  	for _, tt := range outputTests {
    57  		var logBuf bytes.Buffer
    58  		l := New(&logBuf, prefix, tt.debug)
    59  
    60  		l.PrintE(accompanyingMsg, genNestedError())
    61  		errOut := logBuf.String()
    62  		if errOut != tt.expect {
    63  			t.Errorf("Log output not as expected: %s != %s", errOut, tt.expect)
    64  		}
    65  	}
    66  }