2022-11-11 04:19:29 +02:00
|
|
|
package git_commands
|
2022-03-24 08:49:25 +02:00
|
|
|
|
|
|
|
// "*|feat/detect-purge|origin/feat/detect-purge|[ahead 1]"
|
|
|
|
import (
|
2023-12-21 10:57:58 +02:00
|
|
|
"strconv"
|
2022-03-24 08:49:25 +02:00
|
|
|
"testing"
|
2023-12-21 10:57:58 +02:00
|
|
|
"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 {
|
2023-12-21 10:57:58 +02:00
|
|
|
testName string
|
|
|
|
input []string
|
|
|
|
storeCommitDateAsRecency bool
|
|
|
|
expectedBranch *models.Branch
|
2022-03-24 08:49:25 +02:00
|
|
|
}
|
|
|
|
|
2023-12-21 10:57:58 +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{
|
|
|
|
{
|
2023-12-21 10:57:58 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
{
|
2023-12-21 10:57:58 +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
|
|
|
},
|
|
|
|
{
|
2023-12-21 10:57:58 +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
|
|
|
},
|
|
|
|
{
|
2023-12-21 10:57:58 +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
|
|
|
},
|
|
|
|
{
|
2023-12-21 10:57:58 +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
|
|
|
},
|
2023-12-21 10:57:58 +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) {
|
2023-12-21 10:57:58 +02:00
|
|
|
branch := obtainBranch(s.input, s.storeCommitDateAsRecency)
|
2022-03-24 08:49:25 +02:00
|
|
|
assert.EqualValues(t, s.expectedBranch, branch)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|