2019-04-20 05:47:00 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-04-20 05:47:00 +03:00
2019-06-26 21:15:26 +03:00
package git
2019-04-20 05:47:00 +03:00
import (
2020-07-01 16:01:17 +03:00
"context"
2019-04-20 05:47:00 +03:00
"testing"
"github.com/stretchr/testify/assert"
2024-07-30 22:41:10 +03:00
"github.com/stretchr/testify/require"
2019-04-20 05:47:00 +03:00
)
func TestReadingBlameOutput ( t * testing . T ) {
2020-07-01 16:01:17 +03:00
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
2019-04-20 05:47:00 +03:00
2023-09-16 20:42:34 +03:00
t . Run ( "Without .git-blame-ignore-revs" , func ( t * testing . T ) {
repo , err := OpenRepository ( ctx , "./tests/repos/repo5_pulls" )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
defer repo . Close ( )
commit , err := repo . GetCommit ( "f32b0a9dfd09a60f616f29158f772cedd89942d2" )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
parts := [ ] * BlamePart {
{
2023-12-01 04:26:52 +03:00
Sha : "72866af952e98d02a73003501836074b286a78f6" ,
Lines : [ ] string {
2023-09-16 20:42:34 +03:00
"# test_repo" ,
"Test repository for testing migration from github to gitea" ,
} ,
} ,
{
2023-12-01 04:26:52 +03:00
Sha : "f32b0a9dfd09a60f616f29158f772cedd89942d2" ,
Lines : [ ] string { "" , "Do not make any changes to this repo it is used for unit testing" } ,
PreviousSha : "72866af952e98d02a73003501836074b286a78f6" ,
PreviousPath : "README.md" ,
2023-09-16 20:42:34 +03:00
} ,
}
for _ , bypass := range [ ] bool { false , true } {
2023-12-17 14:56:08 +03:00
blameReader , err := CreateBlameReader ( ctx , Sha1ObjectFormat , "./tests/repos/repo5_pulls" , commit , "README.md" , bypass )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
assert . NotNil ( t , blameReader )
defer blameReader . Close ( )
assert . False ( t , blameReader . UsesIgnoreRevs ( ) )
for _ , part := range parts {
actualPart , err := blameReader . NextPart ( )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
assert . Equal ( t , part , actualPart )
}
// make sure all parts have been read
actualPart , err := blameReader . NextPart ( )
assert . Nil ( t , actualPart )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
}
} )
t . Run ( "With .git-blame-ignore-revs" , func ( t * testing . T ) {
repo , err := OpenRepository ( ctx , "./tests/repos/repo6_blame" )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
defer repo . Close ( )
full := [ ] * BlamePart {
{
2023-12-01 04:26:52 +03:00
Sha : "af7486bd54cfc39eea97207ca666aa69c9d6df93" ,
Lines : [ ] string { "line" , "line" } ,
2023-09-16 20:42:34 +03:00
} ,
{
2023-12-01 04:26:52 +03:00
Sha : "45fb6cbc12f970b04eacd5cd4165edd11c8d7376" ,
Lines : [ ] string { "changed line" } ,
PreviousSha : "af7486bd54cfc39eea97207ca666aa69c9d6df93" ,
PreviousPath : "blame.txt" ,
2023-09-16 20:42:34 +03:00
} ,
{
2023-12-01 04:26:52 +03:00
Sha : "af7486bd54cfc39eea97207ca666aa69c9d6df93" ,
Lines : [ ] string { "line" , "line" , "" } ,
2023-09-16 20:42:34 +03:00
} ,
}
cases := [ ] struct {
CommitID string
UsesIgnoreRevs bool
Bypass bool
Parts [ ] * BlamePart
} {
{
CommitID : "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7" ,
UsesIgnoreRevs : true ,
Bypass : false ,
Parts : [ ] * BlamePart {
{
2023-12-01 04:26:52 +03:00
Sha : "af7486bd54cfc39eea97207ca666aa69c9d6df93" ,
Lines : [ ] string { "line" , "line" , "changed line" , "line" , "line" , "" } ,
2023-09-16 20:42:34 +03:00
} ,
} ,
} ,
{
CommitID : "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7" ,
UsesIgnoreRevs : false ,
Bypass : true ,
Parts : full ,
} ,
{
CommitID : "45fb6cbc12f970b04eacd5cd4165edd11c8d7376" ,
UsesIgnoreRevs : false ,
Bypass : false ,
Parts : full ,
} ,
{
CommitID : "45fb6cbc12f970b04eacd5cd4165edd11c8d7376" ,
UsesIgnoreRevs : false ,
Bypass : false ,
Parts : full ,
} ,
}
2024-03-12 07:21:27 +03:00
objectFormat , err := repo . GetObjectFormat ( )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
for _ , c := range cases {
commit , err := repo . GetCommit ( c . CommitID )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
2024-03-12 07:21:27 +03:00
blameReader , err := CreateBlameReader ( ctx , objectFormat , "./tests/repos/repo6_blame" , commit , "blame.txt" , c . Bypass )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
assert . NotNil ( t , blameReader )
defer blameReader . Close ( )
assert . Equal ( t , c . UsesIgnoreRevs , blameReader . UsesIgnoreRevs ( ) )
for _ , part := range c . Parts {
actualPart , err := blameReader . NextPart ( )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
assert . Equal ( t , part , actualPart )
}
// make sure all parts have been read
actualPart , err := blameReader . NextPart ( )
assert . Nil ( t , actualPart )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-09-16 20:42:34 +03:00
}
} )
2019-04-20 05:47:00 +03:00
}