-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_test.go
104 lines (92 loc) · 2.03 KB
/
github_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package imgbed4github
import (
"encoding/json"
"flag"
"github.com/gohouse/golib/file"
t2 "github.com/gohouse/golib/t"
"io/ioutil"
"path/filepath"
"testing"
)
// https://developer.github.com/v3/repos/contents/#create-or-update-a-file
var gh *Github
var f string
var token struct{Token string}
func initGh() {
flag.StringVar(&f,"f","config.json","配置文件")
flag.Parse()
b,err := file.NewFile(f).ReadFile()
if err!=nil {
panic(err)
}
err = t2.New(b).Bind(&token)
if err!=nil {
panic(err)
}
gh = NewGithub(token.Token, "imgbed", "images")
}
func TestNewGithub(t *testing.T) {
initGh()
t.Log(gh.owner)
}
func TestGithub_Upload(t *testing.T) {
b, err := ioutil.ReadFile("../../img/2.png")
if err != nil {
t.Error(err.Error())
return
}
res,err := gh.Upload("test/a.png", &b)
var aaa RepositoryContentResponse
res.Bind(&aaa)
t.Log(*aaa.Content.Name)
t.Log(*aaa.Content.Path)
t.Log(*aaa.Content.HTMLURL)
}
func TestGithub_UploadFromLocalFile(t *testing.T) {
res,err := gh.UploadFromLocalFile("../../img/2.png","")
if err != nil {
t.Error(err.Error())
return
}
t.Log(res.String())
}
func TestGithub_UploadFromLocalDir(t *testing.T) {
var localdir = "../../img"
res,err := gh.UploadFromLocalDir(localdir,"test2")
if err != nil {
t.Error(err.Error())
}
var result []*RepositoryContent
for _,item:=range res{
var tmp RepositoryContentResponse
item.Bind(&tmp)
result = append(result, tmp.Content)
t.Log(*tmp.Content.Path)
}
}
func TestGithub_UploadFromLocalDir2(t *testing.T) {
var localdir = "../../img"
dirs,err := ioutil.ReadDir(localdir)
if err!=nil {
return
}
for _,f := range dirs {
//fullPath := filepath.Join(localdir, f.Name())
f := filepath.Join("/test2","//", f.Name())
t.Log(f)
}
}
func TestGithub_GetContents(t *testing.T) {
initGh()
resp,err:=gh.GetContents("gif/3c1/3c1cc42404547205c4ebcc817e38f908de257699.gif")
if err!=nil {
t.Error(err.Error())
return
}
var cont []RepositoryContent
resp.Bind(&cont)
t.Log(len(cont))
b,e := json.Marshal(cont)
t.Log(e)
t.Logf("%s",b)
}