Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Check For Existing Files And Attachments #97

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ec61a8a
Added Check For Existing Files And Attachments
Gamma169 Aug 30, 2017
c331832
Updated Tests To Actually Create File
Gamma169 Aug 30, 2017
92faf95
Unfortunate fork ;-( Original maintainer missing/uncontactable
pedromorgan Dec 6, 2017
3e55582
Fix imports
pedromorgan Dec 6, 2017
594fad0
fix coverage link
pedromorgan Dec 6, 2017
f10eb0a
Export NetDialTimeout to public
Elbandi Oct 8, 2017
8340910
Document NetDialTimeout
ivy Dec 6, 2017
ab8dca3
Merge pull request #4 from ivy/export-net-dial-timeout
pedromorgan Dec 6, 2017
8dc4458
Add syntax highlighting to Go example
ivy Dec 6, 2017
c0dd687
Ditch go < 1.5 (and no external dependancies)
pedromorgan Dec 6, 2017
4e2ebb0
make travis check master+v2 for now
pedromorgan Dec 6, 2017
0fbc7e5
Merge pull request #5 from ivy/syntax-highlighting
pedromorgan Dec 6, 2017
00ccffb
gitter
pedromorgan Dec 6, 2017
875d2d5
revert removal < 1.5 and small fixes
pedromorgan Dec 6, 2017
45ab546
Allow timeout in connections, retries to be configurable
connor4312 Dec 9, 2016
0fb8809
Document default Dialer.RetryFailure
ivy Dec 7, 2017
73cb68f
Merge pull request #10 from ivy/user-defined-timeouts
ivy Dec 7, 2017
8367efd
Remove gitter badge
ivy Dec 7, 2017
1e5036a
Rename package to mail
ivy Dec 7, 2017
a691f04
Update readme with gopkg.in import path
ivy Dec 7, 2017
c6c7e1d
Fix import path in readme example
ivy Dec 7, 2017
6a770e9
Fix references to old repo name
ivy Dec 7, 2017
7c2224c
travis: Fix incorrectly specified Go versions
ivy Dec 7, 2017
1f3725f
Fix missing import in mime_go14.go
ivy Dec 7, 2017
ef37398
Document progress and steps for transitioning
ivy Dec 7, 2017
71893bb
Added teardown method for files
Gamma169 Dec 9, 2017
cd0ee12
Removed Rename File Creation
Gamma169 Dec 10, 2017
8a0aa62
Fixed Build Typo
Gamma169 Dec 10, 2017
8ca38ac
Added originalName property for file
Gamma169 Dec 10, 2017
c491950
Fixed Typo with originalName
Gamma169 Dec 10, 2017
09b474d
Added Tests for Embed/Attachment Validation
Gamma169 Dec 10, 2017
c117660
Fogot to add strings import
Gamma169 Dec 10, 2017
c273b3f
Merge branch 'v2' into Fixing-Attachment-Error
Gamma169 Dec 10, 2017
f5e292e
Removed first net/mail import
Gamma169 Dec 10, 2017
4a60beb
Revert "Fogot to add strings import"
Gamma169 Dec 10, 2017
aaa67cd
Revert A bunch of things
Gamma169 Dec 10, 2017
bafcd84
Revert "Removed first net/mail import"
Gamma169 Dec 10, 2017
3e277d0
Revert "Merge branch 'v2' into Fixing-Attachment-Error"
Gamma169 Dec 10, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func SetPartEncoding(e Encoding) PartSetting {

type file struct {
Name string
originalName string
Header map[string][]string
CopyFunc func(w io.Writer) error
}
Expand Down Expand Up @@ -285,6 +286,7 @@ func SetCopyFunc(f func(io.Writer) error) FileSetting {

func (m *Message) appendFile(list []*file, name string, settings []FileSetting) []*file {
f := &file{
originalName: filepath.Base(name),
Name: filepath.Base(name),
Header: make(map[string][]string),
CopyFunc: func(w io.Writer) error {
Expand Down
49 changes: 49 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gomail

import (
"os"
"bytes"
"encoding/base64"
"io"
Expand Down Expand Up @@ -256,6 +257,9 @@ func TestAttachmentOnly(t *testing.T) {
}

testMessage(t, m, 0, want)
if err := teardownFile("/tmp/test.pdf"); err != nil {
panic(err)
}
}

func TestAttachment(t *testing.T) {
Expand Down Expand Up @@ -288,6 +292,9 @@ func TestAttachment(t *testing.T) {
}

testMessage(t, m, 1, want)
if err := teardownFile("/tmp/test.pdf"); err != nil {
panic(err)
}
}

func TestRename(t *testing.T) {
Expand All @@ -297,6 +304,8 @@ func TestRename(t *testing.T) {
m.SetBody("text/plain", "Test")
name, copy := mockCopyFile("/tmp/test.pdf")
rename := Rename("another.pdf")


m.Attach(name, copy, rename)

want := &message{
Expand All @@ -322,6 +331,9 @@ func TestRename(t *testing.T) {
}

testMessage(t, m, 1, want)
if err := teardownFile("/tmp/test.pdf"); err != nil {
panic(err)
}
}

func TestAttachmentsOnly(t *testing.T) {
Expand Down Expand Up @@ -355,6 +367,12 @@ func TestAttachmentsOnly(t *testing.T) {
}

testMessage(t, m, 1, want)
if err := teardownFile("/tmp/test.pdf"); err != nil {
panic(err)
}
if err := teardownFile("/tmp/test.zip"); err != nil {
panic(err)
}
}

func TestAttachments(t *testing.T) {
Expand Down Expand Up @@ -394,6 +412,12 @@ func TestAttachments(t *testing.T) {
}

testMessage(t, m, 1, want)
if err := teardownFile("/tmp/test.pdf"); err != nil {
panic(err)
}
if err := teardownFile("/tmp/test.zip"); err != nil {
panic(err)
}
}

func TestEmbedded(t *testing.T) {
Expand Down Expand Up @@ -435,6 +459,12 @@ func TestEmbedded(t *testing.T) {
}

testMessage(t, m, 1, want)
if err := teardownFile("image1.jpg"); err != nil {
panic(err)
}
if err := teardownFile("image2.jpg"); err != nil {
panic(err)
}
}

func TestFullMessage(t *testing.T) {
Expand Down Expand Up @@ -494,6 +524,14 @@ func TestFullMessage(t *testing.T) {

testMessage(t, m, 3, want)

if err := teardownFile("test.pdf"); err != nil {
panic(err)
}
if err := teardownFile("image.jpg"); err != nil {
panic(err)
}


want = &message{
from: "from@example.com",
to: []string{"to@example.com"},
Expand Down Expand Up @@ -705,6 +743,7 @@ func getBoundaries(t *testing.T, count int, m string) []string {
var boundaryRegExp = regexp.MustCompile("boundary=(\\w+)")

func mockCopyFile(name string) (string, FileSetting) {
os.Create(filepath.Base(name))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but won't this also create a bunch of files in the working directory? We need a teardown function to remove them when the test suite finishes. Also, this ends up being a step away from an actual copyFile and not a mock.

I need to think about this more. I'll come back after I've reviewed some of the other pull requests.

return name, SetCopyFunc(func(w io.Writer) error {
_, err := w.Write([]byte("Content of " + filepath.Base(name)))
return err
Expand All @@ -716,6 +755,10 @@ func mockCopyFileWithHeader(m *Message, name string, h map[string][]string) (str
return name, f, SetHeader(h)
}

func teardownFile(name string) error {
return os.Remove(filepath.Base(name))
}

func BenchmarkFull(b *testing.B) {
discardFunc := SendFunc(func(from string, to []string, m io.WriterTo) error {
_, err := m.WriteTo(ioutil.Discard)
Expand All @@ -741,5 +784,11 @@ func BenchmarkFull(b *testing.B) {
panic(err)
}
m.Reset()
if err := teardownFile("benchmark.txt"); err != nil {
panic(err)
}
if err := teardownFile("benchmark.jpg"); err != nil {
panic(err)
}
}
}
19 changes: 19 additions & 0 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/mail"
"os"
)

// Sender is the interface that wraps the Send method.
Expand Down Expand Up @@ -54,6 +55,10 @@ func send(s Sender, m *Message) error {
return err
}

if err := m.checkEmbedsAndAttachments(); err != nil {
return err
}

if err := s.Send(from, to, m); err != nil {
return err
}
Expand Down Expand Up @@ -97,6 +102,20 @@ func (m *Message) getRecipients() ([]string, error) {
return list, nil
}

func (m *Message) checkEmbedsAndAttachments() error {
for _, file := range m.embedded {
if _, err := os.Stat(file.originalName); err != nil {
return err
}
}
for _, file := range m.attachments {
if _, err := os.Stat(file.originalName); err != nil {
return err
}
}
return nil
}

func addAddress(list []string, addr string) []string {
for _, a := range list {
if addr == a {
Expand Down
42 changes: 42 additions & 0 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"reflect"
"testing"
"strings"
)

const (
Expand Down Expand Up @@ -50,6 +51,47 @@ func TestSend(t *testing.T) {
}
}

func TestSendValidatesEmbeds(t *testing.T) {
s := &mockSendCloser{
mockSender: stubSend(t, testFrom, []string{testTo1, testTo2}, testMsg),
close: func() error {
t.Error("Close() should not be called in Send()")
return nil
},
}

m := getTestMessage()
m.Embed("this-file-does-not-exist")

err := Send(s, m)
if err == nil || !strings.HasSuffix(err.Error(),
"no such file or directory") {
t.Errorf("Send(): expected stat error but got %v", err)
}
}


func TestSendValidatesAttachments(t *testing.T) {
s := &mockSendCloser{
mockSender: stubSend(t, testFrom, []string{testTo1, testTo2}, testMsg),
close: func() error {
t.Error("Close() should not be called in Send()")
return nil
},
}

m := getTestMessage()
m.Attach("this-file-does-not-exist")

err := Send(s, m)
if err == nil || !strings.HasSuffix(err.Error(),
"no such file or directory") {
t.Errorf("Send(): expected stat error but got %v", err)
}
}



func getTestMessage() *Message {
m := NewMessage()
m.SetHeader("From", testFrom)
Expand Down