2022-11-10 21:19:29 -05:00
package git_commands
2019-03-02 13:08:09 +11:00
import (
2021-12-30 11:22:29 +11:00
"path/filepath"
2022-03-26 21:55:44 +09:00
"strings"
2019-03-02 13:08:09 +11:00
"testing"
2023-02-26 11:51:02 +01:00
"github.com/fsmiamoto/git-todo-parser/todo"
2023-07-29 12:32:59 +10:00
"github.com/go-errors/errors"
2021-12-30 13:11:58 +11:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-12-30 13:35:10 +11:00
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
2020-09-29 19:10:57 +10:00
"github.com/jesseduffield/lazygit/pkg/utils"
2019-03-02 13:08:09 +11:00
"github.com/stretchr/testify/assert"
)
2023-06-21 19:30:25 +02:00
var commitsOutput = strings . Replace ( ` 0 eea75e8c631fba6b58135697835d58ba4c18dbc | 1640826609 | Jesse Duffield | jessedduffield @ gmail . com | HEAD - > better - tests | b21997d6b4cbdf84b149 | better typing for rebase mode
b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164 | 1640824515 | Jesse Duffield | jessedduffield @ gmail . com | origin / better - tests | e94e8fc5b6fab4cb755f | fix logging
e94e8fc5b6fab4cb755f29f1bdb3ee5e001df35c | 1640823749 | Jesse Duffield | jessedduffield @ gmail . com | tag : 123 , tag : 456 | d8084cd558925eb7c9c3 | refactor
2022-05-08 14:23:29 +10:00
d8084cd558925eb7c9c38afeed5725c21653ab90 | 1640821426 | Jesse Duffield | jessedduffield @ gmail . com || 65 f910ebd85283b5cce9 | WIP
65 f910ebd85283b5cce9bf67d03d3f1a9ea3813a | 1640821275 | Jesse Duffield | jessedduffield @ gmail . com || 26 c07b1ab33860a1a759 | WIP
26 c07b1ab33860a1a7591a0638f9925ccf497ffa | 1640750752 | Jesse Duffield | jessedduffield @ gmail . com || 3 d4470a6c072208722e5 | WIP
3 d4470a6c072208722e5ae9a54bcb9634959a1c5 | 1640748818 | Jesse Duffield | jessedduffield @ gmail . com || 053 a66a7be3da43aacdc | WIP
053 a66a7be3da43aacdc7aa78e1fe757b82c4dd2 | 1640739815 | Jesse Duffield | jessedduffield @ gmail . com || 985 fe482e806b172aea4 | refactoring the config struct ` , "|" , "\x00" , - 1 )
2021-12-30 13:11:58 +11:00
2023-06-21 19:30:25 +02:00
var singleCommitOutput = strings . Replace ( ` 0eea75e8c631fba6b58135697835d58ba4c18dbc|1640826609|Jesse Duffield|jessedduffield@gmail.com|HEAD -> better-tests|b21997d6b4cbdf84b149|better typing for rebase mode ` , "|" , "\x00" , - 1 )
2023-05-09 21:41:25 +02:00
2021-12-30 13:11:58 +11:00
func TestGetCommits ( t * testing . T ) {
2019-03-02 13:08:09 +11:00
type scenario struct {
2023-05-09 21:41:25 +02:00
testName string
runner * oscommands . FakeCmdObjRunner
expectedCommits [ ] * models . Commit
expectedError error
logOrder string
rebaseMode enums . RebaseMode
opts GetCommitsOptions
mainBranches [ ] string
2019-03-02 13:08:09 +11:00
}
scenarios := [ ] scenario {
{
2023-05-09 21:41:25 +02:00
testName : "should return no commits if there are none" ,
logOrder : "topo-order" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "HEAD" , RefForPushedStatus : "mybranch" , IncludeRebaseCommits : false } ,
2021-12-30 13:11:58 +11:00
runner : oscommands . NewFakeRunner ( t ) .
2023-08-19 09:17:12 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "HEAD" , "--topo-order" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--no-show-signature" , "--" } , "" , nil ) ,
2019-03-02 13:08:09 +11:00
2021-12-30 13:11:58 +11:00
expectedCommits : [ ] * models . Commit { } ,
expectedError : nil ,
2019-03-02 13:08:09 +11:00
} ,
2023-02-19 00:13:46 +01:00
{
2023-05-09 21:41:25 +02:00
testName : "should use proper upstream name for branch" ,
logOrder : "topo-order" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "refs/heads/mybranch" , RefForPushedStatus : "refs/heads/mybranch" , IncludeRebaseCommits : false } ,
2023-02-19 00:13:46 +01:00
runner : oscommands . NewFakeRunner ( t ) .
2023-05-21 17:00:29 +10:00
ExpectGitArgs ( [ ] string { "merge-base" , "refs/heads/mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "refs/heads/mybranch" , "--topo-order" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--no-show-signature" , "--" } , "" , nil ) ,
2023-02-19 00:13:46 +01:00
expectedCommits : [ ] * models . Commit { } ,
expectedError : nil ,
} ,
2019-03-02 13:08:09 +11:00
{
2023-05-09 21:41:25 +02:00
testName : "should return commits if they are present" ,
logOrder : "topo-order" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "HEAD" , RefForPushedStatus : "mybranch" , IncludeRebaseCommits : false } ,
2023-06-10 10:49:18 +02:00
mainBranches : [ ] string { "master" , "main" , "develop" } ,
2021-12-30 13:11:58 +11:00
runner : oscommands . NewFakeRunner ( t ) .
// here it's seeing which commits are yet to be pushed
2023-08-19 09:17:12 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2021-12-30 13:11:58 +11:00
// here it's actually getting all the commits in a formatted form, one per line
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "HEAD" , "--topo-order" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--no-show-signature" , "--" } , commitsOutput , nil ) .
2023-06-10 10:49:18 +02:00
// here it's testing which of the configured main branches have an upstream
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "master@{u}" } , "refs/remotes/origin/master" , nil ) . // this one does
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "main@{u}" } , "" , errors . New ( "error" ) ) . // this one doesn't, so it checks origin instead
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/remotes/origin/main" } , "" , nil ) . // yep, origin/main exists
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "develop@{u}" } , "" , errors . New ( "error" ) ) . // this one doesn't, so it checks origin instead
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/remotes/origin/develop" } , "" , errors . New ( "error" ) ) . // doesn't exist there, either, so it checks for a local branch
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/heads/develop" } , "" , errors . New ( "error" ) ) . // no local branch either
2021-12-30 13:11:58 +11:00
// here it's seeing where our branch diverged from the master branch so that we can mark that commit and parent commits as 'merged'
2023-06-10 10:49:18 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "HEAD" , "refs/remotes/origin/master" , "refs/remotes/origin/main" } , "26c07b1ab33860a1a7591a0638f9925ccf497ffa" , nil ) ,
2019-03-02 13:08:09 +11:00
2021-12-30 13:11:58 +11:00
expectedCommits : [ ] * models . Commit {
{
Sha : "0eea75e8c631fba6b58135697835d58ba4c18dbc" ,
Name : "better typing for rebase mode" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusUnpushed ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "(HEAD -> better-tests)" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640826609 ,
Parents : [ ] string {
"b21997d6b4cbdf84b149" ,
} ,
} ,
{
Sha : "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" ,
Name : "fix logging" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusPushed ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "(origin/better-tests)" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640824515 ,
Parents : [ ] string {
"e94e8fc5b6fab4cb755f" ,
} ,
} ,
{
Sha : "e94e8fc5b6fab4cb755f29f1bdb3ee5e001df35c" ,
Name : "refactor" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusPushed ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2023-06-21 19:30:25 +02:00
Tags : [ ] string { "123" , "456" } ,
2023-06-21 19:18:32 +02:00
ExtraInfo : "(tag: 123, tag: 456)" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640823749 ,
Parents : [ ] string {
"d8084cd558925eb7c9c3" ,
} ,
} ,
{
Sha : "d8084cd558925eb7c9c38afeed5725c21653ab90" ,
Name : "WIP" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusPushed ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640821426 ,
Parents : [ ] string {
"65f910ebd85283b5cce9" ,
} ,
} ,
{
Sha : "65f910ebd85283b5cce9bf67d03d3f1a9ea3813a" ,
Name : "WIP" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusPushed ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640821275 ,
Parents : [ ] string {
"26c07b1ab33860a1a759" ,
} ,
} ,
{
Sha : "26c07b1ab33860a1a7591a0638f9925ccf497ffa" ,
Name : "WIP" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusMerged ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640750752 ,
Parents : [ ] string {
"3d4470a6c072208722e5" ,
} ,
} ,
{
Sha : "3d4470a6c072208722e5ae9a54bcb9634959a1c5" ,
Name : "WIP" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusMerged ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640748818 ,
Parents : [ ] string {
"053a66a7be3da43aacdc" ,
} ,
} ,
{
Sha : "053a66a7be3da43aacdc7aa78e1fe757b82c4dd2" ,
Name : "refactoring the config struct" ,
2023-04-03 12:40:29 +02:00
Status : models . StatusMerged ,
2023-04-03 12:42:29 +02:00
Action : models . ActionNone ,
2021-12-30 13:11:58 +11:00
Tags : [ ] string { } ,
ExtraInfo : "" ,
2022-05-08 14:23:29 +10:00
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
2021-12-30 13:11:58 +11:00
UnixTimestamp : 1640739815 ,
Parents : [ ] string {
"985fe482e806b172aea4" ,
} ,
} ,
2019-03-02 13:08:09 +11:00
} ,
2021-12-30 13:11:58 +11:00
expectedError : nil ,
2019-03-02 13:08:09 +11:00
} ,
2023-01-28 21:10:57 +09:00
{
2023-05-09 21:41:25 +02:00
testName : "should not call merge-base for mainBranches if none exist" ,
logOrder : "topo-order" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "HEAD" , RefForPushedStatus : "mybranch" , IncludeRebaseCommits : false } ,
2023-05-09 21:41:25 +02:00
mainBranches : [ ] string { "master" , "main" } ,
runner : oscommands . NewFakeRunner ( t ) .
// here it's seeing which commits are yet to be pushed
2023-08-19 09:17:12 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2023-05-09 21:41:25 +02:00
// here it's actually getting all the commits in a formatted form, one per line
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "HEAD" , "--topo-order" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--no-show-signature" , "--" } , singleCommitOutput , nil ) .
2023-05-09 21:41:25 +02:00
// here it's testing which of the configured main branches exist; neither does
2023-06-10 10:49:18 +02:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "master@{u}" } , "" , errors . New ( "error" ) ) .
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/remotes/origin/master" } , "" , errors . New ( "error" ) ) .
2023-05-21 17:00:29 +10:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/heads/master" } , "" , errors . New ( "error" ) ) .
2023-06-10 10:49:18 +02:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "main@{u}" } , "" , errors . New ( "error" ) ) .
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/remotes/origin/main" } , "" , errors . New ( "error" ) ) .
2023-05-21 17:00:29 +10:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/heads/main" } , "" , errors . New ( "error" ) ) ,
2023-05-09 21:41:25 +02:00
expectedCommits : [ ] * models . Commit {
{
Sha : "0eea75e8c631fba6b58135697835d58ba4c18dbc" ,
Name : "better typing for rebase mode" ,
Status : models . StatusUnpushed ,
Action : models . ActionNone ,
Tags : [ ] string { } ,
ExtraInfo : "(HEAD -> better-tests)" ,
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
UnixTimestamp : 1640826609 ,
Parents : [ ] string {
"b21997d6b4cbdf84b149" ,
} ,
} ,
} ,
expectedError : nil ,
} ,
{
testName : "should call merge-base for all main branches that exist" ,
logOrder : "topo-order" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "HEAD" , RefForPushedStatus : "mybranch" , IncludeRebaseCommits : false } ,
2023-05-09 21:41:25 +02:00
mainBranches : [ ] string { "master" , "main" , "develop" , "1.0-hotfixes" } ,
runner : oscommands . NewFakeRunner ( t ) .
// here it's seeing which commits are yet to be pushed
2023-08-19 09:17:12 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2023-05-09 21:41:25 +02:00
// here it's actually getting all the commits in a formatted form, one per line
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "HEAD" , "--topo-order" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--no-show-signature" , "--" } , singleCommitOutput , nil ) .
2023-05-09 21:41:25 +02:00
// here it's testing which of the configured main branches exist
2023-06-10 10:49:18 +02:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "master@{u}" } , "refs/remotes/origin/master" , nil ) .
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "main@{u}" } , "" , errors . New ( "error" ) ) .
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/remotes/origin/main" } , "" , errors . New ( "error" ) ) .
2023-05-21 17:00:29 +10:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--verify" , "--quiet" , "refs/heads/main" } , "" , errors . New ( "error" ) ) .
2023-06-10 10:49:18 +02:00
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "develop@{u}" } , "refs/remotes/origin/develop" , nil ) .
ExpectGitArgs ( [ ] string { "rev-parse" , "--symbolic-full-name" , "1.0-hotfixes@{u}" } , "refs/remotes/origin/1.0-hotfixes" , nil ) .
2023-05-09 21:41:25 +02:00
// here it's seeing where our branch diverged from the master branch so that we can mark that commit and parent commits as 'merged'
2023-06-10 10:49:18 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "HEAD" , "refs/remotes/origin/master" , "refs/remotes/origin/develop" , "refs/remotes/origin/1.0-hotfixes" } , "26c07b1ab33860a1a7591a0638f9925ccf497ffa" , nil ) ,
2023-05-09 21:41:25 +02:00
expectedCommits : [ ] * models . Commit {
{
Sha : "0eea75e8c631fba6b58135697835d58ba4c18dbc" ,
Name : "better typing for rebase mode" ,
Status : models . StatusUnpushed ,
Action : models . ActionNone ,
Tags : [ ] string { } ,
ExtraInfo : "(HEAD -> better-tests)" ,
AuthorName : "Jesse Duffield" ,
AuthorEmail : "jessedduffield@gmail.com" ,
UnixTimestamp : 1640826609 ,
Parents : [ ] string {
"b21997d6b4cbdf84b149" ,
} ,
} ,
} ,
expectedError : nil ,
} ,
{
testName : "should not specify order if `log.order` is `default`" ,
logOrder : "default" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "HEAD" , RefForPushedStatus : "mybranch" , IncludeRebaseCommits : false } ,
2023-01-28 21:10:57 +09:00
runner : oscommands . NewFakeRunner ( t ) .
2023-08-19 09:17:12 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "HEAD" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--no-show-signature" , "--" } , "" , nil ) ,
2023-03-14 01:11:47 +09:00
expectedCommits : [ ] * models . Commit { } ,
expectedError : nil ,
} ,
{
2023-05-09 21:41:25 +02:00
testName : "should set filter path" ,
logOrder : "default" ,
rebaseMode : enums . REBASE_MODE_NONE ,
2023-08-19 09:17:12 +02:00
opts : GetCommitsOptions { RefName : "HEAD" , RefForPushedStatus : "mybranch" , FilterPath : "src" } ,
2023-03-14 01:11:47 +09:00
runner : oscommands . NewFakeRunner ( t ) .
2023-08-19 09:17:12 +02:00
ExpectGitArgs ( [ ] string { "merge-base" , "mybranch" , "mybranch@{u}" } , "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164" , nil ) .
2023-08-05 11:50:11 +02:00
ExpectGitArgs ( [ ] string { "log" , "HEAD" , "--oneline" , "--pretty=format:%H%x00%at%x00%aN%x00%ae%x00%D%x00%p%x00%s%x00%m" , "--abbrev=40" , "--follow" , "--no-show-signature" , "--" , "src" } , "" , nil ) ,
2023-01-28 21:10:57 +09:00
expectedCommits : [ ] * models . Commit { } ,
expectedError : nil ,
} ,
2019-03-02 13:08:09 +11:00
}
2021-12-30 13:11:58 +11:00
for _ , scenario := range scenarios {
2022-01-08 15:46:35 +11:00
scenario := scenario
2021-12-30 13:11:58 +11:00
t . Run ( scenario . testName , func ( t * testing . T ) {
2023-01-28 21:10:57 +09:00
common := utils . NewDummyCommon ( )
common . UserConfig . Git . Log . Order = scenario . logOrder
2021-12-30 13:35:10 +11:00
builder := & CommitLoader {
2023-05-09 21:41:25 +02:00
Common : common ,
cmd : oscommands . NewDummyCmdObjBuilder ( scenario . runner ) ,
2021-12-30 13:35:10 +11:00
getRebaseMode : func ( ) ( enums . RebaseMode , error ) { return scenario . rebaseMode , nil } ,
2021-12-30 13:11:58 +11:00
dotGitDir : ".git" ,
readFile : func ( filename string ) ( [ ] byte , error ) {
return [ ] byte ( "" ) , nil
} ,
walkFiles : func ( root string , fn filepath . WalkFunc ) error {
return nil
} ,
}
2023-05-09 21:41:25 +02:00
common . UserConfig . Git . MainBranches = scenario . mainBranches
2021-12-30 13:11:58 +11:00
commits , err := builder . GetCommits ( scenario . opts )
assert . Equal ( t , scenario . expectedCommits , commits )
assert . Equal ( t , scenario . expectedError , err )
2022-01-26 10:34:56 +11:00
scenario . runner . CheckForMissingCalls ( )
2019-03-02 13:08:09 +11:00
} )
}
}
2023-02-26 11:51:02 +01:00
func TestCommitLoader_getConflictedCommitImpl ( t * testing . T ) {
scenarios := [ ] struct {
testName string
todos [ ] todo . Todo
doneTodos [ ] todo . Todo
amendFileExists bool
expectedSha string
} {
{
testName : "no done todos" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo { } ,
amendFileExists : false ,
expectedSha : "" ,
} ,
{
testName : "common case (conflict)" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Pick ,
Commit : "deadbeef" ,
} ,
{
Command : todo . Pick ,
Commit : "fa1afe1" ,
} ,
} ,
amendFileExists : false ,
expectedSha : "fa1afe1" ,
} ,
{
testName : "last command was 'break'" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo {
{ Command : todo . Break } ,
} ,
amendFileExists : false ,
expectedSha : "" ,
} ,
{
testName : "last command was 'exec'" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Exec ,
ExecCommand : "make test" ,
} ,
} ,
amendFileExists : false ,
expectedSha : "" ,
} ,
{
testName : "last command was 'reword'" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo {
{ Command : todo . Reword } ,
} ,
amendFileExists : false ,
expectedSha : "" ,
} ,
{
testName : "'pick' was rescheduled" ,
todos : [ ] todo . Todo {
{
Command : todo . Pick ,
Commit : "fa1afe1" ,
} ,
} ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Pick ,
Commit : "fa1afe1" ,
} ,
} ,
amendFileExists : false ,
expectedSha : "" ,
} ,
{
testName : "'pick' was rescheduled, buggy git version" ,
todos : [ ] todo . Todo {
{
Command : todo . Pick ,
Commit : "fa1afe1" ,
} ,
} ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Pick ,
Commit : "deadbeaf" ,
} ,
{
Command : todo . Pick ,
Commit : "fa1afe1" ,
} ,
{
Command : todo . Pick ,
Commit : "deadbeaf" ,
} ,
} ,
amendFileExists : false ,
expectedSha : "" ,
} ,
{
testName : "conflicting 'pick' after 'exec'" ,
todos : [ ] todo . Todo {
{
Command : todo . Exec ,
ExecCommand : "make test" ,
} ,
} ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Pick ,
Commit : "deadbeaf" ,
} ,
{
Command : todo . Exec ,
ExecCommand : "make test" ,
} ,
{
Command : todo . Pick ,
Commit : "fa1afe1" ,
} ,
} ,
amendFileExists : false ,
expectedSha : "fa1afe1" ,
} ,
{
testName : "'edit' with amend file" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Edit ,
Commit : "fa1afe1" ,
} ,
} ,
amendFileExists : true ,
expectedSha : "" ,
} ,
{
testName : "'edit' without amend file" ,
todos : [ ] todo . Todo { } ,
doneTodos : [ ] todo . Todo {
{
Command : todo . Edit ,
Commit : "fa1afe1" ,
} ,
} ,
amendFileExists : false ,
expectedSha : "fa1afe1" ,
} ,
}
for _ , scenario := range scenarios {
t . Run ( scenario . testName , func ( t * testing . T ) {
common := utils . NewDummyCommon ( )
builder := & CommitLoader {
Common : common ,
cmd : oscommands . NewDummyCmdObjBuilder ( oscommands . NewFakeRunner ( t ) ) ,
getRebaseMode : func ( ) ( enums . RebaseMode , error ) { return enums . REBASE_MODE_INTERACTIVE , nil } ,
dotGitDir : ".git" ,
readFile : func ( filename string ) ( [ ] byte , error ) {
return [ ] byte ( "" ) , nil
} ,
walkFiles : func ( root string , fn filepath . WalkFunc ) error {
return nil
} ,
}
sha := builder . getConflictedCommitImpl ( scenario . todos , scenario . doneTodos , scenario . amendFileExists )
assert . Equal ( t , scenario . expectedSha , sha )
} )
}
}
2023-07-29 20:37:05 +02:00
func TestCommitLoader_setCommitMergedStatuses ( t * testing . T ) {
type scenario struct {
testName string
commits [ ] * models . Commit
ancestor string
expectedCommits [ ] * models . Commit
}
scenarios := [ ] scenario {
{
testName : "basic" ,
commits : [ ] * models . Commit {
{ Sha : "12345" , Name : "1" , Action : models . ActionNone , Status : models . StatusUnpushed } ,
{ Sha : "67890" , Name : "2" , Action : models . ActionNone , Status : models . StatusPushed } ,
{ Sha : "abcde" , Name : "3" , Action : models . ActionNone , Status : models . StatusPushed } ,
} ,
ancestor : "67890" ,
expectedCommits : [ ] * models . Commit {
{ Sha : "12345" , Name : "1" , Action : models . ActionNone , Status : models . StatusUnpushed } ,
{ Sha : "67890" , Name : "2" , Action : models . ActionNone , Status : models . StatusMerged } ,
{ Sha : "abcde" , Name : "3" , Action : models . ActionNone , Status : models . StatusMerged } ,
} ,
} ,
{
testName : "with update-ref" ,
commits : [ ] * models . Commit {
{ Sha : "12345" , Name : "1" , Action : models . ActionNone , Status : models . StatusUnpushed } ,
{ Sha : "" , Name : "" , Action : todo . UpdateRef , Status : models . StatusNone } ,
{ Sha : "abcde" , Name : "3" , Action : models . ActionNone , Status : models . StatusPushed } ,
} ,
ancestor : "deadbeef" ,
expectedCommits : [ ] * models . Commit {
{ Sha : "12345" , Name : "1" , Action : models . ActionNone , Status : models . StatusUnpushed } ,
{ Sha : "" , Name : "" , Action : todo . UpdateRef , Status : models . StatusNone } ,
2023-07-29 20:15:00 +02:00
{ Sha : "abcde" , Name : "3" , Action : models . ActionNone , Status : models . StatusPushed } ,
2023-07-29 20:37:05 +02:00
} ,
} ,
}
for _ , scenario := range scenarios {
t . Run ( scenario . testName , func ( t * testing . T ) {
2023-08-21 16:31:20 +02:00
expectedCommits := scenario . commits
setCommitMergedStatuses ( scenario . ancestor , expectedCommits )
2023-07-29 20:37:05 +02:00
assert . Equal ( t , scenario . expectedCommits , expectedCommits )
} )
}
}