github.com/almamedia/fargate@v0.2.4-0.20220704071213-7b5b3d27c5eb/git/main_test.go (about)

     1  package git
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestGetShortSha(t *testing.T) {
    12  	cwd, err := os.Getwd()
    13  
    14  	if err != nil {
    15  		t.Error("Could not read current working directory", err)
    16  		return
    17  	}
    18  
    19  	dir, err := ioutil.TempDir("", "fargate-tests")
    20  
    21  	if err != nil {
    22  		t.Error("Could not create temporary directory", err)
    23  		return
    24  	}
    25  	defer os.RemoveAll(dir)
    26  
    27  	os.Chdir(dir)
    28  	defer os.Chdir(cwd)
    29  
    30  	exec.Command("git", "init").Run()
    31  
    32  	gitCommit := exec.Command("git", "commit", "--allow-empty", "--message", "dummy commit")
    33  	commitOutput, err := gitCommit.CombinedOutput()
    34  
    35  	if err != nil {
    36  		t.Errorf("Could not create dummy git commit: %v", err)
    37  		t.Errorf("Output: %s", commitOutput)
    38  		t.FailNow()
    39  	}
    40  
    41  	if shortSha := GetShortSha(); !strings.Contains(string(commitOutput), GetShortSha()) {
    42  		t.Errorf("expected %s to contain %s", commitOutput, shortSha)
    43  	}
    44  }
    45  
    46  func TestIsCwdGitRepoAgainstADir(t *testing.T) {
    47  	cwd, err := os.Getwd()
    48  
    49  	if err != nil {
    50  		t.Error("Could not read current working directory", err)
    51  		return
    52  	}
    53  
    54  	dir, err := ioutil.TempDir("", "fargate-tests")
    55  
    56  	if err != nil {
    57  		t.Error("Could not create temporary directory", err)
    58  		return
    59  	}
    60  	defer os.RemoveAll(dir)
    61  
    62  	os.Chdir(dir)
    63  	defer os.Chdir(cwd)
    64  
    65  	if isCwdGitRepo := IsCwdGitRepo(); isCwdGitRepo {
    66  		t.Errorf("wanted false, got %+v", isCwdGitRepo)
    67  	}
    68  }
    69  
    70  func TestIsCwdGitRepoAgainstARepo(t *testing.T) {
    71  	cwd, err := os.Getwd()
    72  
    73  	if err != nil {
    74  		t.Error("Could not read current working directory", err)
    75  		return
    76  	}
    77  
    78  	dir, err := ioutil.TempDir("", "fargate-tests")
    79  
    80  	if err != nil {
    81  		t.Error("Could not create temporary directory", err)
    82  		return
    83  	}
    84  	defer os.RemoveAll(dir)
    85  
    86  	os.Chdir(dir)
    87  	defer os.Chdir(cwd)
    88  
    89  	cmd := exec.Command("git", "init")
    90  	cmd.Run()
    91  
    92  	if isCwdGitRepo := IsCwdGitRepo(); !isCwdGitRepo {
    93  		t.Errorf("wanted true, got %+v", isCwdGitRepo)
    94  	}
    95  }