github.com/purpleclay/gitz@v0.8.2-0.20240515052600-43f80eea2fe1/show_test.go (about)

     1  package git_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	git "github.com/purpleclay/gitz"
     8  	"github.com/purpleclay/gitz/gittest"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestShowBlobs(t *testing.T) {
    14  	gittest.InitRepository(t)
    15  	gittest.TempFile(t, "README.md", "The quick brown fox jumps over the lazy dog")
    16  	gittest.StageFile(t, "README.md")
    17  	gittest.TempFile(t, "LICENSE", "Follow the yellow brick road")
    18  	gittest.StageFile(t, "LICENSE")
    19  	gittest.Commit(t, "docs: include readme")
    20  
    21  	// Lookup refs
    22  	readmeRef := gittest.ObjectRef(t, "README.md")
    23  	licenseRef := gittest.ObjectRef(t, "LICENSE")
    24  
    25  	client, _ := git.NewClient()
    26  	blobs, err := client.ShowBlobs(readmeRef, licenseRef)
    27  	require.NoError(t, err)
    28  
    29  	require.Len(t, blobs, 2)
    30  	assert.Equal(t, readmeRef, blobs[readmeRef].Ref)
    31  	assert.Equal(t, "The quick brown fox jumps over the lazy dog", blobs[readmeRef].Diff)
    32  
    33  	assert.Equal(t, licenseRef, blobs[licenseRef].Ref)
    34  	assert.Equal(t, "Follow the yellow brick road", blobs[licenseRef].Diff)
    35  }
    36  
    37  func TestShowTrees(t *testing.T) {
    38  	gittest.InitRepository(t, gittest.WithStagedFiles(
    39  		"README.md",
    40  		"pkg/scan/scanner.go",
    41  		"pkg/scan/scanner_test.go",
    42  		"internal/task/scan.go",
    43  		"internal/task.go",
    44  		"internal/tui/dashboard.go"))
    45  	gittest.Commit(t, "chore: commit everything")
    46  
    47  	// Lookup refs
    48  	pkgRef := gittest.ObjectRef(t, "pkg")
    49  	internalRef := gittest.ObjectRef(t, "internal")
    50  
    51  	client, _ := git.NewClient()
    52  	trees, err := client.ShowTrees(pkgRef, internalRef)
    53  	require.NoError(t, err)
    54  
    55  	require.Len(t, trees, 2)
    56  	assert.Equal(t, pkgRef, trees[pkgRef].Ref)
    57  	assert.ElementsMatch(t, []string{"scan/"}, trees[pkgRef].Entries)
    58  
    59  	assert.Equal(t, internalRef, trees[internalRef].Ref)
    60  	assert.ElementsMatch(t, []string{"task.go", "task/", "tui/"}, trees[internalRef].Entries)
    61  }
    62  
    63  func TestShowCommits(t *testing.T) {
    64  	gittest.InitRepository(t)
    65  	gittest.CommitEmptyWithAuthor(t, "joker", "joker@dc.com", "docs: document new parsing features")
    66  	gittest.CommitEmpty(t, `feat: add functionality to parse a commit
    67  
    68  ensure a commit can be parsed when using the git show command`)
    69  
    70  	entries := gittest.Log(t)
    71  
    72  	client, _ := git.NewClient()
    73  	commits, err := client.ShowCommits(entries[0].Hash, entries[1].Hash)
    74  	require.NoError(t, err)
    75  
    76  	require.Len(t, commits, 2)
    77  	ref := entries[0].Hash
    78  	assert.Equal(t, ref, commits[ref].Ref)
    79  	assert.Equal(t, "batman", commits[ref].Author.Name)
    80  	assert.Equal(t, "batman@dc.com", commits[ref].Author.Email)
    81  	assert.WithinDuration(t, time.Now(), commits[ref].AuthorDate, time.Second*2)
    82  	assert.Equal(t, "batman", commits[ref].Committer.Name)
    83  	assert.Equal(t, "batman@dc.com", commits[ref].Committer.Email)
    84  	assert.WithinDuration(t, time.Now(), commits[ref].CommitterDate, time.Second*2)
    85  	assert.Equal(t, `feat: add functionality to parse a commit
    86  
    87  ensure a commit can be parsed when using the git show command`, commits[ref].Message)
    88  
    89  	ref = entries[1].Hash
    90  	assert.Equal(t, ref, commits[ref].Ref)
    91  	assert.Equal(t, "joker", commits[ref].Author.Name)
    92  	assert.Equal(t, "joker@dc.com", commits[ref].Author.Email)
    93  	assert.WithinDuration(t, time.Now(), commits[ref].AuthorDate, time.Second*2)
    94  	assert.Equal(t, "batman", commits[ref].Committer.Name)
    95  	assert.Equal(t, "batman@dc.com", commits[ref].Committer.Email)
    96  	assert.WithinDuration(t, time.Now(), commits[ref].CommitterDate, time.Second*2)
    97  	assert.Equal(t, "docs: document new parsing features", commits[ref].Message)
    98  }
    99  
   100  func TestShowTags(t *testing.T) {
   101  	gittest.InitRepository(t)
   102  	gittest.Tag(t, "0.1.0")
   103  	gittest.TagAnnotated(t, "0.2.0", "chore: tagged release at 0.2.0")
   104  
   105  	client, _ := git.NewClient()
   106  	tags, err := client.ShowTags("0.1.0", "0.2.0")
   107  	require.NoError(t, err)
   108  
   109  	require.Len(t, tags, 2)
   110  	tag := tags["0.1.0"]
   111  	assert.Equal(t, "0.1.0", tag.Ref)
   112  	assert.Nil(t, tag.Annotation)
   113  	assert.Equal(t, gittest.InitialCommit, tag.Commit.Message)
   114  
   115  	tag = tags["0.2.0"]
   116  	assert.Equal(t, "0.2.0", tag.Ref)
   117  	require.NotNil(t, tag.Annotation)
   118  	assert.Equal(t, "batman", tag.Annotation.Tagger.Name)
   119  	assert.Equal(t, "batman@dc.com", tag.Annotation.Tagger.Email)
   120  	assert.WithinDuration(t, time.Now(), tag.Annotation.TaggerDate, time.Second*2)
   121  	assert.Equal(t, "chore: tagged release at 0.2.0", tag.Annotation.Message)
   122  	assert.Equal(t, gittest.InitialCommit, tag.Commit.Message)
   123  }