github.com/supabase/cli@v1.168.1/internal/functions/new/new_test.go (about)

     1  package new
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/supabase/cli/internal/utils"
    12  )
    13  
    14  func TestNewCommand(t *testing.T) {
    15  	t.Run("creates new function", func(t *testing.T) {
    16  		// Setup in-memory fs
    17  		fsys := afero.NewMemMapFs()
    18  		// Run test
    19  		assert.NoError(t, Run(context.Background(), "test-func", fsys))
    20  		// Validate output
    21  		funcPath := filepath.Join(utils.FunctionsDir, "test-func", "index.ts")
    22  		contains, err := afero.FileContainsBytes(fsys, funcPath, []byte(
    23  			`curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/test-func'`,
    24  		))
    25  		assert.NoError(t, err)
    26  		assert.True(t, contains)
    27  	})
    28  
    29  	t.Run("throws error on malformed slug", func(t *testing.T) {
    30  		assert.Error(t, Run(context.Background(), "@", afero.NewMemMapFs()))
    31  	})
    32  
    33  	t.Run("throws error on duplicate slug", func(t *testing.T) {
    34  		// Setup in-memory fs
    35  		fsys := afero.NewMemMapFs()
    36  		funcPath := filepath.Join(utils.FunctionsDir, "test-func", "index.ts")
    37  		require.NoError(t, afero.WriteFile(fsys, funcPath, []byte{}, 0644))
    38  		// Run test
    39  		assert.Error(t, Run(context.Background(), "test-func", fsys))
    40  	})
    41  
    42  	t.Run("throws error on permission denied", func(t *testing.T) {
    43  		// Setup in-memory fs
    44  		fsys := afero.NewReadOnlyFs(afero.NewMemMapFs())
    45  		// Run test
    46  		assert.Error(t, Run(context.Background(), "test-func", fsys))
    47  	})
    48  }