github.com/blend/go-sdk@v1.20220411.3/logger/scopes_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func Test_Scopes_IsEnabled_all(t *testing.T) {
    17  	its := assert.New(t)
    18  
    19  	all := ScopesAll()
    20  	its.True(all.IsEnabled())
    21  	its.True(all.IsEnabled("test0"))
    22  	its.True(all.IsEnabled("test0", "test1"))
    23  }
    24  
    25  func Test_Scopes_IsEnabled_all_explicit(t *testing.T) {
    26  	its := assert.New(t)
    27  
    28  	all := NewScopes(ScopeAll)
    29  	its.True(all.All())
    30  	its.True(all.IsEnabled())
    31  	its.True(all.IsEnabled("test0"))
    32  	its.True(all.IsEnabled("test0", "test1"))
    33  }
    34  
    35  func Test_Scopes_IsEnabled_none(t *testing.T) {
    36  	its := assert.New(t)
    37  
    38  	none := ScopesNone()
    39  	its.True(none.None())
    40  	its.False(none.IsEnabled())
    41  	its.False(none.IsEnabled("test0"))
    42  	its.False(none.IsEnabled("test0", "test1"))
    43  }
    44  
    45  func Test_Scopes_SetNone(t *testing.T) {
    46  	its := assert.New(t)
    47  
    48  	none := NewScopes("foo", "bar")
    49  	its.True(none.IsEnabled("foo"))
    50  	its.True(none.IsEnabled("bar"))
    51  	none.SetNone()
    52  	its.True(none.None())
    53  	its.False(none.IsEnabled())
    54  	its.False(none.IsEnabled("foo"))
    55  	its.False(none.IsEnabled("bar"))
    56  }
    57  
    58  func Test_Scopes_IsEnabled_strict(t *testing.T) {
    59  	its := assert.New(t)
    60  
    61  	scopes := NewScopes(
    62  		"test0/test1",
    63  		"foo0/foo1",
    64  	)
    65  	its.True(scopes.IsEnabled())
    66  
    67  	its.False(scopes.all)
    68  
    69  	its.False(scopes.IsEnabled("test0"))
    70  	its.True(scopes.IsEnabled("test0", "test1"))
    71  	its.False(scopes.IsEnabled("test0", "test2"))
    72  	its.False(scopes.IsEnabled("test2", "test1"))
    73  
    74  	its.False(scopes.IsEnabled("foo0"))
    75  	its.True(scopes.IsEnabled("foo0", "foo1"))
    76  	its.False(scopes.IsEnabled("foo0", "foo2"))
    77  	its.False(scopes.IsEnabled("foo2", "foo1"))
    78  }
    79  
    80  func Test_Scopes_IsEnabled_strict_explicitDisable(t *testing.T) {
    81  	its := assert.New(t)
    82  
    83  	all := NewScopes(
    84  		"test0/test1",
    85  		"-foo0/foo1",
    86  	)
    87  	its.True(all.IsEnabled())
    88  
    89  	its.False(all.IsEnabled("test0"))
    90  	its.True(all.IsEnabled("test0", "test1"))
    91  	its.False(all.IsEnabled("test0", "test2"))
    92  	its.False(all.IsEnabled("test2", "test1"))
    93  
    94  	its.False(all.IsEnabled("foo0"))
    95  	its.False(all.IsEnabled("foo0", "foo1"))
    96  	its.False(all.IsEnabled("foo0", "foo2"))
    97  	its.False(all.IsEnabled("foo2", "foo1"))
    98  }
    99  
   100  func Test_Scopes_IsEnabled_wildcard(t *testing.T) {
   101  	its := assert.New(t)
   102  
   103  	scopes := NewScopes(
   104  		"test0/*",
   105  		"foo0/foo1",
   106  	)
   107  	its.True(scopes.IsEnabled())
   108  
   109  	its.False(scopes.IsEnabled("test0"))
   110  	its.True(scopes.IsEnabled("test0", "test1"))
   111  	its.True(scopes.IsEnabled("test0", "test2"))
   112  	its.False(scopes.IsEnabled("test1", "test0"))
   113  
   114  	its.False(scopes.IsEnabled("foo0"))
   115  	its.True(scopes.IsEnabled("foo0", "foo1"))
   116  	its.False(scopes.IsEnabled("foo0", "foo2"))
   117  	its.False(scopes.IsEnabled("foo2", "foo1"))
   118  }
   119  
   120  func Test_Scopes_IsEnabled_inputAll_strict_explicitDisable(t *testing.T) {
   121  	its := assert.New(t)
   122  
   123  	all := NewScopes(
   124  		ScopeAll,
   125  		"-foo0/foo1",
   126  	)
   127  	its.True(all.IsEnabled())
   128  
   129  	its.True(all.IsEnabled("test0"))
   130  	its.True(all.IsEnabled("test0", "test1"))
   131  	its.True(all.IsEnabled("test0", "test2"))
   132  	its.True(all.IsEnabled("test2", "test1"))
   133  
   134  	its.True(all.IsEnabled("foo0")) // doesn't match glob
   135  	its.False(all.IsEnabled("foo0", "foo1"))
   136  	its.True(all.IsEnabled("foo0", "foo2"))
   137  	its.True(all.IsEnabled("foo2", "foo1"))
   138  }
   139  
   140  func Test_Scopes_IsEnabled_inputAll_wildcard_explicitDisable(t *testing.T) {
   141  	its := assert.New(t)
   142  
   143  	all := NewScopes(
   144  		ScopeAll,
   145  		"-foo0/*",
   146  	)
   147  	its.True(all.IsEnabled())
   148  
   149  	its.True(all.IsEnabled("test0"))
   150  	its.True(all.IsEnabled("test0", "test1"))
   151  	its.True(all.IsEnabled("test0", "test2"))
   152  	its.True(all.IsEnabled("test2", "test1"))
   153  
   154  	its.True(all.IsEnabled("foo0")) // doesn't match glob
   155  	its.False(all.IsEnabled("foo0", "foo1"))
   156  	its.False(all.IsEnabled("foo0", "foo2"))
   157  	its.True(all.IsEnabled("foo2", "foo1"))
   158  }
   159  
   160  func Test_Scopes_IsEnabled_wildcard_explicitDisable(t *testing.T) {
   161  	its := assert.New(t)
   162  
   163  	scopes := NewScopes(
   164  		"-test0/*",
   165  		"foo0/foo1",
   166  	)
   167  	its.True(scopes.IsEnabled())
   168  
   169  	its.False(scopes.IsEnabled("test0"))
   170  	its.False(scopes.IsEnabled("test0", "test1"))
   171  	its.False(scopes.IsEnabled("test0", "test2"))
   172  	its.False(scopes.IsEnabled("test1", "test0"))
   173  
   174  	its.False(scopes.IsEnabled("foo0"))
   175  	its.True(scopes.IsEnabled("foo0", "foo1"))
   176  	its.False(scopes.IsEnabled("foo0", "foo2"))
   177  	its.False(scopes.IsEnabled("foo2", "foo1"))
   178  }
   179  
   180  func Test_Scopes_String(t *testing.T) {
   181  	its := assert.New(t)
   182  
   183  	its.Equal("*", ScopesAll().String())
   184  	its.Equal("*", NewScopes(ScopeAll, "foo/bar/*", "test/testo").String())
   185  	its.Equal("*, -test/testo", NewScopes(ScopeAll, "foo/bar/*", "-test/testo").String())
   186  }
   187  
   188  func Test_Scopes_isExplicitlyDisabled(t *testing.T) {
   189  	its := assert.New(t)
   190  
   191  	s := NewScopes("*", "-foo/*")
   192  
   193  	its.False(s.isScopeExplicitlyDisabled("bar"))
   194  	its.False(s.isScopeExplicitlyDisabled("foo"))
   195  	its.False(s.isScopeExplicitlyDisabled("bar/foo"))
   196  	its.True(s.isScopeExplicitlyDisabled("foo/bar"))
   197  }
   198  
   199  func Test_Scopes_matches(t *testing.T) {
   200  	its := assert.New(t)
   201  
   202  	its.True(NewScopes("*").matches("foo/bar", "foo/*"))
   203  	its.False(NewScopes("*").matches("foo/*", "foo/bar"))
   204  }