github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/db/branch/delete/delete_test.go (about)

     1  package delete
     2  
     3  import (
     4  	"net/http"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/client"
     9  	"github.com/spf13/afero"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/Redstoneguy129/cli/internal/testing/apitest"
    13  	"github.com/Redstoneguy129/cli/internal/utils"
    14  	"gopkg.in/h2non/gock.v1"
    15  )
    16  
    17  func TestBranchDir(t *testing.T) {
    18  	t.Run("removes a branch directory", func(t *testing.T) {
    19  		// Setup in-memory fs
    20  		fsys := afero.NewMemMapFs()
    21  		path := filepath.Join(filepath.Dir(utils.CurrBranchPath), "test-branch")
    22  		require.NoError(t, fsys.Mkdir(path, 0755))
    23  		// Run test
    24  		assert.NoError(t, deleteBranchDir("test-branch", fsys))
    25  		// Validate removal
    26  		exists, err := afero.Exists(fsys, path)
    27  		assert.NoError(t, err)
    28  		assert.False(t, exists)
    29  	})
    30  
    31  	t.Run("branch is current", func(t *testing.T) {
    32  		// Setup in-memory fs
    33  		fsys := afero.NewMemMapFs()
    34  		require.NoError(t, afero.WriteFile(fsys, utils.CurrBranchPath, []byte("main"), 0644))
    35  		// Run test
    36  		assert.Error(t, deleteBranchDir("main", fsys))
    37  	})
    38  
    39  	t.Run("branch is reserved", func(t *testing.T) {
    40  		assert.Error(t, deleteBranchDir("main", afero.NewMemMapFs()))
    41  	})
    42  
    43  	t.Run("branch does not exist", func(t *testing.T) {
    44  		assert.Error(t, deleteBranchDir("test-branch", afero.NewMemMapFs()))
    45  	})
    46  
    47  	t.Run("branch permission denied", func(t *testing.T) {
    48  		// Setup read-only fs
    49  		fsys := afero.NewMemMapFs()
    50  		path := filepath.Join(filepath.Dir(utils.CurrBranchPath), "test-branch")
    51  		require.NoError(t, fsys.Mkdir(path, 0755))
    52  		// Run test
    53  		assert.Error(t, deleteBranchDir("test-branch", afero.NewReadOnlyFs(fsys)))
    54  	})
    55  }
    56  
    57  func TestDeleteCommand(t *testing.T) {
    58  	const (
    59  		version = "1.41"
    60  		branch  = "test-branch"
    61  	)
    62  
    63  	t.Run("throws error on missing config", func(t *testing.T) {
    64  		assert.Error(t, Run(branch, afero.NewMemMapFs()))
    65  	})
    66  
    67  	t.Run("throws error on stopped db", func(t *testing.T) {
    68  		// Setup in-memory fs
    69  		fsys := &afero.MemMapFs{}
    70  		require.NoError(t, utils.WriteConfig(fsys, false))
    71  		// Setup mock docker
    72  		require.NoError(t, client.WithHTTPClient(http.DefaultClient)(utils.Docker))
    73  		defer gock.OffAll()
    74  		gock.New("http:///var/run/docker.sock").
    75  			Head("/_ping").
    76  			Reply(http.StatusOK).
    77  			SetHeader("API-Version", version).
    78  			SetHeader("OSType", "linux")
    79  		gock.New("http:///var/run/docker.sock").
    80  			Get("/v" + version + "/containers").
    81  			Reply(http.StatusServiceUnavailable)
    82  		// Run test
    83  		assert.Error(t, Run(branch, fsys))
    84  		// Validate api
    85  		assert.Empty(t, apitest.ListUnmatchedRequests())
    86  	})
    87  }