github.com/supabase/cli@v1.168.1/internal/utils/misc_test.go (about) 1 package utils 2 3 import ( 4 "io/fs" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/spf13/afero" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 type MockFs struct { 16 afero.MemMapFs 17 DenyPath string 18 } 19 20 func (m *MockFs) Stat(name string) (fs.FileInfo, error) { 21 if strings.HasPrefix(name, m.DenyPath) { 22 return nil, fs.ErrPermission 23 } 24 return m.MemMapFs.Stat(name) 25 } 26 27 func TestProjectRoot(t *testing.T) { 28 root := string(filepath.Separator) 29 30 t.Run("stops at root dir", func(t *testing.T) { 31 // Setup in-memory fs 32 fsys := afero.NewMemMapFs() 33 _, err := fsys.Create(filepath.Join(root, ConfigPath)) 34 require.NoError(t, err) 35 // Run test 36 cwd := filepath.Join(root, "home", "user", "project") 37 path := getProjectRoot(cwd, fsys) 38 // Check error 39 assert.Equal(t, root, path) 40 }) 41 42 t.Run("stops at closest parent", func(t *testing.T) { 43 // Setup in-memory fs 44 fsys := afero.NewMemMapFs() 45 _, err := fsys.Create(filepath.Join(root, "supabase", ConfigPath)) 46 require.NoError(t, err) 47 // Run test 48 cwd := filepath.Join(root, "supabase", "supabase", "functions") 49 path := getProjectRoot(cwd, fsys) 50 // Check error 51 assert.Equal(t, filepath.Join(root, "supabase"), path) 52 }) 53 54 t.Run("ignores error on config not found", func(t *testing.T) { 55 cwd, err := os.Getwd() 56 require.NoError(t, err) 57 // Setup in-memory fs 58 fsys := afero.NewMemMapFs() 59 // Run test 60 path := getProjectRoot(cwd, fsys) 61 // Check error 62 assert.NoError(t, err) 63 assert.Equal(t, cwd, path) 64 }) 65 66 t.Run("ignores error if path is not directory", func(t *testing.T) { 67 cwd, err := os.Getwd() 68 require.NoError(t, err) 69 // Setup in-memory fs 70 fsys := &MockFs{DenyPath: filepath.Join(cwd, "supabase")} 71 // Run test 72 path := getProjectRoot(cwd, fsys) 73 // Check error 74 assert.NoError(t, err) 75 assert.Equal(t, cwd, path) 76 }) 77 }