github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/imports/wasi_snapshot_preview1/proc_test.go (about)

     1  package wasi_snapshot_preview1_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	wazero "github.com/wasilibs/wazerox"
     7  	"github.com/wasilibs/wazerox/internal/testing/require"
     8  	"github.com/wasilibs/wazerox/internal/wasip1"
     9  	"github.com/wasilibs/wazerox/sys"
    10  )
    11  
    12  func Test_procExit(t *testing.T) {
    13  	mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
    14  	defer r.Close(testCtx)
    15  
    16  	tests := []struct {
    17  		name        string
    18  		exitCode    uint32
    19  		expectedLog string
    20  	}{
    21  		{
    22  			name:     "success (exitcode 0)",
    23  			exitCode: 0,
    24  			expectedLog: `
    25  ==> wasi_snapshot_preview1.proc_exit(rval=0)
    26  `,
    27  		},
    28  		{
    29  			name:     "arbitrary non-zero exitcode",
    30  			exitCode: 42,
    31  			expectedLog: `
    32  ==> wasi_snapshot_preview1.proc_exit(rval=42)
    33  `,
    34  		},
    35  	}
    36  
    37  	for _, tt := range tests {
    38  		tc := tt
    39  
    40  		t.Run(tc.name, func(t *testing.T) {
    41  			defer log.Reset()
    42  
    43  			// Since procExit panics, any opcodes afterwards cannot be reached.
    44  			_, err := mod.ExportedFunction(wasip1.ProcExitName).Call(testCtx, uint64(tc.exitCode))
    45  			require.Error(t, err)
    46  			sysErr, ok := err.(*sys.ExitError)
    47  			require.True(t, ok, err)
    48  			require.Equal(t, tc.exitCode, sysErr.ExitCode())
    49  			require.Equal(t, tc.expectedLog, "\n"+log.String())
    50  		})
    51  	}
    52  }
    53  
    54  // Test_procRaise only tests it is stubbed for GrainLang per #271
    55  func Test_procRaise(t *testing.T) {
    56  	log := requireErrnoNosys(t, wasip1.ProcRaiseName, 0)
    57  	require.Equal(t, `
    58  ==> wasi_snapshot_preview1.proc_raise(sig=0)
    59  <== errno=ENOSYS
    60  `, log)
    61  }