github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/git/committree_test.go (about)

     1  package git
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // Tests that the date parsing used by GIT_COMMITTER_DATE and
    10  // GIT_AUTHOR_DATE works as expected for the formats defined
    11  // in git-commit-tree(1)
    12  func TestParseDate(t *testing.T) {
    13  	tests := []struct {
    14  		EnvString     string
    15  		GitFormat     string
    16  		ExpectedError bool
    17  	}{
    18  		{
    19  			// RFC 2822
    20  			"Fri, 29 Dec 2017 19:19:25 -0500",
    21  			"1514593165 -0500",
    22  			false,
    23  		},
    24  		{
    25  			// ISO 8601 gets converted to GMT since
    26  			// there's no timezone..
    27  			"2017-12-29T19:19:25",
    28  			"1514575165 +0000",
    29  			false,
    30  		},
    31  		{
    32  			// git internal format
    33  			"1514593165 -0500",
    34  			"1514593165 -0500",
    35  			false,
    36  		},
    37  	}
    38  
    39  	for i, tc := range tests {
    40  		ptime, err := parseDate(tc.EnvString)
    41  		if tc.ExpectedError {
    42  			if err == nil {
    43  				t.Errorf("Case %d: expected error, got none.", i)
    44  				continue
    45  			}
    46  		} else {
    47  			if err != nil {
    48  				t.Errorf("Case %d: %v", i, err)
    49  				continue
    50  			}
    51  		}
    52  
    53  		if gitstr := timeToGitTime(ptime); gitstr != tc.GitFormat {
    54  			t.Errorf("Case %d: got %v want %v", i, gitstr, tc.GitFormat)
    55  		}
    56  	}
    57  }
    58  
    59  // Test that commit-tree creates the same commit ids as the official
    60  // git client, for some known commits.
    61  func TestCommitTree(t *testing.T) {
    62  	// Test cases always use the empty tree for an easy, simple
    63  	// value since write-tree has its own separate tests
    64  	tests := []struct {
    65  		AuthorName, AuthorEmail, AuthorDate string
    66  		CommitName, CommitEmail, CommitDate string
    67  		Parents                             []string
    68  		Message                             string
    69  		ExpectedCommitID                    string
    70  	}{
    71  		// An initial commit with no parents or message
    72  		{
    73  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Author
    74  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Committer
    75  			nil,
    76  			"",
    77  			"82066825bb42aa51b8efda3ce0b3a5ded118467d",
    78  		},
    79  		// An initial commit with a message
    80  		{
    81  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Author
    82  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Committer
    83  			nil,
    84  			"I am a test\n",
    85  			"6f22a18883a3a1150faee61e506e16557535067d",
    86  		},
    87  		// A normal commit with the second test case as a parent.
    88  		{
    89  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Author
    90  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Committer
    91  			[]string{"6f22a18883a3a1150faee61e506e16557535067d"},
    92  			"I am a second commit\n",
    93  			"8b140ee15d13a88024414509b2a3ca1b2588fc9b",
    94  		},
    95  		// A merge commit with the first and second commit as parents. This
    96  		// is a weird, but valid tree, and we've already calculated the commits
    97  		// for the parents, we just reuse them for the test..
    98  		{
    99  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Author
   100  			"John Smith", "test@example.com", "Fri, 29 Dec 2017 20:43:22 -0500", // Committer
   101  			[]string{
   102  				"6f22a18883a3a1150faee61e506e16557535067d",
   103  				"8b140ee15d13a88024414509b2a3ca1b2588fc9b",
   104  			},
   105  			"I am a merge commit",
   106  			"551997ac02954d312c6112a1076725f7c43343dc",
   107  		},
   108  		// FIXME: Add merge commit tests
   109  	}
   110  
   111  	gitdir, err := ioutil.TempDir("", "gitcommittree")
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	defer os.RemoveAll(gitdir)
   116  
   117  	c, err := NewClient(gitdir, "")
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	treeid, err := writeTree(c, "", nil)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	for i, tc := range tests {
   127  		// Set all appropriate environment variables so that they're
   128  		// known values for testing, and not the user config or
   129  		// time.Now()
   130  		if err := os.Setenv("GIT_AUTHOR_NAME", tc.AuthorName); err != nil {
   131  			t.Fatal(err)
   132  		}
   133  		if err := os.Setenv("GIT_AUTHOR_EMAIL", tc.AuthorEmail); err != nil {
   134  			t.Fatal(err)
   135  		}
   136  		if err := os.Setenv("GIT_AUTHOR_DATE", tc.AuthorDate); err != nil {
   137  			t.Fatal(err)
   138  		}
   139  		if err := os.Setenv("GIT_COMMITTER_NAME", tc.CommitName); err != nil {
   140  			t.Fatal(err)
   141  		}
   142  		if err := os.Setenv("GIT_COMMITTER_EMAIL", tc.CommitEmail); err != nil {
   143  			t.Fatal(err)
   144  		}
   145  		if err := os.Setenv("GIT_COMMITTER_DATE", tc.CommitDate); err != nil {
   146  			t.Fatal(err)
   147  		}
   148  
   149  		var parents []CommitID
   150  		for _, p := range tc.Parents {
   151  			psha, err := CommitIDFromString(p)
   152  			if err != nil {
   153  				t.Fatal(err)
   154  			}
   155  			parents = append(parents, psha)
   156  		}
   157  		cid, err := CommitTree(c, CommitTreeOptions{}, TreeID(treeid), parents, tc.Message)
   158  		if err != nil {
   159  			t.Error(err)
   160  		}
   161  		exsha, err := CommitIDFromString(tc.ExpectedCommitID)
   162  		if err != nil {
   163  			t.Fatal(err)
   164  		}
   165  		if exsha != cid {
   166  			t.Errorf("Case %d: got %v want %v", i, cid, exsha)
   167  		}
   168  	}
   169  
   170  }