aboutsummaryrefslogblamecommitdiffstats
path: root/bug/bug_actions_test.go
blob: 38dddce218a478095d5cd4f82f7ec0c6bbe3d3ee (plain) (tree)
1
2
3
4
5
6
7
8
           

        
                 

              
                                            
                                             


                                                   

 
                                 

                                                                  
 
                                                                             
 

                                                                           
                                         
                                
                               
                                          
 





                                               
                             
                                      
                               
 
                                   
                               
 
                                                   





                                                    




                                                                           
                                
                               
 
                                      
                               
 
                                   
                               
 
                                                  





                                                    

                                                            









                                                     









                                          

                                                                  
 



                                                                             
                                         
                                
                               
                                          
 





                                               
                       
 
                                      
                               

                       
                                   
                               
 
                                                   
                               
                                          





                                                                       
                                         



                                                                       
                                
                               
                                          

                       
                                      
                               

                       
                                   
                               
 
                                                   




                                                    
                                                   
                               






                                                          









                                        

                                                                  
 



                                                                             
                                
                               
 





                                               
                       
                                      
                               

                       
                                   







                                                                       
                                







                                                                       
                                







                                                                        
                                
                               

                       
                                   
                               
 
                                                   




                                                    
                                                   
                               





                                                          

                                     







                                       









                                            

                                                                  
 



                                                                             
                                
                               
 





                                               
                       
                                      
                               

                       
                                   







                                                                       
                                







                                                                       
                                







                                                                        
                                
                               
 
                                                   










                                                                        
                                







                                                                        
                                







                                                                        
                                
                               

                       
                                      
                               

                       
                                   
                               
 
                                                   




                                                    
                                                   
                               





                                                          
                                      
                               

                       
                                   
                               
 
                                                  




                                                    
                                                   
                               




                                                          
package bug

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/repository"
)

func TestPushPull(t *testing.T) {
	repoA, repoB, remote := repository.SetupReposAndRemote(t)
	defer repository.CleanupTestRepos(t, repoA, repoB, remote)

	reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")

	bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
	require.NoError(t, err)
	assert.True(t, bug1.NeedCommit())
	err = bug1.Commit(repoA)
	require.NoError(t, err)
	assert.False(t, bug1.NeedCommit())

	// distribute the identity
	_, err = identity.Push(repoA, "origin")
	require.NoError(t, err)
	err = identity.Pull(repoB, "origin")
	require.NoError(t, err)

	// A --> remote --> B
	_, err = Push(repoA, "origin")
	require.NoError(t, err)

	err = Pull(repoB, "origin")
	require.NoError(t, err)

	bugs := allBugs(t, ReadAllLocalBugs(repoB))

	if len(bugs) != 1 {
		t.Fatal("Unexpected number of bugs")
	}

	// B --> remote --> A
	reneB, err := identity.ReadLocal(repoA, reneA.Id())
	require.NoError(t, err)

	bug2, _, err := Create(reneB, time.Now().Unix(), "bug2", "message")
	require.NoError(t, err)
	err = bug2.Commit(repoB)
	require.NoError(t, err)

	_, err = Push(repoB, "origin")
	require.NoError(t, err)

	err = Pull(repoA, "origin")
	require.NoError(t, err)

	bugs = allBugs(t, ReadAllLocalBugs(repoA))

	if len(bugs) != 2 {
		t.Fatal("Unexpected number of bugs")
	}
}

func allBugs(t testing.TB, bugs <-chan StreamedBug) []*Bug {
	var result []*Bug
	for streamed := range bugs {
		if streamed.Err != nil {
			t.Fatal(streamed.Err)
		}
		result = append(result, streamed.Bug)
	}
	return result
}

func TestRebaseTheirs(t *testing.T) {
	_RebaseTheirs(t)
}

func BenchmarkRebaseTheirs(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_RebaseTheirs(b)
	}
}

func _RebaseTheirs(t testing.TB) {
	repoA, repoB, remote := repository.SetupReposAndRemote(t)
	defer repository.CleanupTestRepos(t, repoA, repoB, remote)

	reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")

	bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
	require.NoError(t, err)
	assert.True(t, bug1.NeedCommit())
	err = bug1.Commit(repoA)
	require.NoError(t, err)
	assert.False(t, bug1.NeedCommit())

	// distribute the identity
	_, err = identity.Push(repoA, "origin")
	require.NoError(t, err)
	err = identity.Pull(repoB, "origin")
	require.NoError(t, err)

	// A --> remote

	_, err = Push(repoA, "origin")
	require.NoError(t, err)

	// remote --> B
	err = Pull(repoB, "origin")
	require.NoError(t, err)

	bug2, err := ReadLocalBug(repoB, bug1.Id())
	require.NoError(t, err)
	assert.False(t, bug2.NeedCommit())

	reneB, err := identity.ReadLocal(repoA, reneA.Id())
	require.NoError(t, err)

	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message2")
	require.NoError(t, err)
	assert.True(t, bug2.NeedCommit())
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message3")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message4")
	require.NoError(t, err)
	err = bug2.Commit(repoB)
	require.NoError(t, err)
	assert.False(t, bug2.NeedCommit())

	// B --> remote
	_, err = Push(repoB, "origin")
	require.NoError(t, err)

	// remote --> A
	err = Pull(repoA, "origin")
	require.NoError(t, err)

	bugs := allBugs(t, ReadAllLocalBugs(repoB))

	if len(bugs) != 1 {
		t.Fatal("Unexpected number of bugs")
	}

	bug3, err := ReadLocalBug(repoA, bug1.Id())
	require.NoError(t, err)

	if nbOps(bug3) != 4 {
		t.Fatal("Unexpected number of operations")
	}
}

