Skip to content

Commit

Permalink
chore: Email attachment testcase (Fixes #812) (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
arky authored Feb 10, 2025
1 parent eb7944f commit c831612
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
24 changes: 24 additions & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,30 @@ Sending Email
message: |
Hello world
Sending Email with Attachments
------------------------------

.. code-block:: yaml
smtp:
host: "smtp.foo.bar"
port: "587"
username: "<username>"
password: "<password>"
steps:
- name: step1
executor:
type: mail
config:
to: <to address>
from: <from address>
subject: "Sample Email"
message: |
Hello world
attachments:
- /tmp/email-attachment.txt
Customizing Signal Handling on Stop
-----------------------------------
Expand Down
9 changes: 5 additions & 4 deletions internal/digraph/executor/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ type mail struct {
}

type mailConfig struct {
From string `mapstructure:"from"`
To string `mapstructure:"to"`
Subject string `mapstructure:"subject"`
Message string `mapstructure:"message"`
From string `mapstructure:"from"`
To string `mapstructure:"to"`
Subject string `mapstructure:"subject"`
Message string `mapstructure:"message"`
Attachments []string `json:"attachments,omitempty"`
}

func newMail(ctx context.Context, step digraph.Step) (Executor, error) {
Expand Down
37 changes: 29 additions & 8 deletions internal/digraph/executor/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package executor
import (
"context"
"os"
"path/filepath"
"testing"

"github.com/dagu-org/dagu/internal/digraph"
Expand All @@ -12,9 +13,26 @@ import (
func TestMail(t *testing.T) {
t.Parallel()

// Create temporary directory for test files
tmpDir, err := os.MkdirTemp("", "email-test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)

// Create a test email attachment
attachFile := filepath.Join(tmpDir, "email.txt")
content := []byte("Test email")

os.Setenv("MAIL_SUBJECT", "Test Subject")
err = os.WriteFile(attachFile, content, 0644)
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}

t.Cleanup(func() {
os.Unsetenv("MAIL_SUBJECT")
os.Remove(attachFile)
})

t.Run("NewMail", func(t *testing.T) {
Expand All @@ -27,10 +45,11 @@ func TestMail(t *testing.T) {
step: digraph.Step{
ExecutorConfig: digraph.ExecutorConfig{
Config: map[string]any{
"from": "[email protected]",
"to": "[email protected]",
"subject": "Test Subject",
"message": "Test Message",
"from": "[email protected]",
"to": "[email protected]",
"subject": "Test Subject",
"message": "Test Message",
"attachments": attachFile,
},
},
},
Expand All @@ -40,10 +59,11 @@ func TestMail(t *testing.T) {
step: digraph.Step{
ExecutorConfig: digraph.ExecutorConfig{
Config: map[string]any{
"from": "[email protected]",
"to": "[email protected]",
"subject": "${MAIL_SUBJECT}",
"message": "Test Message",
"from": "[email protected]",
"to": "[email protected]",
"subject": "${MAIL_SUBJECT}",
"message": "Test Message",
"attachments": attachFile,
},
},
},
Expand All @@ -68,6 +88,7 @@ func TestMail(t *testing.T) {
assert.Equal(t, "[email protected]", mailExec.cfg.To)
assert.Equal(t, "Test Subject", mailExec.cfg.Subject)
assert.Equal(t, "Test Message", mailExec.cfg.Message)
assert.Equal(t, attachFile, mailExec.cfg.Attachments[0])
})
}
})
Expand Down

0 comments on commit c831612

Please sign in to comment.