Skip to content

Commit

Permalink
Add error handling for writing EML string to temp file
Browse files Browse the repository at this point in the history
Added conditional statements to handle potential failures when writing the EML string to the temporary files during testing. These updates ensure test failures due to write errors are accurately reflected in test results. Also, a minor fix is implemented on file permission in the os.WriteFile function.
  • Loading branch information
wneessen committed Jun 28, 2024
1 parent d929097 commit 7e4bb00
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion eml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ func TestEMLToMsgFromReaderFailing(t *testing.T) {

func TestEMLToMsgFromFileFailing(t *testing.T) {
tempDir, tempFile, err := stringToTempFile(exampleMailPlainBrokenFrom, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with broken FROM was supposed to fail, but didn't")
Expand All @@ -661,6 +664,9 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
t.Error("failed to remove temp dir:", err)
}
tempDir, tempFile, err = stringToTempFile(exampleMailPlainBrokenHeader, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with broken header was supposed to fail, but didn't")
Expand All @@ -669,6 +675,9 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
t.Error("failed to remove temp dir:", err)
}
tempDir, tempFile, err = stringToTempFile(exampleMailPlainB64BrokenBody, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with broken body was supposed to fail, but didn't")
Expand All @@ -677,6 +686,9 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
t.Error("failed to remove temp dir:", err)
}
tempDir, tempFile, err = stringToTempFile(exampleMailPlainBrokenBody, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with broken body was supposed to fail, but didn't")
Expand All @@ -685,6 +697,9 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
t.Error("failed to remove temp dir:", err)
}
tempDir, tempFile, err = stringToTempFile(exampleMailPlainUnknownContentType, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with unknown content type was supposed to fail, but didn't")
Expand All @@ -693,6 +708,9 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
t.Error("failed to remove temp dir:", err)
}
tempDir, tempFile, err = stringToTempFile(exampleMailPlainNoContentType, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with no content type was supposed to fail, but didn't")
Expand All @@ -701,6 +719,9 @@ func TestEMLToMsgFromFileFailing(t *testing.T) {
t.Error("failed to remove temp dir:", err)
}
tempDir, tempFile, err = stringToTempFile(exampleMailPlainUnsupportedTransferEnc, "testmail")
if err != nil {
t.Errorf("failed to write EML string to temp file: %s", err)
}
_, err = EMLToMsgFromFile(tempFile)
if err == nil {
t.Error("EML from Reader with unsupported Transer-Encoding was supposed to fail, but didn't")
Expand Down Expand Up @@ -852,7 +873,7 @@ func stringToTempFile(data, name string) (string, string, error) {
return tempDir, "", fmt.Errorf("failed to create temp dir: %w", err)
}
filePath := fmt.Sprintf("%s/%s", tempDir, name)
err = os.WriteFile(filePath, []byte(data), 0666)
err = os.WriteFile(filePath, []byte(data), 0o666)
if err != nil {
return tempDir, "", fmt.Errorf("failed to write data to temp file: %w", err)
}
Expand Down

0 comments on commit 7e4bb00

Please sign in to comment.