github.com/soypat/gitaligned@v0.3.4-0.20221228122414-e435aab44fbc/scan_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  type initiator func()
    12  
    13  func defaultOptions() []gitOption {
    14  	maxAuthors = defaultMaxAuthors
    15  	maxCommits = defaultMaxCommits
    16  	var opts = []gitOption{
    17  		optionMaxCommits(defaultMaxCommits),
    18  		optionBranch(""),
    19  	}
    20  	return opts
    21  
    22  }
    23  func TestScan(t *testing.T) {
    24  	opts := defaultOptions()
    25  	commits, authors, err := ScanCWD(opts...)
    26  	if len(commits) == 0 || err != nil {
    27  		t.Errorf("zero commits or error:%v", err)
    28  		t.FailNow()
    29  	}
    30  	if commits[len(commits)-1].Message != "initial commit" {
    31  		t.Error("Expected initial commit at the end")
    32  	}
    33  	_ = authors
    34  }
    35  
    36  func TestAlignments(t *testing.T) {
    37  	opts := defaultOptions()
    38  	commits, authors, _ := ScanCWD(opts...)
    39  	SetAuthorAlignments(commits, authors)
    40  	emptyalignment := alignment{}
    41  	for i := range authors {
    42  		if authors[i].alignment == emptyalignment {
    43  			t.Error("alignment not set for", authors[i].Name)
    44  		}
    45  	}
    46  }
    47  
    48  func TestFile(t *testing.T) {
    49  	var tests = []struct {
    50  		file       string
    51  		maxCommits int
    52  		maxAuthors int
    53  	}{
    54  		{maxCommits: 200, maxAuthors: 100, file: "testdata/awesomego.log"},
    55  		{maxCommits: 315, maxAuthors: 20, file: "testdata/deal.log"},
    56  		{maxCommits: 2, maxAuthors: 20, file: "testdata/deal.log"},
    57  		{maxCommits: 20, maxAuthors: 20, file: "testdata/deal.log"},
    58  		{maxCommits: 80, maxAuthors: 20, file: "testdata/deal.log"},
    59  	}
    60  	for i := range tests {
    61  		maxAuthors, maxCommits = tests[i].maxAuthors, tests[i].maxCommits
    62  		commits, authors := scanFromLogFile(t, tests[i].file)
    63  		if len(commits) == 0 || len(commits) < tests[i].maxCommits || len(authors) == 0 {
    64  			t.Error("bad length of results " + tests[i].file)
    65  		}
    66  		err := SetAuthorAlignments(commits, authors)
    67  		if err != nil {
    68  			t.Error(err)
    69  		}
    70  		emptyalignment := alignment{}
    71  		for i := range authors {
    72  			if authors[i].alignment == emptyalignment {
    73  				t.Errorf("%d:alignment not set for %v", tests[i].maxCommits, authors[i].Stats())
    74  			}
    75  		}
    76  	}
    77  }
    78  
    79  func TestTokenize(t *testing.T) {
    80  	var tests = []struct {
    81  		file                   string
    82  		maxAuthors, maxCommits int
    83  	}{
    84  		{maxCommits: 200, maxAuthors: 100, file: "testdata/awesomego.log"},
    85  		{maxCommits: 315, maxAuthors: 20, file: "testdata/deal.log"},
    86  		{maxCommits: 2, maxAuthors: 20, file: "testdata/deal.log"},
    87  		{maxCommits: 20, maxAuthors: 20, file: "testdata/deal.log"},
    88  		{maxCommits: 80, maxAuthors: 20, file: "testdata/deal.log"},
    89  	}
    90  	for i := range tests {
    91  		maxAuthors, maxCommits = tests[i].maxAuthors, tests[i].maxCommits
    92  		commits, _ := scanFromLogFile(t, tests[i].file)
    93  		tokens, err := tokenizeCommits(commits)
    94  		if err != nil {
    95  			t.Error(err)
    96  		}
    97  		atCommit := 0
    98  		last := -1
    99  		for i := range tokens {
   100  			if tokens[i].Tag == "." {
   101  				// f(&commits[atCommit], tokens[last+1:i])
   102  				msg := replacecommits.Replace(commits[atCommit].Message)
   103  				splitsies := strings.Fields(msg)
   104  				for j := range splitsies {
   105  					if splitsies[j] != "" && splitsies[j] != tokens[last+1+j].Text {
   106  						t.Errorf("expected %q=%q\n in %v=%v\n\n", splitsies[j], tokens[last+1+j].Text, splitsies, tokens[last+1:i])
   107  					}
   108  				}
   109  				last = i
   110  				atCommit++
   111  			}
   112  		}
   113  	}
   114  }
   115  
   116  func TestDisplay(t *testing.T) {
   117  	var tests = []struct {
   118  		file       string
   119  		maxCommits int
   120  		maxAuthors int
   121  	}{
   122  		{maxCommits: 200, maxAuthors: 100, file: "testdata/awesomego.log"},
   123  		{maxCommits: 315, maxAuthors: 20, file: "testdata/deal.log"},
   124  		{maxCommits: 2, maxAuthors: 20, file: "testdata/deal.log"},
   125  		{maxCommits: 20, maxAuthors: 20, file: "testdata/deal.log"},
   126  		{maxCommits: 80, maxAuthors: 20, file: "testdata/deal.log"},
   127  	}
   128  	for i := range tests {
   129  		maxAuthors = tests[i].maxAuthors
   130  		commits, authors := scanFromLogFile(t, tests[i].file)
   131  		err := WriteAuthorAlignments(io.Discard, authors)
   132  		if err != nil {
   133  			t.Error(err)
   134  		}
   135  		err = SetCommitAlignments(commits, authors)
   136  		if err != nil {
   137  			t.Error(err)
   138  		}
   139  		err = WriteCommitAlignments(io.Discard, commits)
   140  		if err != nil {
   141  			t.Error(err)
   142  		}
   143  		err = WriteNLPTags(io.Discard, commits)
   144  		if err != nil {
   145  			t.Error(err)
   146  		}
   147  	}
   148  }
   149  
   150  func scanFromLogFile(t *testing.T, filename string) ([]commit, []author) {
   151  	f, err := os.Open(filename)
   152  	if err != nil {
   153  		t.FailNow()
   154  	}
   155  	t.Cleanup(func() { f.Close() })
   156  
   157  	commits, authors, err := GitLogScan(f)
   158  	if err != nil && err != io.EOF {
   159  		t.Error(err)
   160  		t.FailNow()
   161  	}
   162  	return commits, authors
   163  }
   164  
   165  func TestNoFolderError(t *testing.T) {
   166  	opts := defaultOptions()
   167  	dir, err := filepath.Abs(".")
   168  	if err != nil {
   169  		t.FailNow()
   170  	}
   171  	base := filepath.Base(dir)
   172  	err = os.Chdir("../")
   173  	t.Cleanup(func() {
   174  		os.Chdir(base)
   175  	})
   176  	if err != nil {
   177  		t.Error(err)
   178  		t.FailNow()
   179  	}
   180  	commits, _, err := ScanCWD(opts...)
   181  	if err == nil || len(commits) > 0 {
   182  		t.Error("expected error and no commits detected being in non git dir")
   183  	}
   184  }
   185  
   186  func TestFindAuthor(t *testing.T) {
   187  	firstCommiter := "Patricio Whittingslow"
   188  	opts := []gitOption{
   189  		optionAuthorPattern(firstCommiter),
   190  		optionMaxCommits(4),
   191  	}
   192  	maxAuthors = 1
   193  
   194  	commits, authors, err := ScanCWD(opts...)
   195  	if err != nil {
   196  		t.Error(err)
   197  		t.FailNow()
   198  	}
   199  	if len(authors) != 1 || len(commits) == 0 {
   200  		t.Error("got more than 1 author or no commits")
   201  	}
   202  	if authors[0].Name != firstCommiter {
   203  		t.Error("could not find", firstCommiter, "author")
   204  	}
   205  }
   206  
   207  func TestRun(t *testing.T) {
   208  	err := run()
   209  	if err != nil {
   210  		t.Error(err)
   211  	}
   212  }