github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/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/Redstoneguy129/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  		content, err := afero.ReadFile(fsys, funcPath)
    23  		assert.NoError(t, err)
    24  		assert.Equal(t, index, string(content))
    25  	})
    26  
    27  	t.Run("throws error on malformed slug", func(t *testing.T) {
    28  		assert.Error(t, Run(context.Background(), "@", afero.NewMemMapFs()))
    29  	})
    30  
    31  	t.Run("throws error on duplicate slug", func(t *testing.T) {
    32  		// Setup in-memory fs
    33  		fsys := afero.NewMemMapFs()
    34  		funcDir := filepath.Join(utils.FunctionsDir, "test-func")
    35  		require.NoError(t, fsys.Mkdir(funcDir, 0755))
    36  		// Run test
    37  		assert.Error(t, Run(context.Background(), "test-func", fsys))
    38  	})
    39  
    40  	t.Run("throws error on permission denied", func(t *testing.T) {
    41  		// Setup in-memory fs
    42  		fsys := afero.NewReadOnlyFs(afero.NewMemMapFs())
    43  		// Run test
    44  		assert.Error(t, Run(context.Background(), "test-func", fsys))
    45  	})
    46  }