github.com/arsham/gitrelease@v0.3.2-0.20221207124258-6867180b2c2d/commit/git_test.go (about)

     1  package commit_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	"github.com/arsham/gitrelease/commit"
    10  	"github.com/blokur/testament"
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestGit(t *testing.T) {
    17  	t.Parallel()
    18  	t.Run("LatestTag", testGitLatestTag)
    19  	t.Run("PreviousTag", testGitPreviousTag)
    20  	t.Run("Commits", testGitCommits)
    21  	t.Run("RepoInfo", testGitRepoInfo)
    22  }
    23  
    24  func testGitLatestTag(t *testing.T) {
    25  	t.Parallel()
    26  	ctx := context.Background()
    27  	dir := createGitRepo(t)
    28  
    29  	g := commit.Git{
    30  		Dir: dir,
    31  	}
    32  
    33  	_, err := g.LatestTag(ctx)
    34  	assert.Error(t, err)
    35  
    36  	createFile(t, dir, "file.txt", testament.RandomString(20))
    37  	commitChanges(t, dir, testament.RandomString(20))
    38  	createGitTag(t, dir, "v0.0.1")
    39  
    40  	got, err := g.LatestTag(ctx)
    41  	require.NoError(t, err)
    42  	assert.Equal(t, "v0.0.1", got)
    43  
    44  	createFile(t, dir, "file2.txt", testament.RandomString(20))
    45  	commitChanges(t, dir, testament.RandomString(20))
    46  	createGitTag(t, dir, "v0.0.2")
    47  
    48  	createFile(t, dir, "file3.txt", testament.RandomString(20))
    49  	commitChanges(t, dir, testament.RandomString(20))
    50  	got, err = g.LatestTag(ctx)
    51  	require.NoError(t, err)
    52  	assert.Equal(t, "v0.0.2", got)
    53  }
    54  
    55  func testGitPreviousTag(t *testing.T) {
    56  	t.Parallel()
    57  	ctx := context.Background()
    58  	dir := createGitRepo(t)
    59  
    60  	g := commit.Git{
    61  		Dir: dir,
    62  	}
    63  
    64  	_, err := g.PreviousTag(ctx, "v0.0.10")
    65  	assert.Error(t, err)
    66  
    67  	createFile(t, dir, "file.txt", testament.RandomString(20))
    68  	commitChanges(t, dir, testament.RandomString(20))
    69  	createGitTag(t, dir, "v0.0.1")
    70  
    71  	createFile(t, dir, "file2.txt", testament.RandomString(20))
    72  	commitChanges(t, dir, testament.RandomString(20))
    73  	createGitTag(t, dir, "v0.0.2")
    74  
    75  	got, err := g.PreviousTag(ctx, "v0.0.2")
    76  	require.NoError(t, err)
    77  	assert.Equal(t, "v0.0.1", got)
    78  
    79  	createFile(t, dir, "file3.txt", testament.RandomString(20))
    80  	commitChanges(t, dir, testament.RandomString(20))
    81  	got, err = g.PreviousTag(ctx, "@")
    82  	require.NoError(t, err)
    83  	assert.Equal(t, "v0.0.2", got)
    84  }
    85  
    86  func testGitCommits(t *testing.T) {
    87  	t.Parallel()
    88  	ctx := context.Background()
    89  	dir := createGitRepo(t)
    90  
    91  	g := commit.Git{
    92  		Dir: dir,
    93  	}
    94  
    95  	filename := "file.txt"
    96  
    97  	createFile(t, dir, filename, testament.RandomString(20))
    98  	commitChanges(t, dir, "msg1")
    99  	createGitTag(t, dir, "v0.0.1")
   100  
   101  	msgs := []string{"msg1", "msg2", "msg3"}
   102  	for _, msg := range msgs {
   103  		appendToFile(t, dir, filename, testament.RandomString(20))
   104  		commitChanges(t, dir, msg)
   105  	}
   106  
   107  	createGitTag(t, dir, "v0.0.2")
   108  
   109  	got, err := g.Commits(ctx, "v0.0.1", "v0.0.2")
   110  	require.NoError(t, err)
   111  	if diff := cmp.Diff(msgs, got, commitComparer...); diff != "" {
   112  		t.Errorf("(-want +got):\n%s", diff)
   113  	}
   114  }
   115  
   116  func testGitRepoInfo(t *testing.T) {
   117  	t.Run("Repo", testGitRepoInfoRepo)
   118  	t.Run("Remote", testGitRepoInfoRemote)
   119  }
   120  
   121  func testGitRepoInfoRepo(t *testing.T) {
   122  	t.Parallel()
   123  
   124  	wantUser := "arsham666"
   125  	wantRepo := "gitrelease777"
   126  	addrs := map[string]struct {
   127  		addr     string
   128  		wantUser string
   129  		wantRepo string
   130  	}{
   131  		"git protocol":          {"git@github.com:%s/%s", wantUser, wantRepo},
   132  		"git protocol dot":      {"git@github.com:%s/%s", wantUser, wantRepo + ".nvim"},
   133  		"git protocol tail":     {"git@github.com:%s/%s.git", wantUser, wantRepo},
   134  		"git protocol tail dot": {"git@github.com:%s/%s.git", wantUser, wantRepo + ".nvim"},
   135  		"no protocol":           {"github.com/%s/%s", wantUser, wantRepo},
   136  		"no protocol dot":       {"github.com/%s/%s", wantUser, wantRepo + ".nvim"},
   137  		"no protocol tail":      {"github.com/%s/%s.git", wantUser, wantRepo},
   138  		"no protocol tail dot":  {"github.com/%s/%s.git", wantUser, wantRepo + ".nvim"},
   139  		"protocol":              {"https://github.com/%s/%s", wantUser, wantRepo},
   140  		"protocol dot":          {"https://github.com/%s/%s", wantUser, wantRepo + ".nvim"},
   141  		"protocol tail":         {"https://github.com/%s/%s.git", wantUser, wantRepo},
   142  		"protocol tail dot":     {"https://github.com/%s/%s.git", wantUser, wantRepo + ".nvim"},
   143  	}
   144  
   145  	for name, tc := range addrs {
   146  		tc := tc
   147  		t.Run(name, func(t *testing.T) {
   148  			dir := createGitRepo(t)
   149  			addr := fmt.Sprintf(tc.addr, tc.wantUser, tc.wantRepo)
   150  			g := commit.Git{
   151  				Dir: dir,
   152  			}
   153  			args := []string{
   154  				"remote",
   155  				"add",
   156  				"origin",
   157  				addr,
   158  			}
   159  			cmd := exec.CommandContext(context.Background(), "git", args...)
   160  			cmd.Dir = dir
   161  			out, err := cmd.CombinedOutput()
   162  			require.NoError(t, err, string(out))
   163  
   164  			user, repo, err := g.RepoInfo(context.Background())
   165  			require.NoError(t, err, addr)
   166  			assert.Equal(t, tc.wantUser, user)
   167  			assert.Equal(t, tc.wantRepo, repo)
   168  		})
   169  	}
   170  }
   171  
   172  func testGitRepoInfoRemote(t *testing.T) {
   173  	t.Parallel()
   174  	dir := createGitRepo(t)
   175  	g := commit.Git{
   176  		Dir: dir,
   177  	}
   178  
   179  	setup := []struct {
   180  		addr   string
   181  		remote string
   182  	}{{
   183  		addr:   "git@github.com:arsham/shark.git",
   184  		remote: "origin",
   185  	}, {
   186  		addr:   "git@github.com:arsham/arshlib.nvim.git",
   187  		remote: "other",
   188  	}}
   189  
   190  	for _, s := range setup {
   191  		args := []string{
   192  			"remote",
   193  			"add",
   194  			s.remote,
   195  			s.addr,
   196  		}
   197  		cmd := exec.CommandContext(context.Background(), "git", args...)
   198  		cmd.Dir = dir
   199  		out, err := cmd.CombinedOutput()
   200  		require.NoError(t, err, string(out))
   201  	}
   202  
   203  	user, repo, err := g.RepoInfo(context.Background())
   204  	require.NoError(t, err, setup[0].addr)
   205  	assert.Equal(t, "arsham", user)
   206  	assert.Equal(t, "shark", repo)
   207  
   208  	g.Remote = setup[0].remote
   209  	user, repo, err = g.RepoInfo(context.Background())
   210  	require.NoError(t, err, setup[0].addr)
   211  	assert.Equal(t, "arsham", user)
   212  	assert.Equal(t, "shark", repo)
   213  
   214  	g.Remote = setup[1].remote
   215  	user, repo, err = g.RepoInfo(context.Background())
   216  	require.NoError(t, err, setup[0].addr)
   217  	assert.Equal(t, "arsham", user)
   218  	assert.Equal(t, "arshlib.nvim", repo)
   219  }