func TestRebaseOurs(t *testing.T) {
	_RebaseOurs(t)
}

func BenchmarkRebaseOurs(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_RebaseOurs(b)
	}
}

func _RebaseOurs(t testing.TB) {
	repoA, repoB, remote := repository.SetupReposAndRemote(t)
	defer repository.CleanupTestRepos(t, repoA, repoB, remote)

	reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")

	bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	// distribute the identity
	_, err = identity.Push(repoA, "origin")
	require.NoError(t, err)
	err = identity.Pull(repoB, "origin")
	require.NoError(t, err)

	// A --> remote
	_, err = Push(repoA, "origin")
	require.NoError(t, err)

	// remote --> B
	err = Pull(repoB, "origin")
	require.NoError(t, err)

	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message2")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message3")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message4")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message5")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message6")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message7")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message8")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message9")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message10")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	// remote --> A
	err = Pull(repoA, "origin")
	require.NoError(t, err)

	bugs := allBugs(t, ReadAllLocalBugs(repoA))

	if len(bugs) != 1 {
		t.Fatal("Unexpected number of bugs")
	}

	bug2, err := ReadLocalBug(repoA, bug1.Id())
	require.NoError(t, err)

	if nbOps(bug2) != 10 {
		t.Fatal("Unexpected number of operations")
	}
}

func nbOps(b *Bug) int {
	it := NewOperationIterator(b)
	counter := 0
	for it.Next() {
		counter++
	}
	return counter
}

func TestRebaseConflict(t *testing.T) {
	_RebaseConflict(t)
}

func BenchmarkRebaseConflict(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_RebaseConflict(b)
	}
}

func _RebaseConflict(t testing.TB) {
	repoA, repoB, remote := repository.SetupReposAndRemote(t)
	defer repository.CleanupTestRepos(t, repoA, repoB, remote)

	reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")

	bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	// distribute the identity
	_, err = identity.Push(repoA, "origin")
	require.NoError(t, err)
	err = identity.Pull(repoB, "origin")
	require.NoError(t, err)

	// A --> remote
	_, err = Push(repoA, "origin")
	require.NoError(t, err)

	// remote --> B
	err = Pull(repoB, "origin")
	require.NoError(t, err)

	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message2")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message3")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message4")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message5")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message6")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message7")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message8")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message9")
	require.NoError(t, err)
	_, err = AddComment(bug1, reneA, time.Now().Unix(), "message10")
	require.NoError(t, err)
	err = bug1.Commit(repoA)
	require.NoError(t, err)

	bug2, err := ReadLocalBug(repoB, bug1.Id())
	require.NoError(t, err)

	reneB, err := identity.ReadLocal(repoA, reneA.Id())
	require.NoError(t, err)

	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message11")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message12")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message13")
	require.NoError(t, err)
	err = bug2.Commit(repoB)
	require.NoError(t, err)

	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message14")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message15")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message16")
	require.NoError(t, err)
	err = bug2.Commit(repoB)
	require.NoError(t, err)

	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message17")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message18")
	require.NoError(t, err)
	_, err = AddComment(bug2, reneB, time.Now().Unix(), "message19")
	require.NoError(t, err)
	err = bug2.Commit(repoB)
	require.NoError(t, err)

	// A --> remote
	_, err = Push(repoA, "origin")
	require.NoError(t, err)

	// remote --> B
	err = Pull(repoB, "origin")
	require.NoError(t, err)

	bugs := allBugs(t, ReadAllLocalBugs(repoB))

	if len(bugs) != 1 {
		t.Fatal("Unexpected number of bugs")
	}

	bug3, err := ReadLocalBug(repoB, bug1.Id())
	require.NoError(t, err)

	if nbOps(bug3) != 19 {
		t.Fatal("Unexpected number of operations")
	}

	// B --> remote
	_, err = Push(repoB, "origin")
	require.NoError(t, err)

	// remote --> A
	err = Pull(repoA, "origin")
	require.NoError(t, err)

	bugs = allBugs(t, ReadAllLocalBugs(repoA))

	if len(bugs) != 1 {
		t.Fatal("Unexpected number of bugs")
	}

	bug4, err := ReadLocalBug(repoA, bug1.Id())
	require.NoError(t, err)

	if nbOps(bug4) != 19 {
		t.Fatal("Unexpected number of operations")
	}
}