mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-31 06:21:11 +00:00 
			
		
		
		
	- Refactor arguments of the function to make more sense. - `path` can be inferred from `repo` receiver. - `line` can be `uint64`. - The two calls to this function check for specific errors, do this error checking in the function. - The ID of a object format is not 40 in the case of SHA256, get the object format and use the correct length. - Add test coverage for `LineBlame`, notably it checks for the errors that can legitimately happen. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8419 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			52 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2025 The Forgejo Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: GPL-3.0-or-later
 | |
| 
 | |
| package git
 | |
| 
 | |
| import (
 | |
| 	"path/filepath"
 | |
| 	"testing"
 | |
| 
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| 	"github.com/stretchr/testify/require"
 | |
| )
 | |
| 
 | |
| func TestLineBlame(t *testing.T) {
 | |
| 	t.Run("SHA1", func(t *testing.T) {
 | |
| 		repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
 | |
| 		require.NoError(t, err)
 | |
| 		defer repo.Close()
 | |
| 
 | |
| 		commit, err := repo.LineBlame("HEAD", "foo/link_short", 1)
 | |
| 		require.NoError(t, err)
 | |
| 		assert.Equal(t, "37991dec2c8e592043f47155ce4808d4580f9123", commit.ID.String())
 | |
| 
 | |
| 		commit, err = repo.LineBlame("HEAD", "foo/link_short", 512)
 | |
| 		require.ErrorIs(t, err, ErrBlameFileNotEnoughLines)
 | |
| 		assert.Nil(t, commit)
 | |
| 
 | |
| 		commit, err = repo.LineBlame("HEAD", "non-existent/path", 512)
 | |
| 		require.ErrorIs(t, err, ErrBlameFileDoesNotExist)
 | |
| 		assert.Nil(t, commit)
 | |
| 	})
 | |
| 
 | |
| 	t.Run("SHA256", func(t *testing.T) {
 | |
| 		skipIfSHA256NotSupported(t)
 | |
| 
 | |
| 		repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "repo1_bare_sha256"))
 | |
| 		require.NoError(t, err)
 | |
| 		defer repo.Close()
 | |
| 
 | |
| 		commit, err := repo.LineBlame("HEAD", "foo/link_short", 1)
 | |
| 		require.NoError(t, err)
 | |
| 		assert.Equal(t, "6aae864a3d1d0d6a5be0cc64028c1e7021e2632b031fd8eb82afc5a283d1c3d1", commit.ID.String())
 | |
| 
 | |
| 		commit, err = repo.LineBlame("HEAD", "foo/link_short", 512)
 | |
| 		require.ErrorIs(t, err, ErrBlameFileNotEnoughLines)
 | |
| 		assert.Nil(t, commit)
 | |
| 
 | |
| 		commit, err = repo.LineBlame("HEAD", "non-existent/path", 512)
 | |
| 		require.ErrorIs(t, err, ErrBlameFileDoesNotExist)
 | |
| 		assert.Nil(t, commit)
 | |
| 	})
 | |
| }
 |