@@ -6,28 +6,77 @@ import (
66 "path/filepath"
77 "testing"
88
9+ "github.com/go-git/go-git/v5"
10+ "github.com/go-git/go-git/v5/plumbing"
911 "github.com/gov4git/lib4git/base"
12+ "github.com/gov4git/lib4git/must"
13+ )
14+
15+ var (
16+ testBranch = Branch ("test" )
17+ test2Branch = Branch ("test2" )
18+ test3Branch = Branch ("test3" )
1019)
1120
1221func TestCache (t * testing.T ) {
1322 base .LogVerbosely ()
14- ctx := context .Background ()
23+ ctx := WithAuth ( context .Background (), nil )
1524
1625 dir := t .TempDir ()
1726 fmt .Println ("test root " , dir )
1827 originDir := filepath .Join (dir , "origin" )
1928 cacheDir := filepath .Join (dir , "cache" )
20- originAddr := Address {Repo : URL (originDir ), Branch : MainBranch }
29+ originAddr := Address {Repo : URL (originDir ), Branch : testBranch }
2130
31+ // init origin and cache
2232 InitPlain (ctx , originDir , true )
23- cache := NewCache (ctx , cacheDir )
33+ cache := NewMirrorCache (ctx , cacheDir )
34+
2435 cloned1 := cache .Clone (ctx , originAddr )
25- populate (ctx , cloned1 .Repo (), "hi" , true )
36+ populateNonce (ctx , cloned1 .Repo (), "ok1" )
2637 cloned1 .Push (ctx )
2738
2839 cloned2 := cache .Clone (ctx , originAddr )
29- populate (ctx , cloned2 .Repo (), "ok" , false )
40+ findNonce (ctx , cloned2 .Repo (), "ok1" )
41+ populateNonce (ctx , cloned2 .Repo (), "ok2" )
42+ cloned2 .Tree ().Checkout (& git.CheckoutOptions {
43+ Branch : plumbing .NewBranchReferenceName (string (test2Branch )),
44+ Create : true ,
45+ })
46+ populateNonce (ctx , cloned2 .Repo (), "ok3" )
3047 cloned2 .Push (ctx )
3148
49+ cloned3 := cache .Clone (ctx , Address {Repo : URL (originDir ), Branch : test3Branch })
50+ populateNonce (ctx , cloned3 .Repo (), "ok5" )
51+ cloned3 .Push (ctx )
52+
53+ cloned4 := cache .Clone (ctx , Address {Repo : URL (originDir ), Branch : test2Branch })
54+ findNonce (ctx , cloned4 .Repo (), "ok3" )
55+ populateNonce (ctx , cloned4 .Repo (), "ok6" )
56+ cloned4 .Push (ctx )
57+
3258 // <-(chan int)(nil)
3359}
60+
61+ func populateNonce (ctx context.Context , r * git.Repository , nonce string ) {
62+ w , err := r .Worktree ()
63+ must .NoError (ctx , err )
64+
65+ f , err := w .Filesystem .Create (nonce )
66+ must .NoError (ctx , err )
67+ f .Write ([]byte (nonce ))
68+ err = f .Close ()
69+ must .NoError (ctx , err )
70+ _ , err = w .Add (nonce )
71+ must .NoError (ctx , err )
72+ _ , err = w .Commit (nonce , & git.CommitOptions {})
73+ must .NoError (ctx , err )
74+ }
75+
76+ func findNonce (ctx context.Context , r * git.Repository , nonce string ) {
77+ w , err := r .Worktree ()
78+ must .NoError (ctx , err )
79+
80+ _ , err = w .Filesystem .Stat (nonce )
81+ must .NoError (ctx , err )
82+ }
0 commit comments