github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/internal/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type MockExecutor struct {
     8  	Args    []string
     9  	Command string
    10  	Debug   bool
    11  	Output  []byte
    12  }
    13  
    14  func (me *MockExecutor) Exec(execType string, command string, args ...string) ([]byte, error) {
    15  	me.Command = command
    16  	me.Args = args
    17  	return me.Output, nil
    18  }
    19  
    20  func Test_Checkout(t *testing.T) {
    21  	tests := []struct {
    22  		expectedArgs []string
    23  	}{
    24  		{expectedArgs: []string{"checkout", "branch"}},
    25  	}
    26  
    27  	for _, test := range tests {
    28  		executor := &MockExecutor{Debug: true}
    29  
    30  		g := NewGit(true, executor)
    31  		g.Checkout("branch")
    32  
    33  		if executor.Command != "git" {
    34  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
    35  		}
    36  
    37  		if len(executor.Args) != len(test.expectedArgs) {
    38  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
    39  		}
    40  
    41  		for i, v := range executor.Args {
    42  			if v != test.expectedArgs[i] {
    43  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
    44  			}
    45  		}
    46  	}
    47  }
    48  
    49  func Test_CleanDeletedBranches(t *testing.T) {
    50  	tests := []struct {
    51  		expectedArgs []string
    52  	}{
    53  		{expectedArgs: []string{"branch", "-vv"}},
    54  	}
    55  
    56  	for _, test := range tests {
    57  		executor := &MockExecutor{Debug: true}
    58  
    59  		g := NewGit(true, executor)
    60  		g.CleanDeletedBranches()
    61  
    62  		if executor.Command != "git" {
    63  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
    64  		}
    65  
    66  		if len(executor.Args) != len(test.expectedArgs) {
    67  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
    68  		}
    69  
    70  		for i, v := range executor.Args {
    71  			if v != test.expectedArgs[i] {
    72  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
    73  			}
    74  		}
    75  	}
    76  }
    77  
    78  func Test_CreateBranch(t *testing.T) {
    79  	tests := []struct {
    80  		expectedArgs []string
    81  	}{
    82  		{expectedArgs: []string{"branch", "--no-track", "branch"}},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		executor := &MockExecutor{Debug: true}
    87  
    88  		g := NewGit(true, executor)
    89  		g.CreateBranch("branch")
    90  
    91  		if executor.Command != "git" {
    92  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
    93  		}
    94  
    95  		if len(executor.Args) != len(test.expectedArgs) {
    96  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
    97  		}
    98  
    99  		for i, v := range executor.Args {
   100  			if v != test.expectedArgs[i] {
   101  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   102  			}
   103  		}
   104  	}
   105  }
   106  
   107  func Test_CreateEmptyCommit(t *testing.T) {
   108  	tests := []struct {
   109  		expectedArgs []string
   110  	}{
   111  		{expectedArgs: []string{"commit", "--allow-empty", "-m", "Empty commit"}},
   112  	}
   113  
   114  	for _, test := range tests {
   115  		executor := &MockExecutor{Debug: true}
   116  
   117  		g := NewGit(true, executor)
   118  		g.CreateEmptyCommit()
   119  
   120  		if executor.Command != "git" {
   121  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   122  		}
   123  
   124  		if len(executor.Args) != len(test.expectedArgs) {
   125  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   126  		}
   127  
   128  		for i, v := range executor.Args {
   129  			if v != test.expectedArgs[i] {
   130  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   131  			}
   132  		}
   133  	}
   134  }
   135  
   136  func Test_CurrentBranch(t *testing.T) {
   137  	tests := []struct {
   138  		expectedArgs   []string
   139  		expectedOutput string
   140  		finalOutput    string
   141  	}{
   142  		{
   143  			expectedArgs:   []string{"branch"},
   144  			expectedOutput: "* master",
   145  			finalOutput:    "master",
   146  		},
   147  	}
   148  
   149  	for _, test := range tests {
   150  		executor := &MockExecutor{
   151  			Debug:  true,
   152  			Output: []byte(test.expectedOutput),
   153  		}
   154  
   155  		g := NewGit(true, executor)
   156  		o := g.CurrentBranch()
   157  
   158  		if executor.Command != "git" {
   159  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   160  		}
   161  
   162  		if len(executor.Args) != len(test.expectedArgs) {
   163  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   164  		}
   165  
   166  		for i, v := range executor.Args {
   167  			if v != test.expectedArgs[i] {
   168  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   169  			}
   170  		}
   171  
   172  		if string(executor.Output) != test.expectedOutput {
   173  			t.Errorf("unexpected output received: expected %s, but got %s", test.expectedOutput, executor.Output)
   174  		}
   175  
   176  		if o != test.finalOutput {
   177  			t.Errorf("unexpected output received: expected %s, but got %s", test.finalOutput, o)
   178  		}
   179  	}
   180  }
   181  
   182  func Test_DefaultBranch(t *testing.T) {
   183  	tests := []struct {
   184  		expectedArgs   []string
   185  		expectedOutput string
   186  		finalOutput    string
   187  	}{
   188  		{
   189  			expectedArgs:   []string{"symbolic-ref", "refs/remotes/origin/HEAD"},
   190  			expectedOutput: "refs/remotes/origin/master",
   191  			finalOutput:    "master",
   192  		},
   193  	}
   194  
   195  	for _, test := range tests {
   196  		executor := &MockExecutor{
   197  			Debug:  true,
   198  			Output: []byte(test.expectedOutput),
   199  		}
   200  
   201  		g := NewGit(true, executor)
   202  		o := g.DefaultBranch()
   203  
   204  		if executor.Command != "git" {
   205  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   206  		}
   207  
   208  		if len(executor.Args) != len(test.expectedArgs) {
   209  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   210  		}
   211  
   212  		for i, v := range executor.Args {
   213  			if v != test.expectedArgs[i] {
   214  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   215  			}
   216  		}
   217  
   218  		if string(executor.Output) != test.expectedOutput {
   219  			t.Errorf("unexpected output received: expected %s, but got %s", test.expectedOutput, executor.Output)
   220  		}
   221  
   222  		if o != test.finalOutput {
   223  			t.Errorf("unexpected output received: expected %s, but got %s", test.finalOutput, o)
   224  		}
   225  	}
   226  }
   227  
   228  func Test_Fetch(t *testing.T) {
   229  	tests := []struct {
   230  		expectedArgs []string
   231  	}{
   232  		{expectedArgs: []string{"fetch", "-p"}},
   233  	}
   234  
   235  	for _, test := range tests {
   236  		executor := &MockExecutor{Debug: true}
   237  
   238  		g := NewGit(true, executor)
   239  		g.Fetch()
   240  
   241  		if executor.Command != "git" {
   242  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   243  		}
   244  
   245  		if len(executor.Args) != len(test.expectedArgs) {
   246  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   247  		}
   248  
   249  		for i, v := range executor.Args {
   250  			if v != test.expectedArgs[i] {
   251  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   252  			}
   253  		}
   254  	}
   255  }
   256  
   257  func Test_GetGitRootDir(t *testing.T) {
   258  	tests := []struct {
   259  		expectedArgs   []string
   260  		expectedOutput string
   261  	}{
   262  		{
   263  			expectedArgs:   []string{"rev-parse", "--show-toplevel"},
   264  			expectedOutput: "go-git-helper",
   265  		},
   266  	}
   267  
   268  	for _, test := range tests {
   269  		executor := &MockExecutor{
   270  			Debug:  true,
   271  			Output: []byte(test.expectedOutput),
   272  		}
   273  
   274  		g := NewGit(true, executor)
   275  		o := g.GetGitRootDir()
   276  
   277  		if executor.Command != "git" {
   278  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   279  		}
   280  
   281  		if len(executor.Args) != len(test.expectedArgs) {
   282  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   283  		}
   284  
   285  		for i, v := range executor.Args {
   286  			if v != test.expectedArgs[i] {
   287  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   288  			}
   289  		}
   290  
   291  		if string(executor.Output) != test.expectedOutput {
   292  			t.Errorf("unexpected output received: expected %s, but got %s", test.expectedOutput, executor.Output)
   293  		}
   294  
   295  		if o != test.expectedOutput {
   296  			t.Errorf("unexpected output received: expected %s, but got %s", test.expectedOutput, o)
   297  
   298  		}
   299  	}
   300  }
   301  
   302  func Test_Pull(t *testing.T) {
   303  	tests := []struct {
   304  		expectedArgs []string
   305  	}{
   306  		{expectedArgs: []string{"pull"}},
   307  	}
   308  
   309  	for _, test := range tests {
   310  		executor := &MockExecutor{Debug: true}
   311  
   312  		g := NewGit(true, executor)
   313  		g.Pull()
   314  
   315  		if executor.Command != "git" {
   316  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   317  		}
   318  
   319  		if len(executor.Args) != len(test.expectedArgs) {
   320  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   321  		}
   322  
   323  		for i, v := range executor.Args {
   324  			if v != test.expectedArgs[i] {
   325  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   326  			}
   327  		}
   328  	}
   329  }
   330  
   331  func Test_PushBranch(t *testing.T) {
   332  	tests := []struct {
   333  		expectedArgs []string
   334  	}{
   335  		{expectedArgs: []string{"push", "--set-upstream", "origin", "branch"}},
   336  	}
   337  
   338  	for _, test := range tests {
   339  		executor := &MockExecutor{Debug: true}
   340  
   341  		g := NewGit(true, executor)
   342  		g.PushBranch("branch")
   343  
   344  		if executor.Command != "git" {
   345  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   346  		}
   347  
   348  		if len(executor.Args) != len(test.expectedArgs) {
   349  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   350  		}
   351  
   352  		for i, v := range executor.Args {
   353  			if v != test.expectedArgs[i] {
   354  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   355  			}
   356  		}
   357  	}
   358  }
   359  
   360  func Test_RepoName(t *testing.T) {
   361  	tests := []struct {
   362  		name           string
   363  		expectedArgs   []string
   364  		expectedOutput string
   365  		repoName       string
   366  	}{
   367  		{
   368  			name:         "SSH remote without .git",
   369  			expectedArgs: []string{"remote", "-v"},
   370  			expectedOutput: `origin  git@github.com:emmahsax/go-git-helper (fetch)
   371  origin  git@github.com:emmahsax/go-git-helper (push)`,
   372  			repoName: "emmahsax/go-git-helper",
   373  		},
   374  		{
   375  			name:         "SSH remote with .git",
   376  			expectedArgs: []string{"remote", "-v"},
   377  			expectedOutput: `origin  git@github.com:emmahsax/go-git-helper.git (fetch)
   378  origin  git@github.com:emmahsax/go-git-helper.git (push)`,
   379  			repoName: "emmahsax/go-git-helper",
   380  		},
   381  		{
   382  			name:         "HTTP remote without .git",
   383  			expectedArgs: []string{"remote", "-v"},
   384  			expectedOutput: `origin  https://github.com/emmahsax/go-git-helper (fetch)
   385  origin  https://github.com/emmahsax/go-git-helper (push)`,
   386  			repoName: "emmahsax/go-git-helper",
   387  		},
   388  		{
   389  			name:         "HTTP remote with .git",
   390  			expectedArgs: []string{"remote", "-v"},
   391  			expectedOutput: `origin  https://github.com/emmahsax/go-git-helper.git (fetch)
   392  origin  https://github.com/emmahsax/go-git-helper.git (push)`,
   393  			repoName: "emmahsax/go-git-helper",
   394  		},
   395  	}
   396  
   397  	for _, test := range tests {
   398  		executor := &MockExecutor{
   399  			Debug:  true,
   400  			Output: []byte(test.expectedOutput),
   401  		}
   402  
   403  		g := NewGit(true, executor)
   404  		r := g.RepoName()
   405  
   406  		if executor.Command != "git" {
   407  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   408  		}
   409  
   410  		if len(executor.Args) != len(test.expectedArgs) {
   411  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   412  		}
   413  
   414  		for i, v := range executor.Args {
   415  			if v != test.expectedArgs[i] {
   416  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   417  			}
   418  		}
   419  
   420  		if string(executor.Output) != test.expectedOutput {
   421  			t.Errorf("unexpected output received: expected %s, but got %s", test.expectedOutput, executor.Output)
   422  		}
   423  
   424  		if r != test.repoName {
   425  			t.Errorf("%s unexpected output received: expected %s, but got %s", test.name, test.repoName, r)
   426  		}
   427  	}
   428  }
   429  
   430  func Test_Remotes(t *testing.T) {
   431  	tests := []struct {
   432  		expectedArgs   []string
   433  		expectedOutput string
   434  		remotes        []string
   435  	}{
   436  		{
   437  			expectedArgs: []string{"remote", "-v"},
   438  			expectedOutput: `origin  git@github.com:emmahsax/go-git-helper.git (fetch)
   439  origin  git@github.com:emmahsax/go-git-helper.git (push)`,
   440  			remotes: []string{"origin  git@github.com:emmahsax/go-git-helper.git (fetch)", "origin  git@github.com:emmahsax/go-git-helper.git (push)"},
   441  		},
   442  	}
   443  
   444  	for _, test := range tests {
   445  		executor := &MockExecutor{
   446  			Debug:  true,
   447  			Output: []byte(test.expectedOutput),
   448  		}
   449  
   450  		g := NewGit(true, executor)
   451  		r := g.Remotes()
   452  
   453  		if executor.Command != "git" {
   454  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   455  		}
   456  
   457  		if len(executor.Args) != len(test.expectedArgs) {
   458  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   459  		}
   460  
   461  		for i, v := range executor.Args {
   462  			if v != test.expectedArgs[i] {
   463  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   464  			}
   465  		}
   466  
   467  		if string(executor.Output) != test.expectedOutput {
   468  			t.Errorf("unexpected output received: expected %s, but got %s", test.expectedOutput, executor.Output)
   469  		}
   470  
   471  		for i, v := range r {
   472  			if v != test.remotes[i] {
   473  				t.Errorf("unexpected args received: expected %v, but got %v", test.remotes, r)
   474  			}
   475  		}
   476  	}
   477  }
   478  
   479  func Test_Reset(t *testing.T) {
   480  	tests := []struct {
   481  		expectedArgs []string
   482  	}{
   483  		{expectedArgs: []string{"reset", "--hard", "origin/HEAD"}},
   484  	}
   485  
   486  	for _, test := range tests {
   487  		executor := &MockExecutor{Debug: true}
   488  
   489  		g := NewGit(true, executor)
   490  		g.Reset()
   491  
   492  		if executor.Command != "git" {
   493  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   494  		}
   495  
   496  		if len(executor.Args) != len(test.expectedArgs) {
   497  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   498  		}
   499  
   500  		for i, v := range executor.Args {
   501  			if v != test.expectedArgs[i] {
   502  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   503  			}
   504  		}
   505  	}
   506  }
   507  
   508  func Test_SetHeadRef(t *testing.T) {
   509  	tests := []struct {
   510  		expectedArgs []string
   511  	}{
   512  		{expectedArgs: []string{"symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/branch"}},
   513  	}
   514  
   515  	for _, test := range tests {
   516  		executor := &MockExecutor{Debug: true}
   517  
   518  		g := NewGit(true, executor)
   519  		g.SetHeadRef("branch")
   520  
   521  		if executor.Command != "git" {
   522  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   523  		}
   524  
   525  		if len(executor.Args) != len(test.expectedArgs) {
   526  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   527  		}
   528  
   529  		for i, v := range executor.Args {
   530  			if v != test.expectedArgs[i] {
   531  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   532  			}
   533  		}
   534  	}
   535  }
   536  
   537  func Test_Stash(t *testing.T) {
   538  	tests := []struct {
   539  		expectedArgs []string
   540  	}{
   541  		{expectedArgs: []string{"stash"}},
   542  	}
   543  
   544  	for _, test := range tests {
   545  		executor := &MockExecutor{Debug: true}
   546  
   547  		g := NewGit(true, executor)
   548  		g.Stash()
   549  
   550  		if executor.Command != "git" {
   551  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   552  		}
   553  
   554  		if len(executor.Args) != len(test.expectedArgs) {
   555  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   556  		}
   557  
   558  		for i, v := range executor.Args {
   559  			if v != test.expectedArgs[i] {
   560  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   561  			}
   562  		}
   563  	}
   564  }
   565  
   566  func Test_StashDrop(t *testing.T) {
   567  	tests := []struct {
   568  		expectedArgs []string
   569  	}{
   570  		{expectedArgs: []string{"stash", "drop"}},
   571  	}
   572  
   573  	for _, test := range tests {
   574  		executor := &MockExecutor{Debug: true}
   575  
   576  		g := NewGit(true, executor)
   577  		g.StashDrop()
   578  
   579  		if executor.Command != "git" {
   580  			t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command)
   581  		}
   582  
   583  		if len(executor.Args) != len(test.expectedArgs) {
   584  			t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   585  		}
   586  
   587  		for i, v := range executor.Args {
   588  			if v != test.expectedArgs[i] {
   589  				t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args)
   590  			}
   591  		}
   592  	}
   593  }