github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/actions/lua/hook/lib_test.go (about)

     1  package hook_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/treeverse/lakefs/pkg/actions/lua/hook"
     7  
     8  	"github.com/Shopify/go-lua"
     9  )
    10  
    11  const scriptWithExplicitFailure = `
    12  hook = require("hook")
    13  
    14  hook.fail("this hook shall not pass")
    15  `
    16  
    17  const scriptWithExplicitError = `
    18  error("oh no")
    19  `
    20  
    21  const scriptWithSyntaxError = `
    22  local a = 15
    23  a += "a"
    24  `
    25  
    26  func TestUnwrap(t *testing.T) {
    27  	t.Run("explicit fail", func(t *testing.T) {
    28  		l := lua.NewState()
    29  		lua.OpenLibraries(l)
    30  		hook.Open(l)
    31  		err := lua.DoString(l, scriptWithExplicitFailure)
    32  		if err == nil {
    33  			t.Error("expected error but got none!")
    34  		}
    35  		before := err
    36  		after := hook.Unwrap(before)
    37  		if after.Error() != "this hook shall not pass" {
    38  			t.Errorf("could not unwrap lua hook error, got %s", after.Error())
    39  		}
    40  	})
    41  	t.Run("regular error", func(t *testing.T) {
    42  		l := lua.NewState()
    43  		lua.OpenLibraries(l)
    44  		hook.Open(l)
    45  		err := lua.DoString(l, scriptWithExplicitError)
    46  		if err == nil {
    47  			t.Error("expected error but got none!")
    48  		}
    49  		before := err
    50  		after := hook.Unwrap(err)
    51  		if after.Error() != before.Error() {
    52  			t.Error("unwrapping things not returned by hook.fail should not change the error")
    53  		}
    54  	})
    55  	t.Run("syntax error", func(t *testing.T) {
    56  		l := lua.NewState()
    57  		lua.OpenLibraries(l)
    58  		hook.Open(l)
    59  		err := lua.DoString(l, scriptWithSyntaxError)
    60  		if err == nil {
    61  			t.Error("expected error but got none!")
    62  		}
    63  		before := err
    64  		after := hook.Unwrap(err)
    65  		if after.Error() != before.Error() {
    66  			t.Error("unwrapping things not returned by hook.fail should not change the error")
    67  		}
    68  	})
    69  	t.Run("nil error", func(t *testing.T) {
    70  		after := hook.Unwrap(nil)
    71  		if after != nil {
    72  			t.Error("unwrapping nil should return nil")
    73  		}
    74  	})
    75  }