github.com/nemith/go-gitlog@v0.0.2-0.20180205151741-6c79beb2287b/gitlog_test.go (about)

     1  package gitlog
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func rimraf(dir string) {
    14  	if err := os.RemoveAll(dir); err != nil {
    15  		log.Fatalln(err)
    16  	}
    17  }
    18  
    19  func mkdirp(dir string) {
    20  	if err := os.MkdirAll(dir, 0777); err != nil {
    21  		log.Fatalln(err)
    22  	}
    23  }
    24  
    25  func git(args ...string) string {
    26  	bytes, _ := exec.Command("git", args...).Output()
    27  	return string(bytes)
    28  }
    29  
    30  func gitCommit(msg string) string {
    31  	return git("commit", "--allow-empty", "-m", msg)
    32  }
    33  
    34  func setup() func() {
    35  	cwd, _ := filepath.Abs(".")
    36  	dir := filepath.Join(cwd, ".tmp")
    37  
    38  	mkdirp(dir)
    39  	os.Chdir(dir)
    40  
    41  	// setup test repository
    42  	git("init")
    43  	git("config", "--local", "user.name", "authorname")
    44  	git("config", "--local", "user.email", "mail@example.com")
    45  
    46  	// master
    47  	gitCommit("chore(*): Initial Commit")
    48  
    49  	// v1.0.0
    50  	git("tag", "v1.0.0")
    51  
    52  	// topic
    53  	git("checkout", "-b", "topic")
    54  	gitCommit("docs(readme): Has body commit message\n\nThis is commit message body.\nThere are no problems on multiple lines :)")
    55  
    56  	// master
    57  	git("checkout", "master")
    58  	gitCommit("feat(parser): Add foo feature")
    59  
    60  	// merge
    61  	git("merge", "--no-ff", "topic", "-m", "Merge pull request #12 from tsuyoshiwada/topic")
    62  
    63  	// 2.1.0
    64  	git("tag", "2.1.0")
    65  
    66  	gitCommit("fix(logger): Fix bar function")
    67  	gitCommit("style(*): Run GoFmt")
    68  
    69  	// v3.0.0-rc.10
    70  	git("tag", "v3.0.0-rc.10")
    71  
    72  	gitCommit("chore(release): Bump version to v0.0.0")
    73  
    74  	// 3.6.4-beta.12
    75  	git("tag", "3.6.4-beta.12")
    76  
    77  	os.Chdir(cwd)
    78  
    79  	return func() {
    80  		rimraf(dir)
    81  	}
    82  }
    83  
    84  func TestNewGitLog(t *testing.T) {
    85  	assert := assert.New(t)
    86  
    87  	assert.NotPanics(func() {
    88  		New(nil)
    89  	})
    90  
    91  	assert.NotPanics(func() {
    92  		New(&Config{})
    93  	})
    94  
    95  	assert.NotPanics(func() {
    96  		New(&Config{
    97  			Bin: "",
    98  		})
    99  	})
   100  
   101  	assert.NotPanics(func() {
   102  		New(&Config{
   103  			Path: "",
   104  		})
   105  	})
   106  }
   107  
   108  func TestGitLog(t *testing.T) {
   109  	assert := assert.New(t)
   110  
   111  	clear := setup()
   112  	defer clear()
   113  
   114  	git := New(&Config{
   115  		Path: ".tmp",
   116  	})
   117  
   118  	commits, err := git.Log(nil, nil)
   119  
   120  	assert.Nil(err)
   121  	assert.Equal(7, len(commits))
   122  
   123  	table := [][]string{
   124  		[]string{"chore(release): Bump version to v0.0.0", "", "3.6.4-beta.12"},
   125  		[]string{"style(*): Run GoFmt", "", "v3.0.0-rc.10"},
   126  		[]string{"fix(logger): Fix bar function", "", ""},
   127  		[]string{"Merge pull request #12 from tsuyoshiwada/topic", "", "2.1.0"},
   128  		[]string{"feat(parser): Add foo feature", "", ""},
   129  		[]string{"docs(readme): Has body commit message", "This is commit message body.\nThere are no problems on multiple lines :)", ""},
   130  		[]string{"chore(*): Initial Commit", "", "v1.0.0"},
   131  	}
   132  
   133  	for i, commit := range commits {
   134  		expect := table[i]
   135  
   136  		assert.Equal("authorname", commit.Author.Name)
   137  		assert.Equal("mail@example.com", commit.Author.Email)
   138  
   139  		assert.NotEmpty(commit.Hash.Long)
   140  		assert.NotEmpty(commit.Hash.Short)
   141  
   142  		assert.NotEmpty(commit.Tree.Long)
   143  		assert.NotEmpty(commit.Tree.Short)
   144  
   145  		assert.Equal(expect[0], commit.Subject)
   146  		assert.Equal(expect[1], commit.Body)
   147  		assert.Equal(expect[2], commit.Tag.Name)
   148  	}
   149  }
   150  
   151  func TestGitLogNumber(t *testing.T) {
   152  	assert := assert.New(t)
   153  
   154  	clear := setup()
   155  	defer clear()
   156  
   157  	git := New(&Config{
   158  		Path: ".tmp",
   159  	})
   160  
   161  	commits, err := git.Log(&RevNumber{2}, nil)
   162  
   163  	assert.Nil(err)
   164  	assert.Equal(2, len(commits))
   165  
   166  	table := [][]string{
   167  		[]string{"chore(release): Bump version to v0.0.0"},
   168  		[]string{"style(*): Run GoFmt"},
   169  	}
   170  
   171  	for i, commit := range commits {
   172  		assert.Equal(table[i][0], commit.Subject)
   173  	}
   174  }
   175  
   176  func TestGitLogMergesOnly(t *testing.T) {
   177  	assert := assert.New(t)
   178  
   179  	clear := setup()
   180  	defer clear()
   181  
   182  	git := New(&Config{
   183  		Path: ".tmp",
   184  	})
   185  
   186  	commits, err := git.Log(nil, &Params{
   187  		MergesOnly: true,
   188  	})
   189  
   190  	assert.Nil(err)
   191  	assert.Equal(1, len(commits))
   192  }
   193  
   194  func TestGitLogIgnoreMerges(t *testing.T) {
   195  	assert := assert.New(t)
   196  
   197  	clear := setup()
   198  	defer clear()
   199  
   200  	git := New(&Config{
   201  		Path: ".tmp",
   202  	})
   203  
   204  	commits, err := git.Log(nil, &Params{
   205  		IgnoreMerges: true,
   206  	})
   207  
   208  	assert.Nil(err)
   209  	assert.Equal(6, len(commits))
   210  }
   211  
   212  func TestGitLogReverse(t *testing.T) {
   213  	assert := assert.New(t)
   214  
   215  	clear := setup()
   216  	defer clear()
   217  
   218  	git := New(&Config{
   219  		Path: ".tmp",
   220  	})
   221  
   222  	commits, err := git.Log(nil, &Params{
   223  		Reverse: true,
   224  	})
   225  
   226  	assert.Nil(err)
   227  	assert.Equal(7, len(commits))
   228  
   229  	table := [][]string{
   230  		[]string{"chore(*): Initial Commit"},
   231  		[]string{"docs(readme): Has body commit message"},
   232  	}
   233  
   234  	for i, commit := range commits {
   235  		if len(table) >= i+1 {
   236  			assert.Equal(table[i][0], commit.Subject)
   237  		}
   238  	}
   239  }
   240  
   241  // FIXME: Time tests
   242  
   243  func TestGitLogNotFoundGitCommand(t *testing.T) {
   244  	assert := assert.New(t)
   245  
   246  	clear := setup()
   247  	defer clear()
   248  
   249  	git := New(&Config{
   250  		Bin:  "/notfound/git/bin",
   251  		Path: ".tmp",
   252  	})
   253  
   254  	commits, err := git.Log(nil, nil)
   255  
   256  	assert.Nil(commits)
   257  	assert.Contains(err.Error(), "does not exists")
   258  }
   259  
   260  func TestGitLogNotFoundPath(t *testing.T) {
   261  	assert := assert.New(t)
   262  
   263  	clear := setup()
   264  	defer clear()
   265  
   266  	git := New(&Config{
   267  		Bin:  "git",
   268  		Path: "/notfound/repo",
   269  	})
   270  
   271  	commits, err := git.Log(nil, nil)
   272  
   273  	assert.Nil(commits)
   274  	assert.Contains(err.Error(), "no such file or directory")
   275  }