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 <engzerjun@gmail.com> Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Co-authored-by: James Elliott <james-d-elliott@users.noreply.github.com>pull/4470/head
parent
ee50e5580c
commit
59c11581b8
|
@ -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")
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue