github.com/supabase/cli@v1.168.1/internal/test/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 TestCreatePgTAP(t *testing.T) {
    15  	t.Run("creates test file", func(t *testing.T) {
    16  		// Setup in-memory fs
    17  		fsys := afero.NewMemMapFs()
    18  		// Run test
    19  		err := Run(context.Background(), "pet", TemplatePgTAP, fsys)
    20  		// Check error
    21  		assert.NoError(t, err)
    22  		f, err := fsys.Stat(filepath.Join(utils.DbTestsDir, "pet_test.sql"))
    23  		assert.NoError(t, err)
    24  		assert.EqualValues(t, len(pgtapTest), f.Size())
    25  	})
    26  
    27  	t.Run("throws error on write failure", func(t *testing.T) {
    28  		// Setup in-memory fs
    29  		fsys := afero.NewMemMapFs()
    30  		// Run test
    31  		err := Run(context.Background(), "pet", TemplatePgTAP, afero.NewReadOnlyFs(fsys))
    32  		// Check error
    33  		assert.ErrorContains(t, err, "operation not permitted")
    34  	})
    35  
    36  	t.Run("throws error on file exists", func(t *testing.T) {
    37  		// Setup in-memory fs
    38  		fsys := afero.NewMemMapFs()
    39  		_, err := fsys.Create(filepath.Join(utils.DbTestsDir, "pet_test.sql"))
    40  		require.NoError(t, err)
    41  		// Run test
    42  		err = Run(context.Background(), "pet", TemplatePgTAP, fsys)
    43  		// Check error
    44  		assert.ErrorContains(t, err, "already exists")
    45  	})
    46  }