8
8
"bytes"
9
9
"io"
10
10
"path/filepath"
11
+ "strings"
11
12
"testing"
12
13
13
14
"code.gitea.io/gitea/modules/util"
@@ -18,11 +19,11 @@ import (
18
19
func TestGetFormatPatch (t * testing.T ) {
19
20
bareRepo1Path := filepath .Join (testReposDir , "repo1_bare" )
20
21
clonedPath , err := cloneRepo (bareRepo1Path , testReposDir , "repo1_TestGetFormatPatch" )
21
- assert .NoError (t , err )
22
22
defer util .RemoveAll (clonedPath )
23
- repo , err := OpenRepository (clonedPath )
24
23
assert .NoError (t , err )
24
+ repo , err := OpenRepository (clonedPath )
25
25
defer repo .Close ()
26
+ assert .NoError (t , err )
26
27
rd := & bytes.Buffer {}
27
28
err = repo .GetPatch ("8d92fc95^" , "8d92fc95" , rd )
28
29
assert .NoError (t , err )
@@ -32,3 +33,49 @@ func TestGetFormatPatch(t *testing.T) {
32
33
assert .Regexp (t , "^From 8d92fc95" , patch )
33
34
assert .Contains (t , patch , "Subject: [PATCH] Add file2.txt" )
34
35
}
36
+
37
+ func TestReadPatch (t * testing.T ) {
38
+ // Ensure we can read the patch files
39
+ bareRepo1Path := filepath .Join (testReposDir , "repo1_bare" )
40
+ repo , err := OpenRepository (bareRepo1Path )
41
+ defer repo .Close ()
42
+ assert .NoError (t , err )
43
+ // This patch doesn't exist
44
+ noFile , err := repo .ReadPatchCommit (0 )
45
+ assert .Error (t , err )
46
+ // This patch is an empty one (sometimes it's a 404)
47
+ noCommit , err := repo .ReadPatchCommit (1 )
48
+ assert .Error (t , err )
49
+ // This patch is legit and should return a commit
50
+ oldCommit , err := repo .ReadPatchCommit (2 )
51
+ assert .NoError (t , err )
52
+
53
+ assert .Empty (t , noFile )
54
+ assert .Empty (t , noCommit )
55
+ assert .Len (t , oldCommit , 40 )
56
+ assert .True (t , oldCommit == "6e8e2a6f9efd71dbe6917816343ed8415ad696c3" )
57
+ }
58
+
59
+ func TestReadWritePullHead (t * testing.T ) {
60
+ // Ensure we can write SHA1 head corresponding to PR and open them
61
+ bareRepo1Path := filepath .Join (testReposDir , "repo1_bare" )
62
+ repo , err := OpenRepository (bareRepo1Path )
63
+ assert .NoError (t , err )
64
+ defer repo .Close ()
65
+ // Try to open non-existing Pull
66
+ _ , err = repo .ReadPullHead (0 )
67
+ assert .Error (t , err )
68
+ // Write a fake sha1 with only 40 zeros
69
+ newCommit := strings .Repeat ("0" , 40 )
70
+ err = repo .WritePullHead (1 , newCommit )
71
+ assert .NoError (t , err )
72
+ headFile := filepath .Join (repo .Path , "refs/pull/1/head" )
73
+ // Remove file after the test
74
+ defer util .Remove (headFile )
75
+ assert .FileExists (t , headFile )
76
+ // Read the file created
77
+ headContents , err := repo .ReadPullHead (1 )
78
+ assert .NoError (t , err )
79
+ assert .Len (t , string (headContents ), 40 )
80
+ assert .True (t , string (headContents ) == newCommit )
81
+ }
0 commit comments