github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/init/init_test.go (about)

     1  package init
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/Redstoneguy129/cli/internal/utils"
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestInitCommand(t *testing.T) {
    14  	t.Run("creates config file", func(t *testing.T) {
    15  		// Setup in-memory fs
    16  		fsys := &afero.MemMapFs{}
    17  		require.NoError(t, fsys.Mkdir(".git", 0755))
    18  		// Run test
    19  		assert.NoError(t, Run(fsys))
    20  		// Validate generated config.toml
    21  		exists, err := afero.Exists(fsys, utils.ConfigPath)
    22  		assert.NoError(t, err)
    23  		assert.True(t, exists)
    24  		// Validate generated .gitignore
    25  		ignorePath := filepath.Join(filepath.Dir(utils.ConfigPath), ".gitignore")
    26  		exists, err = afero.Exists(fsys, ignorePath)
    27  		assert.NoError(t, err)
    28  		assert.True(t, exists)
    29  		// Validate generated seed.sql
    30  		exists, err = afero.Exists(fsys, utils.SeedDataPath)
    31  		assert.NoError(t, err)
    32  		assert.True(t, exists)
    33  	})
    34  
    35  	t.Run("does not generate gitignore if no git", func(t *testing.T) {
    36  		// Setup read-only fs
    37  		fsys := &afero.MemMapFs{}
    38  		// Run test
    39  		assert.NoError(t, Run(fsys))
    40  		// Validate generated config.toml
    41  		exists, err := afero.Exists(fsys, utils.ConfigPath)
    42  		assert.NoError(t, err)
    43  		assert.True(t, exists)
    44  		// Validate generated .gitignore
    45  		ignorePath := filepath.Join(filepath.Dir(utils.ConfigPath), ".gitignore")
    46  		exists, err = afero.Exists(fsys, ignorePath)
    47  		assert.NoError(t, err)
    48  		assert.False(t, exists)
    49  		// Validate generated seed.sql
    50  		exists, err = afero.Exists(fsys, utils.SeedDataPath)
    51  		assert.NoError(t, err)
    52  		assert.True(t, exists)
    53  	})
    54  
    55  	t.Run("throws error when config file exists", func(t *testing.T) {
    56  		// Setup in-memory fs
    57  		fsys := &afero.MemMapFs{}
    58  		_, err := fsys.Create(utils.ConfigPath)
    59  		require.NoError(t, err)
    60  		// Run test
    61  		assert.Error(t, Run(fsys))
    62  	})
    63  
    64  	t.Run("throws error on failure to write config", func(t *testing.T) {
    65  		// Setup read-only fs
    66  		fsys := afero.NewReadOnlyFs(afero.NewMemMapFs())
    67  		// Run test
    68  		assert.Error(t, Run(fsys))
    69  	})
    70  }
    71  
    72  func TestUpdateGitIgnore(t *testing.T) {
    73  	const ignorePath = "/home/supabase/.gitignore"
    74  
    75  	t.Run("appends to git ignore", func(t *testing.T) {
    76  		// Setup in-memory fs
    77  		fsys := &afero.MemMapFs{}
    78  		_, err := fsys.Create(ignorePath)
    79  		require.NoError(t, err)
    80  		// Run test
    81  		assert.NoError(t, updateGitIgnore(ignorePath, fsys))
    82  		// Validate file contents
    83  		content, err := afero.ReadFile(fsys, ignorePath)
    84  		assert.NoError(t, err)
    85  		assert.Equal(t, append([]byte("\n"), initGitignore...), content)
    86  	})
    87  
    88  	t.Run("noop if already ignored", func(t *testing.T) {
    89  		// Setup read-only fs
    90  		fsys := &afero.MemMapFs{}
    91  		require.NoError(t, afero.WriteFile(fsys, ignorePath, initGitignore, 0644))
    92  		// Run test
    93  		assert.NoError(t, updateGitIgnore(ignorePath, fsys))
    94  		// Validate file contents
    95  		content, err := afero.ReadFile(fsys, ignorePath)
    96  		assert.NoError(t, err)
    97  		assert.Equal(t, initGitignore, content)
    98  	})
    99  
   100  	t.Run("throws error on failure to create", func(t *testing.T) {
   101  		// Setup read-only fs
   102  		fsys := afero.NewReadOnlyFs(afero.NewMemMapFs())
   103  		// Run test
   104  		assert.Error(t, updateGitIgnore(ignorePath, fsys))
   105  	})
   106  }