github.com/codefly-dev/core@v0.1.107/wool/wool_test.go (about) 1 package wool_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/codefly-dev/core/resources" 9 wool "github.com/codefly-dev/core/wool" 10 "github.com/codefly-dev/core/wool/adapters/log" 11 "github.com/stretchr/testify/require" 12 ) 13 14 type testLogger struct { 15 logs []*wool.Log 16 } 17 18 func (t *testLogger) Process(msg *wool.Log) { 19 t.logs = append(t.logs, msg) 20 } 21 22 func TestDefault(t *testing.T) { 23 ctx := context.Background() 24 25 w := wool.Get(ctx) 26 require.NotNil(t, w) 27 28 logger := &testLogger{} 29 w.WithLogger(logger) 30 31 err := fmt.Errorf("test error") 32 33 werr := w.Wrap(err) 34 35 require.EqualError(t, werr, "test error") 36 37 log.AsLog(w).Info("hello %s", "world") 38 39 require.Equal(t, 1, len(logger.logs)) 40 require.Equal(t, "hello world", logger.logs[0].Message) 41 } 42 43 func TestWoolBasics(t *testing.T) { 44 ctx := context.Background() 45 provider := wool.New(ctx, resources.CLI.AsResource()) 46 47 logger := &testLogger{} 48 provider.WithLogger(logger) 49 defer provider.Done() 50 51 ctx = provider.Inject(ctx) 52 53 w := wool.Get(ctx).In("testBasics", wool.Field("test", "test")) 54 55 require.NotNil(t, w) 56 57 err := fmt.Errorf("test error") 58 werr := w.Wrap(err) 59 require.EqualError(t, werr, "testBasics: test error") 60 61 // Use the standard logger interface 62 log.AsLog(w).Info("hello %s", "world") 63 64 w.Close() 65 66 require.Equal(t, 1, len(logger.logs)) 67 require.Equal(t, "hello world", logger.logs[0].Message) 68 } 69 70 func TestWoolWithContext(t *testing.T) { 71 ctx := context.Background() 72 provider := wool.New(ctx, resources.CLI.AsResource()) 73 74 logger := &testLogger{} 75 provider.WithLogger(logger) 76 defer provider.Done() 77 78 ctx = provider.Inject(ctx) 79 80 w := wool.Get(ctx) 81 82 require.NotNil(t, w) 83 84 // Use the standard logger interface 85 log.AsLog(w).Info("hello %s", "world") 86 87 w.Close() 88 89 require.Equal(t, 1, len(logger.logs)) 90 require.Equal(t, "hello world", logger.logs[0].Message) 91 } 92 93 // 94 //func one(ctx context.Wool, t *testing.T) { 95 // c := provider.GetWith(ctx).In("one", 96 // provider.InfoField("where", "one"), 97 // provider.DebugField("debug", "one"), 98 // provider.ErrorField("error", "one")) 99 //} 100 // 101 //func TestStack(t *testing.T) { 102 // ctx := context.Background() 103 // w := provider.New(ctx, configurations.CLI.AsResource()) 104 // 105 // logger := &testLogger{} 106 // w.WithLogger(logger) 107 // defer w.Done() 108 // 109 // ctx = w.NewContext() 110 // 111 //}