gitea-sonarqube-bot/internal/settings/settings_test.go
justusbunsi 5c6229be8f
Allow custom config path via environment variable
Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
2021-06-20 15:39:54 +02:00

45 lines
863 B
Go

package settings
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
var defaultConfig []byte = []byte(
`gitea:
url: https://example.com/gitea
token: 1337
webhookSecret: {}
repositories: []
sonarqube:
url: https://example.com/sonarqube
token: 42
webhookSecret: {}
projects: []
`)
func WriteConfigFile(t *testing.T, content []byte) {
dir := os.TempDir()
config := path.Join(dir, "config.yaml")
t.Cleanup(func() {
os.Remove(config)
})
_ = ioutil.WriteFile(config, content,0444)
}
func TestLoadWithMissingFile(t *testing.T) {
assert.Panics(t, func() { Load(os.TempDir()) }, "No panic while reading missing file")
}
func TestLoadWithExistingFile(t *testing.T) {
WriteConfigFile(t, defaultConfig)
assert.NotPanics(t, func() { Load(os.TempDir()) }, "Unexpected panic while reading existing file")
}