1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/git_commands/branch_loader_test.go

115 lines
3.1 KiB
Go
Raw Normal View History

package git_commands
2022-03-24 08:49:25 +02:00
// "*|feat/detect-purge|origin/feat/detect-purge|[ahead 1]"
import (
"strconv"
2022-03-24 08:49:25 +02:00
"testing"
"time"
2022-03-24 08:49:25 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/stretchr/testify/assert"
)
2022-11-07 07:35:36 +02:00
func TestObtainBranch(t *testing.T) {
2022-03-24 08:49:25 +02:00
type scenario struct {
testName string
input []string
storeCommitDateAsRecency bool
expectedBranch *models.Branch
2022-03-24 08:49:25 +02:00
}
// Use a time stamp of 2 1/2 hours ago, resulting in a recency string of "2h"
now := time.Now().Unix()
timeStamp := strconv.Itoa(int(now - 2.5*60*60))
2022-03-24 08:49:25 +02:00
scenarios := []scenario{
{
testName: "TrimHeads",
input: []string{"", "heads/a_branch", "", "", "subject", "123", timeStamp},
storeCommitDateAsRecency: false,
2022-11-07 07:35:36 +02:00
expectedBranch: &models.Branch{
Name: "a_branch",
Pushables: "?",
Pullables: "?",
Head: false,
Subject: "subject",
CommitHash: "123",
},
2022-03-24 08:49:25 +02:00
},
{
testName: "NoUpstream",
input: []string{"", "a_branch", "", "", "subject", "123", timeStamp},
storeCommitDateAsRecency: false,
2022-11-07 07:35:36 +02:00
expectedBranch: &models.Branch{
Name: "a_branch",
Pushables: "?",
Pullables: "?",
Head: false,
Subject: "subject",
CommitHash: "123",
},
2022-03-24 08:49:25 +02:00
},
{
testName: "IsHead",
input: []string{"*", "a_branch", "", "", "subject", "123", timeStamp},
storeCommitDateAsRecency: false,
2022-11-07 07:35:36 +02:00
expectedBranch: &models.Branch{
Name: "a_branch",
Pushables: "?",
Pullables: "?",
Head: true,
Subject: "subject",
CommitHash: "123",
},
2022-03-24 08:49:25 +02:00
},
{
testName: "IsBehindAndAhead",
input: []string{"", "a_branch", "a_remote/a_branch", "[behind 2, ahead 3]", "subject", "123", timeStamp},
storeCommitDateAsRecency: false,
2022-11-07 07:35:36 +02:00
expectedBranch: &models.Branch{
Name: "a_branch",
Pushables: "3",
Pullables: "2",
Head: false,
Subject: "subject",
CommitHash: "123",
},
2022-03-24 08:49:25 +02:00
},
{
testName: "RemoteBranchIsGone",
input: []string{"", "a_branch", "a_remote/a_branch", "[gone]", "subject", "123", timeStamp},
storeCommitDateAsRecency: false,
2022-11-07 07:35:36 +02:00
expectedBranch: &models.Branch{
Name: "a_branch",
UpstreamGone: true,
Pushables: "?",
Pullables: "?",
Head: false,
Subject: "subject",
CommitHash: "123",
},
2022-03-24 08:49:25 +02:00
},
{
testName: "WithCommitDateAsRecency",
input: []string{"", "a_branch", "", "", "subject", "123", timeStamp},
storeCommitDateAsRecency: true,
expectedBranch: &models.Branch{
Name: "a_branch",
Recency: "2h",
Pushables: "?",
Pullables: "?",
Head: false,
Subject: "subject",
CommitHash: "123",
},
},
2022-03-24 08:49:25 +02:00
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
branch := obtainBranch(s.input, s.storeCommitDateAsRecency)
2022-03-24 08:49:25 +02:00
assert.EqualValues(t, s.expectedBranch, branch)
})
}
}