@@ -133,3 +133,100 @@ func (s *S) TestCreate(c *C) {
133133 c .Assert (testutil .TreeDumpEntry (entry ), DeepEquals , test .result [slashPath ])
134134 }
135135}
136+
137+ type createWriterTest struct {
138+ options fsutil.CreateOptions
139+ data []byte
140+ hackdir func (c * C , dir string )
141+ result map [string ]string
142+ error string
143+ }
144+
145+ var createWriterTests = []createWriterTest {{
146+ options : fsutil.CreateOptions {
147+ Path : "foo" ,
148+ Mode : 0644 ,
149+ },
150+ data : []byte ("foo" ),
151+ result : map [string ]string {
152+ "/foo" : "file 0644 2c26b46b" ,
153+ },
154+ }, {
155+ options : fsutil.CreateOptions {
156+ Path : "foo" ,
157+ Mode : 0644 | fs .ModeDir ,
158+ },
159+ error : `unsupported file type: \/[a-z0-9\-\/]*foo` ,
160+ }, {
161+ options : fsutil.CreateOptions {
162+ Path : "foo" ,
163+ Mode : 0644 | fs .ModeSymlink ,
164+ },
165+ error : `unsupported file type: /[a-z0-9\-\/]*/foo` ,
166+ }, {
167+ options : fsutil.CreateOptions {
168+ Path : "foo/bar" ,
169+ Mode : 0644 ,
170+ MakeParents : true ,
171+ },
172+ data : []byte ("foo" ),
173+ result : map [string ]string {
174+ "/foo/" : "dir 0755" ,
175+ "/foo/bar" : "file 0644 2c26b46b" ,
176+ },
177+ }, {
178+ options : fsutil.CreateOptions {
179+ Path : "foo/bar" ,
180+ Mode : 0644 ,
181+ MakeParents : false ,
182+ },
183+ error : `open /[a-z0-9\-\/]*/foo/bar: no such file or directory` ,
184+ }}
185+
186+ func (s * S ) TestCreateWriter (c * C ) {
187+ oldUmask := syscall .Umask (0 )
188+ defer func () {
189+ syscall .Umask (oldUmask )
190+ }()
191+
192+ for _ , test := range createWriterTests {
193+ if test .result == nil {
194+ // Empty map for no files created.
195+ test .result = make (map [string ]string )
196+ }
197+ c .Logf ("Options: %v" , test .options )
198+ dir := c .MkDir ()
199+ if test .hackdir != nil {
200+ test .hackdir (c , dir )
201+ }
202+ options := test .options
203+ options .Path = filepath .Join (dir , options .Path )
204+ writer , entry , err := fsutil .CreateWriter (& options )
205+ if test .error != "" {
206+ c .Assert (err , ErrorMatches , test .error )
207+ continue
208+ }
209+ c .Assert (err , IsNil )
210+
211+ // Hash and Size are only set when the writer is closed.
212+ _ , err = writer .Write (test .data )
213+ c .Assert (err , IsNil )
214+ c .Assert (entry .Path , Equals , options .Path )
215+ c .Assert (entry .Mode , Equals , options .Mode )
216+ c .Assert (entry .Hash , Equals , "" )
217+ c .Assert (entry .Size , Equals , 0 )
218+ err = writer .Close ()
219+ c .Assert (err , IsNil )
220+
221+ c .Assert (testutil .TreeDump (dir ), DeepEquals , test .result )
222+ // [fsutil.CreateWriter] does not return information about parent
223+ // directories created implicitly. We only check for the requested path.
224+ entry .Path = strings .TrimPrefix (entry .Path , dir )
225+ // Add the slashes that TreeDump adds to the path.
226+ slashPath := "/" + test .options .Path
227+ if test .options .Mode .IsDir () {
228+ slashPath = slashPath + "/"
229+ }
230+ c .Assert (testutil .TreeDumpEntry (entry ), DeepEquals , test .result [slashPath ])
231+ }
232+ }
0 commit comments