/
cloudservices
/
common
Обзор
Документация
Войти
/
cloudservices
/
common
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
test/env_test.go
64 строки
2 KB
Ivan
init
26 июл 2026, 11:34
26 июл 2026, 11:34
46b4481
Код
Авторство
О чём код?
package common_test import ( "os" "path/filepath" "testing" common "gitverse.ru/cloudservices/common" ) func TestLoadDotEnv(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, ".env") content := "\n# comment\nPLAIN_VALUE=plain\nQUOTED_VALUE=\"quoted value\"\nSINGLE_QUOTED='single value'\nINVALID_LINE\nKEY_WITH_SPACES = value with spaces \n" if err := os.WriteFile(path, []byte(content), 0o600); err != nil { t.Fatal(err) } keys := []string{"PLAIN_VALUE", "QUOTED_VALUE", "SINGLE_QUOTED", "KEY_WITH_SPACES"} for _, key := range keys { os.Unsetenv(key) t.Cleanup(func() { os.Unsetenv(key) }) } if err := common.LoadDotEnv(path); err != nil { t.Fatalf("LoadDotEnv() error = %v", err) } tests := map[string]string{ "PLAIN_VALUE": "plain", "QUOTED_VALUE": "quoted value", "SINGLE_QUOTED": "single value", "KEY_WITH_SPACES": "value with spaces", } for key, want := range tests { if got := os.Getenv(key); got != want { t.Errorf("%s = %q, want %q", key, got, want) } } } func TestLoadDotEnvDoesNotOverrideExistingEnvironment(t *testing.T) { key := "COMMON_TEST_EXISTING" t.Setenv(key, "from-environment") path := filepath.Join(t.TempDir(), ".env") if err := os.WriteFile(path, []byte(key+"=from-file\n"), 0o600); err != nil { t.Fatal(err) } if err := common.LoadDotEnv(path); err != nil { t.Fatalf("LoadDotEnv() error = %v", err) } if got := os.Getenv(key); got != "from-environment" { t.Errorf("environment value = %q, want %q", got, "from-environment") } } func TestLoadDotEnvMissingFile(t *testing.T) { err := common.LoadDotEnv(filepath.Join(t.TempDir(), "missing.env")) if err == nil { t.Fatal("LoadDotEnv() error = nil, want an error") } }