MCPcopy
hub / github.com/qax-os/excelize / TestOpenReader

Function TestOpenReader

excelize_test.go:251–355  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

249}
250
251func TestOpenReader(t *testing.T) {
252 _, err := OpenReader(strings.NewReader(""))
253 assert.EqualError(t, err, zip.ErrFormat.Error())
254 _, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1})
255 assert.EqualError(t, err, ErrWorkbookFileFormat.Error())
256
257 // Prepare unusual workbook, made the specified internal XML parts missing
258 // or contain unsupported charset
259 preset := func(filePath string, notExist bool) *bytes.Buffer {
260 source, err := zip.OpenReader(filepath.Join("test", "Book1.xlsx"))
261 assert.NoError(t, err)
262 buf := new(bytes.Buffer)
263 zw := zip.NewWriter(buf)
264 for _, item := range source.File {
265 // The following statements can be simplified as zw.Copy(item) in go1.17
266 if notExist && item.Name == filePath {
267 continue
268 }
269 writer, err := zw.Create(item.Name)
270 assert.NoError(t, err)
271 readerCloser, err := item.Open()
272 assert.NoError(t, err)
273 _, err = io.Copy(writer, readerCloser)
274 assert.NoError(t, err)
275 }
276 if !notExist {
277 fi, err := zw.Create(filePath)
278 assert.NoError(t, err)
279 _, err = fi.Write(MacintoshCyrillicCharset)
280 assert.NoError(t, err)
281 }
282 assert.NoError(t, zw.Close())
283 return buf
284 }
285 // Test open workbook with unsupported charset internal XML parts
286 for _, defaultXMLPath := range []string{
287 defaultXMLPathCalcChain,
288 defaultXMLPathStyles,
289 defaultXMLPathWorkbookRels,
290 } {
291 _, err = OpenReader(preset(defaultXMLPath, false))
292 assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
293 }
294 // Test open workbook without internal XML parts
295 for _, defaultXMLPath := range []string{
296 defaultXMLPathCalcChain,
297 defaultXMLPathStyles,
298 defaultXMLPathWorkbookRels,
299 } {
300 _, err = OpenReader(preset(defaultXMLPath, true))
301 assert.NoError(t, err)
302 }
303
304 // Test open spreadsheet with unzip size limit
305 _, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100})
306 assert.EqualError(t, err, newUnzipSizeLimitError(100).Error())
307
308 // Test open password protected spreadsheet created by Microsoft Office Excel 2010

Callers

nothing calls this directly

Calls 9

OpenReaderFunction · 0.85
OpenFileFunction · 0.85
newUnzipSizeLimitErrorFunction · 0.85
CreateMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.45
WriteMethod · 0.45
GetCellValueMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected