fork of go-git with some jj specific features
at v4.7.0 5.0 kB view raw
1package git 2 3import ( 4 "context" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "gopkg.in/src-d/go-git.v4/plumbing" 11 12 . "gopkg.in/check.v1" 13 "gopkg.in/src-d/go-git-fixtures.v3" 14) 15 16type SubmoduleSuite struct { 17 BaseSuite 18 Worktree *Worktree 19 path string 20} 21 22var _ = Suite(&SubmoduleSuite{}) 23 24func (s *SubmoduleSuite) SetUpTest(c *C) { 25 path := fixtures.ByTag("submodule").One().Worktree().Root() 26 27 dir, err := ioutil.TempDir("", "submodule") 28 c.Assert(err, IsNil) 29 30 r, err := PlainClone(filepath.Join(dir, "worktree"), false, &CloneOptions{ 31 URL: path, 32 }) 33 34 c.Assert(err, IsNil) 35 36 s.Repository = r 37 s.Worktree, err = r.Worktree() 38 c.Assert(err, IsNil) 39 40 s.path = dir 41} 42 43func (s *SubmoduleSuite) TearDownTest(c *C) { 44 err := os.RemoveAll(s.path) 45 c.Assert(err, IsNil) 46} 47 48func (s *SubmoduleSuite) TestInit(c *C) { 49 sm, err := s.Worktree.Submodule("basic") 50 c.Assert(err, IsNil) 51 52 c.Assert(sm.initialized, Equals, false) 53 err = sm.Init() 54 c.Assert(err, IsNil) 55 56 c.Assert(sm.initialized, Equals, true) 57 58 cfg, err := s.Repository.Config() 59 c.Assert(err, IsNil) 60 61 c.Assert(cfg.Submodules, HasLen, 1) 62 c.Assert(cfg.Submodules["basic"], NotNil) 63 64 status, err := sm.Status() 65 c.Assert(err, IsNil) 66 c.Assert(status.IsClean(), Equals, false) 67} 68 69func (s *SubmoduleSuite) TestUpdate(c *C) { 70 if testing.Short() { 71 c.Skip("skipping test in short mode.") 72 } 73 74 sm, err := s.Worktree.Submodule("basic") 75 c.Assert(err, IsNil) 76 77 err = sm.Update(&SubmoduleUpdateOptions{ 78 Init: true, 79 }) 80 81 c.Assert(err, IsNil) 82 83 r, err := sm.Repository() 84 c.Assert(err, IsNil) 85 86 ref, err := r.Reference(plumbing.HEAD, true) 87 c.Assert(err, IsNil) 88 c.Assert(ref.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") 89 90 status, err := sm.Status() 91 c.Assert(err, IsNil) 92 c.Assert(status.IsClean(), Equals, true) 93} 94 95func (s *SubmoduleSuite) TestRepositoryWithoutInit(c *C) { 96 sm, err := s.Worktree.Submodule("basic") 97 c.Assert(err, IsNil) 98 99 r, err := sm.Repository() 100 c.Assert(err, Equals, ErrSubmoduleNotInitialized) 101 c.Assert(r, IsNil) 102} 103 104func (s *SubmoduleSuite) TestUpdateWithoutInit(c *C) { 105 sm, err := s.Worktree.Submodule("basic") 106 c.Assert(err, IsNil) 107 108 err = sm.Update(&SubmoduleUpdateOptions{}) 109 c.Assert(err, Equals, ErrSubmoduleNotInitialized) 110} 111 112func (s *SubmoduleSuite) TestUpdateWithNotFetch(c *C) { 113 sm, err := s.Worktree.Submodule("basic") 114 c.Assert(err, IsNil) 115 116 err = sm.Update(&SubmoduleUpdateOptions{ 117 Init: true, 118 NoFetch: true, 119 }) 120 121 // Since we are not fetching, the object is not there 122 c.Assert(err, Equals, plumbing.ErrObjectNotFound) 123} 124 125func (s *SubmoduleSuite) TestUpdateWithRecursion(c *C) { 126 if testing.Short() { 127 c.Skip("skipping test in short mode.") 128 } 129 130 sm, err := s.Worktree.Submodule("itself") 131 c.Assert(err, IsNil) 132 133 err = sm.Update(&SubmoduleUpdateOptions{ 134 Init: true, 135 RecurseSubmodules: 2, 136 }) 137 138 c.Assert(err, IsNil) 139 140 fs := s.Worktree.Filesystem 141 _, err = fs.Stat(fs.Join("itself", "basic", "LICENSE")) 142 c.Assert(err, IsNil) 143} 144 145func (s *SubmoduleSuite) TestUpdateWithInitAndUpdate(c *C) { 146 if testing.Short() { 147 c.Skip("skipping test in short mode.") 148 } 149 150 sm, err := s.Worktree.Submodule("basic") 151 c.Assert(err, IsNil) 152 153 err = sm.Update(&SubmoduleUpdateOptions{ 154 Init: true, 155 }) 156 c.Assert(err, IsNil) 157 158 idx, err := s.Repository.Storer.Index() 159 c.Assert(err, IsNil) 160 161 for i, e := range idx.Entries { 162 if e.Name == "basic" { 163 e.Hash = plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d") 164 } 165 166 idx.Entries[i] = e 167 } 168 169 err = s.Repository.Storer.SetIndex(idx) 170 c.Assert(err, IsNil) 171 172 err = sm.Update(&SubmoduleUpdateOptions{}) 173 c.Assert(err, IsNil) 174 175 r, err := sm.Repository() 176 c.Assert(err, IsNil) 177 178 ref, err := r.Reference(plumbing.HEAD, true) 179 c.Assert(err, IsNil) 180 c.Assert(ref.Hash().String(), Equals, "b029517f6300c2da0f4b651b8642506cd6aaf45d") 181 182} 183 184func (s *SubmoduleSuite) TestSubmodulesInit(c *C) { 185 sm, err := s.Worktree.Submodules() 186 c.Assert(err, IsNil) 187 188 err = sm.Init() 189 c.Assert(err, IsNil) 190 191 sm, err = s.Worktree.Submodules() 192 c.Assert(err, IsNil) 193 194 for _, m := range sm { 195 c.Assert(m.initialized, Equals, true) 196 } 197} 198 199func (s *SubmoduleSuite) TestGitSubmodulesSymlink(c *C) { 200 f, err := s.Worktree.Filesystem.Create("badfile") 201 c.Assert(err, IsNil) 202 defer f.Close() 203 204 err = s.Worktree.Filesystem.Remove(gitmodulesFile) 205 c.Assert(err, IsNil) 206 207 err = s.Worktree.Filesystem.Symlink("badfile", gitmodulesFile) 208 c.Assert(err, IsNil) 209 210 _, err = s.Worktree.Submodules() 211 c.Assert(err, Equals, ErrGitModulesSymlink) 212} 213 214func (s *SubmoduleSuite) TestSubmodulesStatus(c *C) { 215 sm, err := s.Worktree.Submodules() 216 c.Assert(err, IsNil) 217 218 status, err := sm.Status() 219 c.Assert(err, IsNil) 220 c.Assert(status, HasLen, 2) 221} 222 223func (s *SubmoduleSuite) TestSubmodulesUpdateContext(c *C) { 224 if testing.Short() { 225 c.Skip("skipping test in short mode.") 226 } 227 228 sm, err := s.Worktree.Submodules() 229 c.Assert(err, IsNil) 230 231 ctx, cancel := context.WithCancel(context.Background()) 232 cancel() 233 234 err = sm.UpdateContext(ctx, &SubmoduleUpdateOptions{Init: true}) 235 c.Assert(err, NotNil) 236}