shiori/internal/config/config_test.go
Monirzadeh 82aa1e5080
fix: fixes path issues on windows (#829)
* return corrent path in windows with usiing path lib

* ci: run tests in windows and macos too

* avoid testing mysql/psql in windows and macos

* windows & macos matrix

* disable gotestfmt

* replace which in shell script

* handle error in test db removal

* fix expected path baseed on platform

* add leading seprator

* proper temporary storage dir and db cleanup

* fix failed to create destination dir file does not exist in windows

* move temp to /tmp

* update temp folder

* fix config tests in windows

* apply patch for db

* revert temp dir creation.

* unify account db tests pach

* remove TmpDir for sqlite tests

* try to force CGO disable with enviroment variable

* Remove unneeded log

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* remove unneeded comment

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* fix file path for download images

* change way to create temp directory

* use diffrent file name for each test

* fix typo

* fix absolute path in successful download image

* correct filename with png

* change test to download image from internet instead of local machine

* remvoe unneeded import

* remove os.RemoveAll(.env)

* unify variable names in unit test

* return CGO_ENABLED=0

* test other way to set enviroment variable

* try to set enviroment variable sepratly in macos and windows

* set enviroment variable before run commands in windows

* fix windows test name

* combine two workflow for windows and macos again

* fix typo

* remove env

* change env path

* cleanup unneeded env

* general CGO_ENABLED environ

* use absolute path to run fileserver instead of relative

* serve file test from internet shiori repository

* check file existance after download and unify varibale name from temp to tmpDir

* remove unneeded log

---------

Co-authored-by: Felipe M <me@fmartingr.com>
Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>
2024-02-05 10:21:23 +01:00

104 lines
2.7 KiB
Go

package config
import (
"context"
"os"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestHostnameVariable(t *testing.T) {
os.Setenv("HOSTNAME", "test_hostname")
defer os.Unsetenv("HOSTNAME")
log := logrus.New()
cfg := ParseServerConfiguration(context.TODO(), log)
require.Equal(t, "test_hostname", cfg.Hostname)
}
// TestBackwardsCompatibility tests that the old environment variables changed from 1.5.5 onwards
// are still supported and working with the new configuration system.
func TestBackwardsCompatibility(t *testing.T) {
for _, env := range []struct {
env string
want string
eval func(t *testing.T, cfg *Config)
}{
{"HOSTNAME", "test_hostname", func(t *testing.T, cfg *Config) {
require.Equal(t, "test_hostname", cfg.Hostname)
}},
{"SHIORI_DIR", "test", func(t *testing.T, cfg *Config) {
require.Equal(t, "test", cfg.Storage.DataDir)
}},
{"SHIORI_DBMS", "test", func(t *testing.T, cfg *Config) {
require.Equal(t, "test", cfg.Database.DBMS)
}},
} {
t.Run(env.env, func(t *testing.T) {
os.Setenv(env.env, env.want)
t.Cleanup(func() {
os.Unsetenv(env.env)
})
log := logrus.New()
cfg := ParseServerConfiguration(context.Background(), log)
env.eval(t, cfg)
})
}
}
func TestReadDotEnv(t *testing.T) {
log := logrus.New()
for _, testCase := range []struct {
name string
line string
env map[string]string
}{
{"empty", "", map[string]string{}},
{"comment", "# comment", map[string]string{}},
{"ignore invalid lines", "invalid line", map[string]string{}},
{"single variable", "SHIORI_HTTP_PORT=9999", map[string]string{"SHIORI_HTTP_PORT": "9999"}},
{"multiple variable", "SHIORI_HTTP_PORT=9999\nSHIORI_HTTP_SECRET_KEY=123123", map[string]string{"SHIORI_HTTP_PORT": "9999", "SHIORI_HTTP_SECRET_KEY": "123123"}},
} {
t.Run(testCase.name, func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "")
require.NoError(t, err)
require.NoError(t, os.Chdir(tmpDir))
// Write the .env file in the temporary directory
handler, err := os.OpenFile(".env", os.O_CREATE|os.O_WRONLY, 0655)
require.NoError(t, err)
handler.Write([]byte(testCase.line + "\n"))
handler.Close()
e := readDotEnv(log)
require.Equal(t, testCase.env, e)
})
}
t.Run("no file", func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "")
require.NoError(t, err)
require.NoError(t, os.Chdir(tmpDir))
e := readDotEnv(log)
require.Equal(t, map[string]string{}, e)
})
}
func TestConfigSetDefaults(t *testing.T) {
log := logrus.New()
cfg := ParseServerConfiguration(context.TODO(), log)
cfg.SetDefaults(log, false)
require.NotEmpty(t, cfg.Http.SecretKey)
require.NotEmpty(t, cfg.Storage.DataDir)
require.NotEmpty(t, cfg.Database.URL)
}