Repository: jhillyerd/enmime
Pull request #319 (2024-01-07 20:15:39)

chore: fixed timezone for tests
The tests are failing when ran in a TZ different from GMT.

```
--- FAIL: TestEncodePartDefaultHeaders (0.00s)
    encode_test.go:71: Test output did not match testdata/encode/part-default-headers.golden
        To update golden file, run: go test -update
    encode_test.go:71: diff -want +got:
        | Content-Disposition: attachment; filename=stuff.zip; modification-date="01
        |- Feb 03 04:05 GMT"
        |+ Feb 03 05:05 CET"
        | Content-Id: <mycontentid>
        | Content-Transfer-Encoding: base64
        ...
        | WklQWklQWklQ
        | 
```

This PR fixates test run to TZ=Europe/London

coveralls: 
[![Coverage Status](https://coveralls.io/builds/64921518/badge)](https://coveralls.io/builds/64921518)

coverage: 86.283%. remained the same
when pulling **1ce26e19f72eb4e313e37fd0544c88b7fe0f6d90 on milankonir:fix-timezone-for-tests**
into **61b3eaf9b452814428173408a31b79541177612e on jhillyerd:main**.

jhillyerd: For the record, I am not in GMT/UTC, and am unable to duplicate the failure with any of:

```sh
env TZ=CET go test -race -count 1 ./...
env TZ=America/Los_Angeles go test -race -count 1 ./...
env TZ=Europe/London go test -race -count 1 ./...
```

Perhaps this is a change in a newer version of Go than the 1.20.11 I am running.  Either way, I don't think altering the Makefile is the correct way to fix this, as IDEs and GitHub CI won't be using Makefiles.

Someone contributed a similar fix to one of my other projects in https://github.com/inbucket/inbucket/pull/406/files -- that's likely to be a better approach, _if_ we can use it here.
milankonir: So this is quite interesting, the test were failing for my TZ specifically. You can reproduce yourself by running:

```
TZ=Europe/Prague go test -race ./... 
```

I assume it has to do with zone abbreviation vs current locale behavior described in https://pkg.go.dev/time#Parse

Originally, I did not want to make code changes for the sake of tests, but formatting the mod date in UTC should not do any harm.
jhillyerd: Ah, yes, your example does fail for me, interesting.

Thanks for sending in the fix.

Patch:
diff --git a/encode.go b/encode.go
index 7241a6f..5a6c522 100644
--- a/encode.go
+++ b/encode.go
@@ -154,7 +154,7 @@ func (p *Part) setupMIMEHeaders() transferEncoding {
 		param := make(map[string]string)
 		setParamValue(param, hpFilename, fileName)
 		if !p.FileModDate.IsZero() {
-			setParamValue(param, hpModDate, p.FileModDate.Format(time.RFC822))
+			setParamValue(param, hpModDate, p.FileModDate.UTC().Format(time.RFC822))
 		}
 		if mt := mime.FormatMediaType(p.Disposition, param); mt != "" {
 			p.Disposition = mt


Test patch:
diff --git a/testdata/encode/part-default-headers.golden b/testdata/encode/part-default-headers.golden
index 59686cb..6fca8c4 100644
--- a/testdata/encode/part-default-headers.golden
+++ b/testdata/encode/part-default-headers.golden
@@ -1,5 +1,5 @@
 Content-Disposition: attachment; filename=stuff.zip; modification-date="01
- Feb 03 04:05 GMT"
+ Feb 03 04:05 UTC"
 Content-Id: <mycontentid>
 Content-Transfer-Encoding: base64
 Content-Type: application/zip; boundary=enmime-abcdefg0123456789;
diff --git a/testdata/encode/part-file-mod-date.golden b/testdata/encode/part-file-mod-date.golden
index 691fdb1..556bd6b 100644
--- a/testdata/encode/part-file-mod-date.golden
+++ b/testdata/encode/part-file-mod-date.golden
@@ -1,4 +1,4 @@
-Content-Disposition: inline; modification-date="01 Feb 03 04:05 GMT"
+Content-Disposition: inline; modification-date="01 Feb 03 04:05 UTC"
 Content-Transfer-Encoding: quoted-printable
 Content-Type: text/plain; charset=utf-8
 
diff --git a/testdata/encode/part-quoted-headers.golden b/testdata/encode/part-quoted-headers.golden
index d3278ba..910a854 100644
--- a/testdata/encode/part-quoted-headers.golden
+++ b/testdata/encode/part-quoted-headers.golden
@@ -1,6 +1,6 @@
 Content-Disposition: attachment;
  filename="=?utf-8?b?w6FydsOtenTFsXLFkSAieCIgdMO8a8O2cmbDunLDs2fDqXAuemlw?=";
- modification-date="01 Feb 03 04:05 GMT"
+ modification-date="01 Feb 03 04:05 UTC"
 Content-Id: <mycontentid>
 Content-Transfer-Encoding: base64
 Content-Type: application/zip; boundary=enmime-abcdefg0123456789;
diff --git a/testdata/encode/part-quoted-printable-headers.golden b/testdata/encode/part-quoted-printable-headers.golden
index d8dd302..73cc5b1 100644
--- a/testdata/encode/part-quoted-printable-headers.golden
+++ b/testdata/encode/part-quoted-printable-headers.golden
@@ -1,6 +1,6 @@
 Content-Disposition: attachment;
  filename="=?utf-8?b?w6FydsOtenTFsXLFkSAieCIgdMO8a8O2cmbDunLDs2fDqXAuemlw?=";
- modification-date="01 Feb 03 04:05 GMT"
+ modification-date="01 Feb 03 04:05 UTC"
 Content-Id: <mycontentid>
 Content-Transfer-Encoding: base64
 Content-Type: application/zip; boundary=enmime-abcdefg0123456789;