github.com/titpetric/pendulum@v0.1.180207-1512.0.20180514135826-1f399445df57/cmd/pendulum/git_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"io/ioutil"
     9  )
    10  
    11  func TestGit(t *testing.T) {
    12  	// Set up clean git repo in test/ folder
    13  	git := Git{}
    14  	git.exec(strings.Split("rm -rf test", " "))
    15  	os.Mkdir("test", 0777)
    16  	os.Chdir("test")
    17  	git.exec([]string{"git", "init"})
    18  
    19  	// Verify empty user settings
    20  	if val := git.UserName(); val != "" {
    21  		t.Error("Expected name=empty, got " + val)
    22  	}
    23  	if val := git.UserEmail(); val != "" {
    24  		t.Error("Expected email=empty, got " + val)
    25  	}
    26  
    27  	// Set local repo settings for test
    28  	git.exec([]string{"git", "config", "user.name", "Tit Petric"})
    29  	git.exec([]string{"git", "config", "user.email", "black@scene-si.org"})
    30  
    31  	// Verify new user settings
    32  	if val := git.UserName(); val != "Tit Petric" {
    33  		t.Error("Expected name=Tit Petric, got " + val)
    34  	}
    35  	if val := git.UserEmail(); val != "black@scene-si.org" {
    36  		t.Error("Expected email=black@scene-si.org, got " + val)
    37  	}
    38  	os.Chdir("..")
    39  
    40  	// Write a file and commit it, check that path is restored to previous cwd.
    41  	ioutil.WriteFile("test/test.txt", []byte("hello world, I am from the future"), 0644)
    42  	git.Filename = "test/test.txt"
    43  	cwd1, _ := os.Getwd()
    44  	_, err := git.Commit()
    45  	cwd2, _ := os.Getwd()
    46  	if err != nil {
    47  		t.Error(err)
    48  	}
    49  	if cwd1 != cwd2 {
    50  		t.Error("Should reset working directory back to root")
    51  	}
    52  
    53  	// Check we made one commit
    54  	os.Chdir("test")
    55  	output := git.exec(strings.Split("git log --format=oneline", " "))
    56  	if strings.Contains(output, "\n") {
    57  		t.Errorf("Expected one commit in the git log, got: '%s'", output)
    58  	}
    59  	os.Chdir("..")
    60  }