github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/scm/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"testing"
     9  
    10  	testcmd "github.com/kubesphere/s2irun/pkg/test/cmd"
    11  	testfs "github.com/kubesphere/s2irun/pkg/test/fs"
    12  	"github.com/kubesphere/s2irun/pkg/utils/fs"
    13  )
    14  
    15  func TestIsValidGitRepository(t *testing.T) {
    16  	fileSystem := fs.NewFileSystem()
    17  
    18  	// a local git repo with a commit
    19  	d, err := CreateLocalGitDirectory()
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	defer os.RemoveAll(d)
    24  
    25  	ok, err := IsLocalNonBareGitRepository(fileSystem, d)
    26  	if !ok || err != nil {
    27  		t.Errorf("IsLocalNonBareGitRepository returned %v, %v", ok, err)
    28  	}
    29  	empty, err := LocalNonBareGitRepositoryIsEmpty(fileSystem, d)
    30  	if empty || err != nil {
    31  		t.Errorf("LocalNonBareGitRepositoryIsEmpty returned %v, %v", ok, err)
    32  	}
    33  
    34  	// a local git repo with no commit
    35  	d, err = CreateEmptyLocalGitDirectory()
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	defer os.RemoveAll(d)
    40  
    41  	ok, err = IsLocalNonBareGitRepository(fileSystem, d)
    42  	if !ok || err != nil {
    43  		t.Errorf("IsLocalNonBareGitRepository returned %v, %v", ok, err)
    44  	}
    45  	empty, err = LocalNonBareGitRepositoryIsEmpty(fileSystem, d)
    46  	if !empty || err != nil {
    47  		t.Errorf("LocalNonBareGitRepositoryIsEmpty returned %v, %v", ok, err)
    48  	}
    49  
    50  	// a directory which is not a git repo
    51  	d = filepath.Join(d, ".git")
    52  
    53  	ok, err = IsLocalNonBareGitRepository(fileSystem, d)
    54  	if ok || err != nil {
    55  		t.Errorf("IsLocalNonBareGitRepository returned %v, %v", ok, err)
    56  	}
    57  
    58  	// a submodule git repo with a commit
    59  	d, err = CreateLocalGitDirectoryWithSubmodule()
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer os.RemoveAll(d)
    64  
    65  	ok, err = IsLocalNonBareGitRepository(fileSystem, filepath.Join(d, "submodule"))
    66  	if !ok || err != nil {
    67  		t.Errorf("IsLocalNonBareGitRepository returned %v, %v", ok, err)
    68  	}
    69  	empty, err = LocalNonBareGitRepositoryIsEmpty(fileSystem, filepath.Join(d, "submodule"))
    70  	if empty || err != nil {
    71  		t.Errorf("LocalNonBareGitRepositoryIsEmpty returned %v, %v", ok, err)
    72  	}
    73  }
    74  
    75  func getGit() (Git, *testcmd.FakeCmdRunner) {
    76  	cr := &testcmd.FakeCmdRunner{}
    77  	gh := New(&testfs.FakeFileSystem{}, cr)
    78  
    79  	return gh, cr
    80  }
    81  
    82  func TestGitClone(t *testing.T) {
    83  	gh, ch := getGit()
    84  	err := gh.Clone(MustParse("source1"), "target1", CloneConfig{Quiet: true, Recursive: true})
    85  	if err != nil {
    86  		t.Errorf("Unexpected error returned from clone: %v", err)
    87  	}
    88  	if ch.Name != "git" {
    89  		t.Errorf("Unexpected command name: %q", ch.Name)
    90  	}
    91  	if !reflect.DeepEqual(ch.Args, []string{"clone", "--quiet", "--recursive", "source1", "target1"}) {
    92  		t.Errorf("Unexpected command arguments: %#v", ch.Args)
    93  	}
    94  }
    95  
    96  func TestGitCloneError(t *testing.T) {
    97  	gh, ch := getGit()
    98  	runErr := fmt.Errorf("Run Error")
    99  	ch.Err = runErr
   100  	err := gh.Clone(MustParse("source1"), "target1", CloneConfig{})
   101  	if err != runErr {
   102  		t.Errorf("Unexpected error returned from clone: %v", err)
   103  	}
   104  }
   105  
   106  func TestGitCheckout(t *testing.T) {
   107  	gh, ch := getGit()
   108  	err := gh.Checkout("repo1", "ref1")
   109  	if err != nil {
   110  		t.Errorf("Unexpected error returned from checkout: %v", err)
   111  	}
   112  	if ch.Name != "git" {
   113  		t.Errorf("Unexpected command name: %q", ch.Name)
   114  	}
   115  	if !reflect.DeepEqual(ch.Args, []string{"checkout", "ref1"}) {
   116  		t.Errorf("Unexpected command arguments: %#v", ch.Args)
   117  	}
   118  	if ch.Opts.Dir != "repo1" {
   119  		t.Errorf("Unexpected value in exec directory: %q", ch.Opts.Dir)
   120  	}
   121  }
   122  
   123  func TestGitCheckoutError(t *testing.T) {
   124  	gh, ch := getGit()
   125  	runErr := fmt.Errorf("Run Error")
   126  	ch.Err = runErr
   127  	err := gh.Checkout("repo1", "ref1")
   128  	if err != runErr {
   129  		t.Errorf("Unexpected error returned from checkout: %v", err)
   130  	}
   131  }