2023-06-29 22:46:17 +01:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadConfig(t *testing.T) {
|
|
|
|
t.Run("should correctly read a configuration file", func(t *testing.T) {
|
2023-07-11 15:43:20 +01:00
|
|
|
f, err := os.CreateTemp("", "birthdaybot-")
|
2023-06-29 22:46:17 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
2023-07-10 20:08:05 +01:00
|
|
|
_, werr := io.WriteString(f, "---\nbirthdays:\n file: birthday.csv")
|
2023-06-30 08:51:34 +01:00
|
|
|
require.NoError(t, werr)
|
|
|
|
require.NoError(t, f.Close())
|
2023-06-29 22:46:17 +01:00
|
|
|
|
|
|
|
config, err := ReadConfig(f.Name())
|
|
|
|
require.NoError(t, err)
|
2023-07-10 20:08:05 +01:00
|
|
|
require.Equal(t, "birthday.csv", config.Birthdays.File)
|
2023-06-29 22:46:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should fail if the file doesn't exist", func(t *testing.T) {
|
2023-07-11 15:43:20 +01:00
|
|
|
f, err := os.CreateTemp("", "birthdaybot-")
|
2023-06-29 22:46:17 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
|
|
|
|
config, err := ReadConfig(f.Name())
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, config)
|
|
|
|
})
|
|
|
|
}
|