-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from qmuntal/improve-errors
Improve IO error handling
- Loading branch information
Showing
11 changed files
with
925 additions
and
924 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
|
||
|
||
language: go | ||
go_import_path: github.com/qmuntal/opc | ||
|
||
go: | ||
- 1.10.x | ||
- 1.11.x | ||
|
||
env: | ||
- GO111MODULE=on | ||
|
||
notifications: | ||
- email: false | ||
|
||
before_script: | ||
- go get -u github.com/mattn/goveralls | ||
- go get -u golang.org/x/tools/cmd/cover | ||
- go get -t -v . | ||
|
||
script: | ||
- go test . -coverprofile=coverage.out -race -timeout=5s | ||
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci | ||
|
||
|
||
language: go | ||
go_import_path: github.com/qmuntal/opc | ||
|
||
go: | ||
- 1.10.x | ||
- 1.11.x | ||
- 1.12.x | ||
- 1.13.x | ||
|
||
env: | ||
- GO111MODULE=on | ||
|
||
notifications: | ||
- email: false | ||
|
||
before_script: | ||
- go get -u github.com/mattn/goveralls | ||
- go get -u golang.org/x/tools/cmd/cover | ||
- go get -t -v . | ||
|
||
script: | ||
- go test . -coverprofile=coverage.out -race -timeout=5s | ||
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
# opc | ||
|
||
[![Documentation](https://godoc.org/github.com/qmuntal/opc?status.svg)](https://godoc.org/github.com/qmuntal/opc) | ||
[![Build Status](https://travis-ci.org/qmuntal/opc.svg?branch=master)](https://travis-ci.org/qmuntal/opc) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/qmuntal/opc)](https://goreportcard.com/report/github.com/qmuntal/opc) | ||
[![codecov](https://coveralls.io/repos/github/qmuntal/opc/badge.svg)](https://coveralls.io/github/qmuntal/opc?branch=master) | ||
[![codeclimate](https://codeclimate.com/github/qmuntal/opc/badges/gpa.svg)](https://codeclimate.com/github/qmuntal/opc) | ||
[![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause) | ||
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) | ||
|
||
Package opc implements the ISO/IEC 29500-2, also known as the [Open Packaging Convention](https://en.wikipedia.org/wiki/Open_Packaging_Conventions). | ||
|
||
The Open Packaging specification describes an abstract model and physical format conventions for the use of XML, Unicode, ZIP, and other openly available technologies and specifications to organize the content and resources of a document within a package. | ||
|
||
The OPC is the foundation technology for many new file formats: .docx, .pptx, .xlsx, .3mf, .dwfx, ... | ||
|
||
## Features | ||
- [x] Package reader and writer | ||
- [x] Package core properties and relationships | ||
- [x] Part relationships | ||
- [x] ZIP mapping | ||
- [x] Package, relationships and parts validation against specs | ||
- [ ] Part interleaved pieces | ||
- [ ] Digital signatures | ||
|
||
## Examples | ||
### Write | ||
```go | ||
// Create a file to write our archive to. | ||
f, _ := os.Create("example.xlsx") | ||
|
||
// Create a new OPC archive. | ||
w := opc.NewWriter(f) | ||
|
||
// Create a new OPC part. | ||
name := opc.NormalizePartName("docs\\readme.txt") | ||
part, _ := w.Create(name, "text/plain") | ||
|
||
// Write content to the part. | ||
part.Write([]byte("This archive contains some text files.")) | ||
|
||
// Make sure to check the error on Close. | ||
w.Close() | ||
``` | ||
|
||
### Read | ||
```go | ||
r, _ := opc.OpenReader("testdata/test.xlsx") | ||
defer r.Close() | ||
|
||
// Iterate through the files in the archive, | ||
// printing some of their contents. | ||
for _, f := range r.Files { | ||
fmt.Printf("Contents of %s with type %s :\n", f.Name, f.ContentType) | ||
rc, _ := f.Open() | ||
io.CopyN(os.Stdout, rc, 68) | ||
rc.Close() | ||
fmt.Println() | ||
} | ||
``` | ||
# opc | ||
|
||
[![Documentation](https://godoc.org/github.com/qmuntal/opc?status.svg)](https://godoc.org/github.com/qmuntal/opc) | ||
[![Build Status](https://travis-ci.com/qmuntal/opc.svg?branch=master)](https://travis-ci.com/qmuntal/opc) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/qmuntal/opc)](https://goreportcard.com/report/github.com/qmuntal/opc) | ||
[![codecov](https://coveralls.io/repos/github/qmuntal/opc/badge.svg)](https://coveralls.io/github/qmuntal/opc?branch=master) | ||
[![codeclimate](https://codeclimate.com/github/qmuntal/opc/badges/gpa.svg)](https://codeclimate.com/github/qmuntal/opc) | ||
[![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause) | ||
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) | ||
|
||
Package opc implements the ISO/IEC 29500-2, also known as the [Open Packaging Convention](https://en.wikipedia.org/wiki/Open_Packaging_Conventions). | ||
|
||
The Open Packaging specification describes an abstract model and physical format conventions for the use of XML, Unicode, ZIP, and other openly available technologies and specifications to organize the content and resources of a document within a package. | ||
|
||
The OPC is the foundation technology for many new file formats: .docx, .pptx, .xlsx, .3mf, .dwfx, ... | ||
|
||
## Features | ||
- [x] Package reader and writer | ||
- [x] Package core properties and relationships | ||
- [x] Part relationships | ||
- [x] ZIP mapping | ||
- [x] Package, relationships and parts validation against specs | ||
- [ ] Part interleaved pieces | ||
- [ ] Digital signatures | ||
|
||
## Examples | ||
### Write | ||
```go | ||
// Create a file to write our archive to. | ||
f, _ := os.Create("example.xlsx") | ||
|
||
// Create a new OPC archive. | ||
w := opc.NewWriter(f) | ||
|
||
// Create a new OPC part. | ||
name := opc.NormalizePartName("docs\\readme.txt") | ||
part, _ := w.Create(name, "text/plain") | ||
|
||
// Write content to the part. | ||
part.Write([]byte("This archive contains some text files.")) | ||
|
||
// Make sure to check the error on Close. | ||
w.Close() | ||
``` | ||
|
||
### Read | ||
```go | ||
r, _ := opc.OpenReader("testdata/test.xlsx") | ||
defer r.Close() | ||
|
||
// Iterate through the files in the archive, | ||
// printing some of their contents. | ||
for _, f := range r.Files { | ||
fmt.Printf("Contents of %s with type %s :\n", f.Name, f.ContentType) | ||
rc, _ := f.Open() | ||
io.CopyN(os.Stdout, rc, 68) | ||
rc.Close() | ||
fmt.Println() | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,89 @@ | ||
package opc | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestError_Code(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want int | ||
}{ | ||
{"empty", new(Error), 0}, | ||
{"base", newError(1, "base"), 1}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.e.Code(); got != tt.want { | ||
t.Errorf("Error.Code() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestError_PartName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want string | ||
}{ | ||
{"empty", new(Error), ""}, | ||
{"base", &Error{partName: "base"}, "base"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.e.PartName(); got != tt.want { | ||
t.Errorf("Error.PartName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestError_Error(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want string | ||
wantPanic bool | ||
}{ | ||
{"base", &Error{101, "/doc.xml", ""}, "OPC: Part='/doc.xml' | Reason='a part name shall not be empty'", false}, | ||
{"panic", &Error{0, "/doc.xml", ""}, "", true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
if !tt.wantPanic { | ||
t.Errorf("Error.Error() want panic") | ||
} | ||
} | ||
}() | ||
if got := tt.e.Error(); got != tt.want { | ||
t.Errorf("Error.Error() = %v, want %v", got, tt.want) | ||
return | ||
} | ||
if tt.wantPanic { | ||
t.Error("Error.Error() want error") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestError_RelationshipID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want string | ||
}{ | ||
{"empty", new(Error), ""}, | ||
{"base", newErrorRelationship(101, "/doc.xml", "21211"), "21211"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.e.RelationshipID(); got != tt.want { | ||
t.Errorf("Error.RelationshipID() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
package opc | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestError_Code(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want int | ||
}{ | ||
{"empty", new(Error), 0}, | ||
{"base", newError(1, "base"), 1}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.e.Code(); got != tt.want { | ||
t.Errorf("Error.Code() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestError_PartName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want string | ||
}{ | ||
{"empty", new(Error), ""}, | ||
{"base", &Error{partName: "base"}, "base"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.e.PartName(); got != tt.want { | ||
t.Errorf("Error.PartName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestError_Error(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want string | ||
wantPanic bool | ||
}{ | ||
{"base", &Error{101, "/doc.xml", ""}, "opc: /doc.xml: a part name shall not be empty", false}, | ||
{"panic", &Error{0, "/doc.xml", ""}, "", true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
if !tt.wantPanic { | ||
t.Errorf("Error.Error() want panic") | ||
} | ||
} | ||
}() | ||
if got := tt.e.Error(); got != tt.want { | ||
t.Errorf("Error.Error() = %v, want %v", got, tt.want) | ||
return | ||
} | ||
if tt.wantPanic { | ||
t.Error("Error.Error() want error") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestError_RelationshipID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e *Error | ||
want string | ||
}{ | ||
{"empty", new(Error), ""}, | ||
{"base", newErrorRelationship(101, "/doc.xml", "21211"), "21211"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.e.RelationshipID(); got != tt.want { | ||
t.Errorf("Error.RelationshipID() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/qmuntal/opc | ||
|
||
require github.com/stretchr/testify v1.3.0 | ||
|
||
go 1.13 |
Oops, something went wrong.