summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-05-10 09:58:54 +0200
committerRobin Jarry <robin@jarry.cc>2022-05-10 14:50:49 +0200
commitb65f5649c853badcf8dad6b7be06cafed0ed70c0 (patch)
tree872dbca2fd912f40e81f9ad3f6a1e19184db9885
parent43a79acdfafead3a9d23a1f5b690fda9cdf8995f (diff)
downloadaerc-b65f5649c853badcf8dad6b7be06cafed0ed70c0.zip
gpg: make tests more robust
Skip the tests if gpg is not installed. Avoid interference with the global ~/.gnupg. Automatically delete GNUPGHOME at the end of tests. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
-rw-r--r--Makefile4
-rw-r--r--lib/crypto/gpg/gpg_test.go29
-rw-r--r--lib/crypto/gpg/reader_test.go16
-rw-r--r--lib/crypto/gpg/writer_test.go6
4 files changed, 31 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index 77de2c8..52c0c55 100644
--- a/Makefile
+++ b/Makefile
@@ -58,6 +58,10 @@ checkfmt:
exit 1; \
fi
+.PHONY: tests
+tests:
+ $(GO) test $(GOFLAGS) -v ./...
+
.PHONY: debug
debug: aerc.debug
@echo 'Run `./aerc.debug` and use this command in another terminal to attach a debugger:'
diff --git a/lib/crypto/gpg/gpg_test.go b/lib/crypto/gpg/gpg_test.go
index 61c2ec6..d833830 100644
--- a/lib/crypto/gpg/gpg_test.go
+++ b/lib/crypto/gpg/gpg_test.go
@@ -2,9 +2,8 @@ package gpg
import (
"bytes"
- "fmt"
"io"
- "log"
+ "os"
"os/exec"
"strings"
"testing"
@@ -12,22 +11,20 @@ import (
"git.sr.ht/~rjarry/aerc/models"
)
-func TestHasGpg(t *testing.T) {
- gpgmail := new(Mail)
- hasGpg := gpgmail.Init(new(log.Logger))
-
- if hasGpg != nil {
- t.Errorf("System does not have GPG")
+func initGPGtest(t *testing.T) {
+ if _, err := exec.LookPath("gpg"); err != nil {
+ t.Skipf("%s", err)
}
-}
-
-func CleanUp() {
- cmd := exec.Command("gpg", "--batch", "--yes", "--delete-secret-and-public-keys", testKeyId)
- err := cmd.Run()
- if err != nil {
- fmt.Println("Test cleanup failed: you may need to delete the test keys from your GPG keyring")
- return
+ // temp dir is automatically deleted by the test runtime
+ dir := t.TempDir()
+ // t.Setenv is only available since go 1.17
+ if err := os.Setenv("GNUPGHOME", dir); err != nil {
+ t.Fatalf("failed to set GNUPGHOME: %s", err)
}
+ t.Cleanup(func() {
+ os.Unsetenv("GNUPGHOME")
+ })
+ t.Logf("using GNUPGHOME = %s", dir)
}
func toCRLF(s string) string {
diff --git a/lib/crypto/gpg/reader_test.go b/lib/crypto/gpg/reader_test.go
index 3cd7c4b..06cf7a3 100644
--- a/lib/crypto/gpg/reader_test.go
+++ b/lib/crypto/gpg/reader_test.go
@@ -21,6 +21,8 @@ func importPublicKey() {
}
func TestReader_encryptedSignedPGPMIME(t *testing.T) {
+ initGPGtest(t)
+
var expect = models.MessageDetails{
IsEncrypted: true,
IsSigned: true,
@@ -41,11 +43,11 @@ func TestReader_encryptedSignedPGPMIME(t *testing.T) {
}
deepEqual(t, r.MessageDetails, &expect)
-
- t.Cleanup(CleanUp)
}
func TestReader_signedPGPMIME(t *testing.T) {
+ initGPGtest(t)
+
var expect = models.MessageDetails{
IsEncrypted: false,
IsSigned: true,
@@ -67,11 +69,11 @@ func TestReader_signedPGPMIME(t *testing.T) {
}
deepEqual(t, r.MessageDetails, &expect)
-
- t.Cleanup(CleanUp)
}
func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) {
+ initGPGtest(t)
+
var expect = models.MessageDetails{
IsEncrypted: true,
IsSigned: true,
@@ -100,6 +102,8 @@ func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) {
}
}
func TestReader_signedPGPMIMEInvalid(t *testing.T) {
+ initGPGtest(t)
+
var expect = models.MessageDetails{
IsEncrypted: false,
IsSigned: true,
@@ -120,11 +124,11 @@ func TestReader_signedPGPMIMEInvalid(t *testing.T) {
t.Fatalf("pgpmail.Read() = %v", err)
}
deepEqual(t, r.MessageDetails, &expect)
-
- t.Cleanup(CleanUp)
}
func TestReader_plaintext(t *testing.T) {
+ initGPGtest(t)
+
sr := strings.NewReader(testPlaintext)
r, err := Read(sr)
if err != nil {
diff --git a/lib/crypto/gpg/writer_test.go b/lib/crypto/gpg/writer_test.go
index 0f9ab10..e8defc1 100644
--- a/lib/crypto/gpg/writer_test.go
+++ b/lib/crypto/gpg/writer_test.go
@@ -16,6 +16,8 @@ func init() {
}
func TestEncrypt(t *testing.T) {
+ initGPGtest(t)
+
importPublicKey()
importSecretKey()
var h textproto.Header
@@ -55,11 +57,11 @@ func TestEncrypt(t *testing.T) {
if s := body.String(); s != wantEncrypted {
t.Errorf("Encrypt() = \n%v\n but want \n%v", s, wantEncrypted)
}
-
- t.Cleanup(CleanUp)
}
func TestSign(t *testing.T) {
+ initGPGtest(t)
+
importPublicKey()
importSecretKey()
var h textproto.Header