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

chore: Email attachment testcase (Fixes #812) #814

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading