@@ -4,13 +4,18 @@ import (
44 "fmt"
55 "io"
66 "os"
7+ "path/filepath"
78 "strconv"
9+ "testing"
810
11+ "github.com/go-git/go-billy/v5"
912 "github.com/go-git/go-billy/v5/osfs"
1013 "github.com/go-git/go-billy/v5/util"
1114 "github.com/go-git/go-git/v5/plumbing"
1215 "github.com/go-git/go-git/v5/plumbing/format/idxfile"
1316 "github.com/go-git/go-git/v5/plumbing/format/packfile"
17+ "github.com/stretchr/testify/assert"
18+ "github.com/stretchr/testify/require"
1419
1520 fixtures "github.com/go-git/go-git-fixtures/v4"
1621 . "gopkg.in/check.v1"
@@ -135,3 +140,78 @@ func (s *SuiteDotGit) TestPackWriterUnusedNotify(c *C) {
135140
136141 c .Assert (w .Close (), IsNil )
137142}
143+
144+ func TestPackWriterPermissions (t * testing.T ) {
145+ t .Parallel ()
146+
147+ for _ , tc := range []struct {
148+ name string
149+ fs billy.Filesystem
150+ }{
151+ {"BoundOS" , osfs .New (t .TempDir (), osfs .WithBoundOS ())},
152+ {"ChrootOS" , osfs .New (t .TempDir (), osfs .WithChrootOS ())},
153+ } {
154+ t .Run (tc .name , func (t * testing.T ) {
155+ t .Parallel ()
156+
157+ f := fixtures .Basic ().One ()
158+
159+ dot := New (tc .fs )
160+ require .NoError (t , dot .Initialize ())
161+
162+ w , err := dot .NewObjectPack ()
163+ require .NoError (t , err )
164+
165+ _ , err = io .Copy (w , f .Packfile ())
166+ require .NoError (t , err )
167+
168+ require .NoError (t , w .Close ())
169+
170+ pfPath := filepath .Join ("objects" , "pack" , fmt .Sprintf ("pack-%s.pack" , f .PackfileHash ))
171+ idxPath := filepath .Join ("objects" , "pack" , fmt .Sprintf ("pack-%s.idx" , f .PackfileHash ))
172+
173+ ro , err := isReadOnly (tc .fs , pfPath )
174+ require .NoError (t , err )
175+ assert .True (t , ro , "file %q is not read-only" , pfPath )
176+
177+ ro , err = isReadOnly (tc .fs , idxPath )
178+ require .NoError (t , err )
179+ assert .True (t , ro , "file %q is not read-only" , idxPath )
180+ })
181+ }
182+ }
183+
184+ func TestObjectWriterPermissions (t * testing.T ) {
185+ t .Parallel ()
186+
187+ for _ , tc := range []struct {
188+ name string
189+ fs billy.Filesystem
190+ }{
191+ {"BoundOS" , osfs .New (t .TempDir (), osfs .WithBoundOS ())},
192+ {"ChrootOS" , osfs .New (t .TempDir (), osfs .WithChrootOS ())},
193+ } {
194+ t .Run (tc .name , func (t * testing.T ) {
195+ t .Parallel ()
196+
197+ dot := New (tc .fs )
198+ require .NoError (t , dot .Initialize ())
199+
200+ w , err := dot .NewObject ()
201+ require .NoError (t , err )
202+
203+ err = w .WriteHeader (plumbing .BlobObject , 14 )
204+ require .NoError (t , err )
205+
206+ _ , err = w .Write ([]byte ("this is a test" ))
207+ require .NoError (t , err )
208+
209+ require .NoError (t , w .Close ())
210+
211+ path := filepath .Join ("objects" , "a8" , "a940627d132695a9769df883f85992f0ff4a43" )
212+ ro , err := isReadOnly (tc .fs , path )
213+ require .NoError (t , err )
214+ assert .True (t , ro , "file %q is not read-only" , path )
215+ })
216+ }
217+ }
0 commit comments