github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/git/odb/commit_test.go (about)

     1  package odb
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"io"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestCommitReturnsCorrectObjectType(t *testing.T) {
    16  	assert.Equal(t, CommitObjectType, new(Commit).Type())
    17  }
    18  
    19  func TestCommitEncoding(t *testing.T) {
    20  	author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
    21  	committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
    22  
    23  	c := &Commit{
    24  		Author:    author.String(),
    25  		Committer: committer.String(),
    26  		ParentIDs: [][]byte{
    27  			[]byte("aaaaaaaaaaaaaaaaaaaa"), []byte("bbbbbbbbbbbbbbbbbbbb"),
    28  		},
    29  		TreeID: []byte("cccccccccccccccccccc"),
    30  		ExtraHeaders: []*ExtraHeader{
    31  			{"foo", "bar"},
    32  		},
    33  		Message: "initial commit",
    34  	}
    35  
    36  	buf := new(bytes.Buffer)
    37  
    38  	_, err := c.Encode(buf)
    39  	assert.Nil(t, err)
    40  
    41  	assertLine(t, buf, "tree 6363636363636363636363636363636363636363")
    42  	assertLine(t, buf, "parent 6161616161616161616161616161616161616161")
    43  	assertLine(t, buf, "parent 6262626262626262626262626262626262626262")
    44  	assertLine(t, buf, "author %s", author.String())
    45  	assertLine(t, buf, "committer %s", committer.String())
    46  	assertLine(t, buf, "foo bar")
    47  	assertLine(t, buf, "")
    48  	assertLine(t, buf, "initial commit")
    49  
    50  	assert.Equal(t, 0, buf.Len())
    51  }
    52  
    53  func TestCommitDecoding(t *testing.T) {
    54  	author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
    55  	committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
    56  
    57  	p1 := []byte("aaaaaaaaaaaaaaaaaaaa")
    58  	p2 := []byte("bbbbbbbbbbbbbbbbbbbb")
    59  	treeId := []byte("cccccccccccccccccccc")
    60  
    61  	from := new(bytes.Buffer)
    62  	fmt.Fprintf(from, "author %s\n", author)
    63  	fmt.Fprintf(from, "committer %s\n", committer)
    64  	fmt.Fprintf(from, "parent %s\n", hex.EncodeToString(p1))
    65  	fmt.Fprintf(from, "parent %s\n", hex.EncodeToString(p2))
    66  	fmt.Fprintf(from, "foo bar\n")
    67  	fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
    68  	fmt.Fprintf(from, "\ninitial commit\n")
    69  
    70  	flen := from.Len()
    71  
    72  	commit := new(Commit)
    73  	n, err := commit.Decode(from, int64(flen))
    74  
    75  	assert.Nil(t, err)
    76  	assert.Equal(t, flen, n)
    77  
    78  	assert.Equal(t, author.String(), commit.Author)
    79  	assert.Equal(t, committer.String(), commit.Committer)
    80  	assert.Equal(t, [][]byte{p1, p2}, commit.ParentIDs)
    81  	assert.Equal(t, 1, len(commit.ExtraHeaders))
    82  	assert.Equal(t, "foo", commit.ExtraHeaders[0].K)
    83  	assert.Equal(t, "bar", commit.ExtraHeaders[0].V)
    84  	assert.Equal(t, "initial commit", commit.Message)
    85  }
    86  
    87  func TestCommitDecodingWithEmptyName(t *testing.T) {
    88  	author := &Signature{Name: "", Email: "john@example.com", When: time.Now()}
    89  	committer := &Signature{Name: "", Email: "jane@example.com", When: time.Now()}
    90  
    91  	treeId := []byte("cccccccccccccccccccc")
    92  
    93  	from := new(bytes.Buffer)
    94  
    95  	fmt.Fprintf(from, "author %s\n", author)
    96  	fmt.Fprintf(from, "committer %s\n", committer)
    97  	fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
    98  	fmt.Fprintf(from, "\ninitial commit\n")
    99  
   100  	flen := from.Len()
   101  
   102  	commit := new(Commit)
   103  	n, err := commit.Decode(from, int64(flen))
   104  
   105  	assert.Nil(t, err)
   106  	assert.Equal(t, flen, n)
   107  
   108  	assert.Equal(t, author.String(), commit.Author)
   109  	assert.Equal(t, committer.String(), commit.Committer)
   110  	assert.Equal(t, "initial commit", commit.Message)
   111  }
   112  
   113  func TestCommitDecodingWithMessageKeywordPrefix(t *testing.T) {
   114  	author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
   115  	committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
   116  
   117  	treeId := []byte("aaaaaaaaaaaaaaaaaaaa")
   118  	treeIdAscii := hex.EncodeToString(treeId)
   119  
   120  	from := new(bytes.Buffer)
   121  	fmt.Fprintf(from, "author %s\n", author)
   122  	fmt.Fprintf(from, "committer %s\n", committer)
   123  	fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
   124  	fmt.Fprintf(from, "\nfirst line\n\nsecond line\n")
   125  
   126  	flen := from.Len()
   127  
   128  	commit := new(Commit)
   129  	n, err := commit.Decode(from, int64(flen))
   130  
   131  	assert.NoError(t, err)
   132  	assert.Equal(t, flen, n)
   133  
   134  	assert.Equal(t, author.String(), commit.Author)
   135  	assert.Equal(t, committer.String(), commit.Committer)
   136  	assert.Equal(t, treeIdAscii, hex.EncodeToString(commit.TreeID))
   137  	assert.Equal(t, "first line\n\nsecond line", commit.Message)
   138  }
   139  
   140  func TestCommitDecodingWithWhitespace(t *testing.T) {
   141  	author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
   142  	committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
   143  
   144  	treeId := []byte("aaaaaaaaaaaaaaaaaaaa")
   145  	treeIdAscii := hex.EncodeToString(treeId)
   146  
   147  	from := new(bytes.Buffer)
   148  	fmt.Fprintf(from, "author %s\n", author)
   149  	fmt.Fprintf(from, "committer %s\n", committer)
   150  	fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
   151  	fmt.Fprintf(from, "\ntree <- initial commit\n")
   152  
   153  	flen := from.Len()
   154  
   155  	commit := new(Commit)
   156  	n, err := commit.Decode(from, int64(flen))
   157  
   158  	assert.NoError(t, err)
   159  	assert.Equal(t, flen, n)
   160  
   161  	assert.Equal(t, author.String(), commit.Author)
   162  	assert.Equal(t, committer.String(), commit.Committer)
   163  	assert.Equal(t, treeIdAscii, hex.EncodeToString(commit.TreeID))
   164  	assert.Equal(t, "tree <- initial commit", commit.Message)
   165  }
   166  
   167  func assertLine(t *testing.T, buf *bytes.Buffer, wanted string, args ...interface{}) {
   168  	got, err := buf.ReadString('\n')
   169  	if err == io.EOF {
   170  		err = nil
   171  	}
   172  
   173  	assert.Nil(t, err)
   174  	assert.Equal(t, fmt.Sprintf(wanted, args...), strings.TrimSuffix(got, "\n"))
   175  }
   176  
   177  func TestCommitEqualReturnsTrueWithIdenticalCommits(t *testing.T) {
   178  	c1 := &Commit{
   179  		Author:    "Jane Doe <jane@example.com> 1503956287 -0400",
   180  		Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
   181  		ParentIDs: [][]byte{make([]byte, 20)},
   182  		TreeID:    make([]byte, 20),
   183  		ExtraHeaders: []*ExtraHeader{
   184  			{K: "Signed-off-by", V: "Joe Smith"},
   185  		},
   186  		Message: "initial commit",
   187  	}
   188  	c2 := &Commit{
   189  		Author:    "Jane Doe <jane@example.com> 1503956287 -0400",
   190  		Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
   191  		ParentIDs: [][]byte{make([]byte, 20)},
   192  		TreeID:    make([]byte, 20),
   193  		ExtraHeaders: []*ExtraHeader{
   194  			{K: "Signed-off-by", V: "Joe Smith"},
   195  		},
   196  		Message: "initial commit",
   197  	}
   198  
   199  	assert.True(t, c1.Equal(c2))
   200  }
   201  
   202  func TestCommitEqualReturnsFalseWithDifferentParentCounts(t *testing.T) {
   203  	c1 := &Commit{
   204  		ParentIDs: [][]byte{make([]byte, 20), make([]byte, 20)},
   205  	}
   206  	c2 := &Commit{
   207  		ParentIDs: [][]byte{make([]byte, 20)},
   208  	}
   209  
   210  	assert.False(t, c1.Equal(c2))
   211  }
   212  
   213  func TestCommitEqualReturnsFalseWithDifferentParentsIds(t *testing.T) {
   214  	c1 := &Commit{
   215  		ParentIDs: [][]byte{make([]byte, 20)},
   216  	}
   217  	c2 := &Commit{
   218  		ParentIDs: [][]byte{make([]byte, 20)},
   219  	}
   220  
   221  	c1.ParentIDs[0][1] = 0x1
   222  
   223  	assert.False(t, c1.Equal(c2))
   224  }
   225  
   226  func TestCommitEqualReturnsFalseWithDifferentHeaderCounts(t *testing.T) {
   227  	c1 := &Commit{
   228  		ExtraHeaders: []*ExtraHeader{
   229  			{K: "Signed-off-by", V: "Joe Smith"},
   230  			{K: "GPG-Signature", V: "..."},
   231  		},
   232  	}
   233  	c2 := &Commit{
   234  		ExtraHeaders: []*ExtraHeader{
   235  			{K: "Signed-off-by", V: "Joe Smith"},
   236  		},
   237  	}
   238  
   239  	assert.False(t, c1.Equal(c2))
   240  }
   241  
   242  func TestCommitEqualReturnsFalseWithDifferentHeaders(t *testing.T) {
   243  	c1 := &Commit{
   244  		ExtraHeaders: []*ExtraHeader{
   245  			{K: "Signed-off-by", V: "Joe Smith"},
   246  		},
   247  	}
   248  	c2 := &Commit{
   249  		ExtraHeaders: []*ExtraHeader{
   250  			{K: "Signed-off-by", V: "Jane Smith"},
   251  		},
   252  	}
   253  
   254  	assert.False(t, c1.Equal(c2))
   255  }
   256  
   257  func TestCommitEqualReturnsFalseWithDifferentAuthors(t *testing.T) {
   258  	c1 := &Commit{
   259  		Author: "Jane Doe <jane@example.com> 1503956287 -0400",
   260  	}
   261  	c2 := &Commit{
   262  		Author: "John Doe <john@example.com> 1503956287 -0400",
   263  	}
   264  
   265  	assert.False(t, c1.Equal(c2))
   266  }
   267  
   268  func TestCommitEqualReturnsFalseWithDifferentCommitters(t *testing.T) {
   269  	c1 := &Commit{
   270  		Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
   271  	}
   272  	c2 := &Commit{
   273  		Committer: "John Doe <john@example.com> 1503956287 -0400",
   274  	}
   275  
   276  	assert.False(t, c1.Equal(c2))
   277  }
   278  
   279  func TestCommitEqualReturnsFalseWithDifferentMessages(t *testing.T) {
   280  	c1 := &Commit{
   281  		Message: "initial commit",
   282  	}
   283  	c2 := &Commit{
   284  		Message: "not the initial commit",
   285  	}
   286  
   287  	assert.False(t, c1.Equal(c2))
   288  }
   289  
   290  func TestCommitEqualReturnsFalseWithDifferentTreeIDs(t *testing.T) {
   291  	c1 := &Commit{
   292  		TreeID: make([]byte, 20),
   293  	}
   294  	c2 := &Commit{
   295  		TreeID: make([]byte, 20),
   296  	}
   297  
   298  	c1.TreeID[0] = 0x1
   299  
   300  	assert.False(t, c1.Equal(c2))
   301  }
   302  
   303  func TestCommitEqualReturnsFalseWhenOneCommitIsNil(t *testing.T) {
   304  	c1 := &Commit{
   305  		Author:    "Jane Doe <jane@example.com> 1503956287 -0400",
   306  		Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
   307  		ParentIDs: [][]byte{make([]byte, 20)},
   308  		TreeID:    make([]byte, 20),
   309  		ExtraHeaders: []*ExtraHeader{
   310  			{K: "Signed-off-by", V: "Joe Smith"},
   311  		},
   312  		Message: "initial commit",
   313  	}
   314  	c2 := (*Commit)(nil)
   315  
   316  	assert.False(t, c1.Equal(c2))
   317  }
   318  
   319  func TestCommitEqualReturnsTrueWhenBothCommitsAreNil(t *testing.T) {
   320  	c1 := (*Commit)(nil)
   321  	c2 := (*Commit)(nil)
   322  
   323  	assert.True(t, c1.Equal(c2))
   324  }