| 1 | package errs |
| 2 | |
| 3 | import ( |
| 4 | "io/ioutil" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "errors" |
| 9 | |
| 10 | "github.com/stretchr/testify/require" |
| 11 | ) |
| 12 | |
| 13 | func TestFileError(t *testing.T) { |
| 14 | tests := []struct { |
| 15 | err error |
| 16 | expected string |
| 17 | }{ |
| 18 | { |
| 19 | err: os.NewSyscallError("open", errors.New("out of file descriptors")), |
| 20 | expected: "open failed: out of file descriptors", |
| 21 | }, |
| 22 | { |
| 23 | err: func() error { |
| 24 | _, err := ioutil.ReadFile("im-fairly-certain-this-file-doesnt-exist") |
| 25 | require.Error(t, err) |
| 26 | return err |
| 27 | }(), |
| 28 | expected: "open im-fairly-certain-this-file-doesnt-exist failed", |
| 29 | }, |
| 30 | { |
| 31 | err: func() error { |
| 32 | err := os.Link("im-fairly-certain-this-file-doesnt-exist", "neither-does-this") |
| 33 | require.Error(t, err) |
| 34 | return err |
| 35 | }(), |
| 36 | expected: "link im-fairly-certain-this-file-doesnt-exist neither-does-this failed", |
| 37 | }, |
| 38 | } |
| 39 | for _, tt := range tests { |
| 40 | err := FileError(tt.err, "myfile") |
| 41 | require.Error(t, err) |
| 42 | require.Contains(t, err.Error(), tt.expected) |
| 43 | } |
| 44 | } |