mirror of
https://github.com/bluenviron/mediamtx
synced 2025-03-11 06:47:58 +00:00
test: use t.Setenv
to set env vars in tests (#2722)
This commit replaces `os.Setenv` with `t.Setenv` in tests. The environment variable is automatically restored to its original value when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.Setenv Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
dee7176cb0
commit
f335fc67f9
@ -113,20 +113,16 @@ func TestConfFromFile(t *testing.T) {
|
||||
|
||||
func TestConfFromFileAndEnv(t *testing.T) {
|
||||
// global parameter
|
||||
os.Setenv("RTSP_PROTOCOLS", "tcp")
|
||||
defer os.Unsetenv("RTSP_PROTOCOLS")
|
||||
t.Setenv("RTSP_PROTOCOLS", "tcp")
|
||||
|
||||
// path parameter
|
||||
os.Setenv("MTX_PATHS_CAM1_SOURCE", "rtsp://testing")
|
||||
defer os.Unsetenv("MTX_PATHS_CAM1_SOURCE")
|
||||
t.Setenv("MTX_PATHS_CAM1_SOURCE", "rtsp://testing")
|
||||
|
||||
// deprecated global parameter
|
||||
os.Setenv("MTX_RTMPDISABLE", "yes")
|
||||
defer os.Unsetenv("MTX_RTMPDISABLE")
|
||||
t.Setenv("MTX_RTMPDISABLE", "yes")
|
||||
|
||||
// deprecated path parameter
|
||||
os.Setenv("MTX_PATHS_CAM2_DISABLEPUBLISHEROVERRIDE", "yes")
|
||||
defer os.Unsetenv("MTX_PATHS_CAM2_DISABLEPUBLISHEROVERRIDE")
|
||||
t.Setenv("MTX_PATHS_CAM2_DISABLEPUBLISHEROVERRIDE", "yes")
|
||||
|
||||
tmpf, err := writeTempFile([]byte("{}"))
|
||||
require.NoError(t, err)
|
||||
@ -149,8 +145,7 @@ func TestConfFromFileAndEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConfFromEnvOnly(t *testing.T) {
|
||||
os.Setenv("MTX_PATHS_CAM1_SOURCE", "rtsp://testing")
|
||||
defer os.Unsetenv("MTX_PATHS_CAM1_SOURCE")
|
||||
t.Setenv("MTX_PATHS_CAM1_SOURCE", "rtsp://testing")
|
||||
|
||||
conf, confPath, err := Load("", nil)
|
||||
require.NoError(t, err)
|
||||
@ -179,8 +174,7 @@ func TestConfEncryption(t *testing.T) {
|
||||
return base64.StdEncoding.EncodeToString(encrypted)
|
||||
}()
|
||||
|
||||
os.Setenv("RTSP_CONFKEY", key)
|
||||
defer os.Unsetenv("RTSP_CONFKEY")
|
||||
t.Setenv("RTSP_CONFKEY", key)
|
||||
|
||||
tmpf, err := writeTempFile([]byte(encryptedConf))
|
||||
require.NoError(t, err)
|
||||
|
4
internal/conf/env/env_test.go
vendored
4
internal/conf/env/env_test.go
vendored
@ -2,7 +2,6 @@ package env
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -127,8 +126,7 @@ func TestLoad(t *testing.T) {
|
||||
}
|
||||
|
||||
for key, val := range env {
|
||||
os.Setenv(key, val)
|
||||
defer os.Unsetenv(key)
|
||||
t.Setenv(key, val)
|
||||
}
|
||||
|
||||
var s testStruct
|
||||
|
Loading…
Reference in New Issue
Block a user