From 59c11581b840aa92a4de6a3014e756b875c719ce Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sun, 4 Dec 2022 17:57:28 +0800 Subject: [PATCH] test: use testing tempdir tooling (#4468) This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun Signed-off-by: Eng Zer Jun Co-authored-by: James Elliott --- .../configuration/koanf_callbacks_test.go | 7 ++--- internal/configuration/provider_test.go | 20 ++++++------- internal/configuration/template_test.go | 9 ++---- internal/logging/logger_test.go | 28 ++++--------------- internal/utils/hashing_test.go | 5 ++-- 5 files changed, 21 insertions(+), 48 deletions(-) diff --git a/internal/configuration/koanf_callbacks_test.go b/internal/configuration/koanf_callbacks_test.go index 43461fe28..561038efd 100644 --- a/internal/configuration/koanf_callbacks_test.go +++ b/internal/configuration/koanf_callbacks_test.go @@ -2,7 +2,6 @@ package configuration import ( "fmt" - "os" "path/filepath" "runtime" "testing" @@ -58,8 +57,7 @@ func TestKoanfSecretCallbackWithValidSecrets(t *testing.T) { "AUTHELIA__STORAGE_MYSQL_FAKE_PASSWORD": "storage.mysql.fake_password", } - dir, err := os.MkdirTemp("", "authelia-test-callbacks") - assert.NoError(t, err) + dir := t.TempDir() secretOne := filepath.Join(dir, "secert_one") secretTwo := filepath.Join(dir, "secret_two") @@ -108,8 +106,7 @@ func TestKoanfSecretCallbackShouldErrorOnFSError(t *testing.T) { "AUTHELIA_THEME": "theme", } - dir, err := os.MkdirTemp("", "authelia-test-callbacks") - assert.NoError(t, err) + dir := t.TempDir() secret := filepath.Join(dir, "inaccessible") diff --git a/internal/configuration/provider_test.go b/internal/configuration/provider_test.go index b5eb50ecc..147f71eff 100644 --- a/internal/configuration/provider_test.go +++ b/internal/configuration/provider_test.go @@ -19,8 +19,7 @@ import ( func TestShouldErrorSecretNotExist(t *testing.T) { testReset() - dir, err := os.MkdirTemp("", "authelia-test-secret-not-exist") - assert.NoError(t, err) + dir := t.TempDir() testSetEnv(t, "JWT_SECRET_FILE", filepath.Join(dir, "jwt")) testSetEnv(t, "DUO_API_SECRET_KEY_FILE", filepath.Join(dir, "duo")) @@ -36,7 +35,7 @@ func TestShouldErrorSecretNotExist(t *testing.T) { testSetEnv(t, "IDENTITY_PROVIDERS_OIDC_HMAC_SECRET_FILE", filepath.Join(dir, "oidc-hmac")) val := schema.NewStructValidator() - _, _, err = Load(val, NewEnvironmentSource(DefaultEnvPrefix, DefaultEnvDelimiter), NewSecretsSource(DefaultEnvPrefix, DefaultEnvDelimiter)) + _, _, err := Load(val, NewEnvironmentSource(DefaultEnvPrefix, DefaultEnvDelimiter), NewSecretsSource(DefaultEnvPrefix, DefaultEnvDelimiter)) assert.NoError(t, err) assert.Len(t, val.Warnings(), 0) @@ -162,15 +161,14 @@ func TestShouldRaiseIOErrOnUnreadableFile(t *testing.T) { testReset() - dir, err := os.MkdirTemp("", "authelia-conf") - assert.NoError(t, err) + dir := t.TempDir() assert.NoError(t, os.WriteFile(filepath.Join(dir, "myconf.yml"), []byte("server:\n port: 9091\n"), 0000)) cfg := filepath.Join(dir, "myconf.yml") val := schema.NewStructValidator() - _, _, err = Load(val, NewYAMLFileSource(cfg)) + _, _, err := Load(val, NewYAMLFileSource(cfg)) assert.NoError(t, err) require.Len(t, val.Errors(), 1) @@ -390,14 +388,13 @@ func TestShouldNotReadConfigurationOnFSAccessDenied(t *testing.T) { testReset() - dir, err := os.MkdirTemp("", "authelia-config") - assert.NoError(t, err) + dir := t.TempDir() cfg := filepath.Join(dir, "config.yml") assert.NoError(t, testCreateFile(filepath.Join(dir, "config.yml"), "port: 9091\n", 0000)) val := schema.NewStructValidator() - _, _, err = Load(val, NewYAMLFileSource(cfg)) + _, _, err := Load(val, NewYAMLFileSource(cfg)) assert.NoError(t, err) require.Len(t, val.Errors(), 1) @@ -408,11 +405,10 @@ func TestShouldNotReadConfigurationOnFSAccessDenied(t *testing.T) { func TestShouldNotLoadDirectoryConfiguration(t *testing.T) { testReset() - dir, err := os.MkdirTemp("", "authelia-config") - assert.NoError(t, err) + dir := t.TempDir() val := schema.NewStructValidator() - _, _, err = Load(val, NewYAMLFileSource(dir)) + _, _, err := Load(val, NewYAMLFileSource(dir)) assert.NoError(t, err) require.Len(t, val.Errors(), 1) diff --git a/internal/configuration/template_test.go b/internal/configuration/template_test.go index e4a02a5ee..3be14868f 100644 --- a/internal/configuration/template_test.go +++ b/internal/configuration/template_test.go @@ -13,8 +13,7 @@ import ( ) func TestShouldGenerateConfiguration(t *testing.T) { - dir, err := os.MkdirTemp("", "authelia-config") - assert.NoError(t, err) + dir := t.TempDir() cfg := filepath.Join(dir, "config.yml") @@ -31,8 +30,7 @@ func TestShouldNotGenerateConfigurationOnFSAccessDenied(t *testing.T) { t.Skip("skipping test due to being on windows") } - dir, err := os.MkdirTemp("", "authelia-config") - assert.NoError(t, err) + dir := t.TempDir() assert.NoError(t, os.Mkdir(filepath.Join(dir, "zero"), 0000)) @@ -44,8 +42,7 @@ func TestShouldNotGenerateConfigurationOnFSAccessDenied(t *testing.T) { } func TestShouldNotGenerateConfiguration(t *testing.T) { - dir, err := os.MkdirTemp("", "authelia-config") - assert.NoError(t, err) + dir := t.TempDir() cfg := filepath.Join(dir, "..", "not-a-dir", "config.yml") diff --git a/internal/logging/logger_test.go b/internal/logging/logger_test.go index cd1baead7..0976b494e 100644 --- a/internal/logging/logger_test.go +++ b/internal/logging/logger_test.go @@ -3,7 +3,6 @@ package logging import ( "fmt" "io" - "log" "os" "runtime" "testing" @@ -16,15 +15,10 @@ import ( ) func TestShouldWriteLogsToFile(t *testing.T) { - dir, err := os.MkdirTemp("/tmp", "logs-dir") - if err != nil { - log.Fatal(err) - } - - defer os.RemoveAll(dir) + dir := t.TempDir() path := fmt.Sprintf("%s/authelia.log", dir) - err = InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: false}, false) + err := InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: false}, false) require.NoError(t, err) Logger().Info("This is a test") @@ -39,15 +33,10 @@ func TestShouldWriteLogsToFile(t *testing.T) { } func TestShouldWriteLogsToFileAndStdout(t *testing.T) { - dir, err := os.MkdirTemp("/tmp", "logs-dir") - if err != nil { - log.Fatal(err) - } - - defer os.RemoveAll(dir) + dir := t.TempDir() path := fmt.Sprintf("%s/authelia.log", dir) - err = InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: true}, false) + err := InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: true}, false) require.NoError(t, err) Logger().Info("This is a test") @@ -62,15 +51,10 @@ func TestShouldWriteLogsToFileAndStdout(t *testing.T) { } func TestShouldFormatLogsAsJSON(t *testing.T) { - dir, err := os.MkdirTemp("/tmp", "logs-dir") - if err != nil { - log.Fatal(err) - } - - defer os.RemoveAll(dir) + dir := t.TempDir() path := fmt.Sprintf("%s/authelia.log", dir) - err = InitializeLogger(schema.LogConfiguration{Format: "json", FilePath: path, KeepStdout: false}, false) + err := InitializeLogger(schema.LogConfiguration{Format: "json", FilePath: path, KeepStdout: false}, false) require.NoError(t, err) Logger().Info("This is a test") diff --git a/internal/utils/hashing_test.go b/internal/utils/hashing_test.go index 5a86c83be..cf016820b 100644 --- a/internal/utils/hashing_test.go +++ b/internal/utils/hashing_test.go @@ -30,10 +30,9 @@ func TestShouldHashString(t *testing.T) { } func TestShouldHashPath(t *testing.T) { - dir, err := os.MkdirTemp("", "authelia-hashing") - assert.NoError(t, err) + dir := t.TempDir() - err = os.WriteFile(filepath.Join(dir, "myfile"), []byte("output\n"), 0600) + err := os.WriteFile(filepath.Join(dir, "myfile"), []byte("output\n"), 0600) assert.NoError(t, err) err = os.WriteFile(filepath.Join(dir, "anotherfile"), []byte("another\n"), 0600)