@@ -904,27 +904,34 @@ func TestGetDir(t *testing.T) {
904904 }
905905}
906906
907- func TestGetHashForPathOrEmpty (t * testing.T ) {
907+ // TestGetHashForPath tests the internal getHashForPath, but
908+ // via the public GetHashForPath function which accepts a commit hash
909+ // instead of a Commit object, to avoid duplicate testing.
910+ func TestGetHashForPath (t * testing.T ) {
908911 t .Parallel ()
909912
910- setupInitialRepo := func (t * testing.T ) (* git. Repository , * object.Commit ) {
913+ setupInitialRepo := func (t * testing.T ) (LocalRepository , * object.Commit ) {
911914 t .Helper ()
912- repo , _ := initTestRepo (t )
915+ repo , dir := initTestRepo (t )
913916 commit := createAndCommit (t , repo , "initial.txt" , []byte ("initial content" ), "initial commit" )
914- return repo , commit
917+ localRepository := LocalRepository {
918+ Dir : dir ,
919+ repo : repo ,
920+ }
921+ return localRepository , commit
915922 }
916923
917924 for _ , test := range []struct {
918925 name string
919- setup func (t * testing.T ) (commit * object.Commit , path string )
926+ setup func (t * testing.T ) (repo LocalRepository , commit * object.Commit , path string )
920927 wantHash func (commit * object.Commit , path string ) string
921928 wantErr bool
922929 }{
923930 {
924931 name : "existing file" ,
925- setup : func (t * testing.T ) (* object.Commit , string ) {
926- _ , commit := setupInitialRepo (t )
927- return commit , "initial.txt"
932+ setup : func (t * testing.T ) (LocalRepository , * object.Commit , string ) {
933+ localRepository , commit := setupInitialRepo (t )
934+ return localRepository , commit , "initial.txt"
928935 },
929936 wantHash : func (commit * object.Commit , path string ) string {
930937 tree , err := commit .Tree ()
@@ -940,8 +947,9 @@ func TestGetHashForPathOrEmpty(t *testing.T) {
940947 },
941948 {
942949 name : "existing directory" ,
943- setup : func (t * testing.T ) (* object.Commit , string ) {
944- repo , _ := setupInitialRepo (t )
950+ setup : func (t * testing.T ) (LocalRepository , * object.Commit , string ) {
951+ localRepository , _ := setupInitialRepo (t )
952+ repo := localRepository .repo
945953 // Create a directory and a file inside it to ensure the directory gets a hash
946954 _ = createAndCommit (t , repo , "my_dir/file_in_dir.txt" , []byte ("content of file in dir" ), "add dir and file" )
947955 head , err := repo .Head ()
@@ -952,7 +960,7 @@ func TestGetHashForPathOrEmpty(t *testing.T) {
952960 if err != nil {
953961 t .Fatalf ("repo.CommitObject failed: %v" , err )
954962 }
955- return commit , "my_dir"
963+ return localRepository , commit , "my_dir"
956964 },
957965 wantHash : func (commit * object.Commit , path string ) string {
958966 tree , err := commit .Tree ()
@@ -968,28 +976,29 @@ func TestGetHashForPathOrEmpty(t *testing.T) {
968976 },
969977 {
970978 name : "non-existent file" ,
971- setup : func (t * testing.T ) (* object.Commit , string ) {
972- _ , commit := setupInitialRepo (t )
973- return commit , "non_existent_file.txt"
979+ setup : func (t * testing.T ) (LocalRepository , * object.Commit , string ) {
980+ localRepository , commit := setupInitialRepo (t )
981+ return localRepository , commit , "non_existent_file.txt"
974982 },
975983 wantHash : func (commit * object.Commit , path string ) string {
976984 return ""
977985 },
978986 },
979987 {
980988 name : "non-existent directory" ,
981- setup : func (t * testing.T ) (* object.Commit , string ) {
982- _ , commit := setupInitialRepo (t )
983- return commit , "non_existent_dir"
989+ setup : func (t * testing.T ) (LocalRepository , * object.Commit , string ) {
990+ localRepository , commit := setupInitialRepo (t )
991+ return localRepository , commit , "non_existent_dir"
984992 },
985993 wantHash : func (commit * object.Commit , path string ) string {
986994 return ""
987995 },
988996 },
989997 {
990998 name : "file in subdirectory" ,
991- setup : func (t * testing.T ) (* object.Commit , string ) {
992- repo , _ := setupInitialRepo (t )
999+ setup : func (t * testing.T ) (LocalRepository , * object.Commit , string ) {
1000+ localRepository , _ := setupInitialRepo (t )
1001+ repo := localRepository .repo
9931002 _ = createAndCommit (t , repo , "another_dir/sub_dir/nested_file.txt" , []byte ("nested content" ), "add nested file" )
9941003 head , err := repo .Head ()
9951004 if err != nil {
@@ -999,7 +1008,7 @@ func TestGetHashForPathOrEmpty(t *testing.T) {
9991008 if err != nil {
10001009 t .Fatalf ("repo.CommitObject failed: %v" , err )
10011010 }
1002- return commit , "another_dir/sub_dir/nested_file.txt"
1011+ return localRepository , commit , "another_dir/sub_dir/nested_file.txt"
10031012 },
10041013 wantHash : func (commit * object.Commit , path string ) string {
10051014 tree , err := commit .Tree ()
@@ -1017,17 +1026,52 @@ func TestGetHashForPathOrEmpty(t *testing.T) {
10171026 t .Run (test .name , func (t * testing.T ) {
10181027 t .Parallel ()
10191028
1020- commit , path := test .setup (t )
1029+ localRepository , commit , path := test .setup (t )
10211030
1022- got , err := getHashForPathOrEmpty (commit , path )
1031+ got , err := localRepository . GetHashForPath (commit . Hash . String () , path )
10231032 if (err != nil ) != test .wantErr {
1024- t .Errorf ("getHashForPathOrEmpty () error = %v, wantErr %v" , err , test .wantErr )
1033+ t .Errorf ("getHashForPath () error = %v, wantErr %v" , err , test .wantErr )
10251034 return
10261035 }
10271036
10281037 wantHash := test .wantHash (commit , path )
10291038 if diff := cmp .Diff (wantHash , got ); diff != "" {
1030- t .Errorf ("getHashForPathOrEmpty() mismatch (-want +got):\n %s" , diff )
1039+ t .Errorf ("getHashForPath() mismatch (-want +got):\n %s" , diff )
1040+ }
1041+ })
1042+ }
1043+ }
1044+
1045+ // TestGetHashForPathBadCommitHash tests the one path not
1046+ // otherwise tested in TestGetHashForPath, where we can't
1047+ // get the commit for the hash.
1048+ func TestGetHashForPathBadCommitHash (t * testing.T ) {
1049+ repo , dir := initTestRepo (t )
1050+ localRepository := LocalRepository {
1051+ Dir : dir ,
1052+ repo : repo ,
1053+ }
1054+ for _ , test := range []struct {
1055+ name string
1056+ commitHash string
1057+ }{
1058+ {
1059+ name : "empty hash" ,
1060+ commitHash : "" ,
1061+ },
1062+ {
1063+ name : "invalid hash" ,
1064+ commitHash : "bad-hash" ,
1065+ },
1066+ {
1067+ name : "hash not in repo" ,
1068+ commitHash : "d93e160f57f0a6eccd6e230dd40f465988bede63" ,
1069+ },
1070+ } {
1071+ t .Run (test .name , func (t * testing.T ) {
1072+ _ , err := localRepository .GetHashForPath (test .commitHash , "path/to/file" )
1073+ if err == nil {
1074+ t .Error ("GetHashForPath() err = nil, should fail when an invalid or absent hash is provided" )
10311075 }
10321076 })
10331077 }
0 commit comments