2021-10-04 15:38:49 +10:30
package config
import (
"io/ioutil"
"os"
"testing"
)
func TestNoConfig ( t * testing . T ) {
2021-10-06 23:12:43 +10:30
c := ConfigService { }
2021-10-04 15:38:49 +10:30
2021-10-06 23:12:43 +10:30
c . ConfigFilename = emptyTempFile ( )
2021-10-16 16:18:02 +10:30
err := os . Remove ( c . ConfigFilename )
if err != nil {
t . Fatalf ( "could not remove file: %v" , err )
}
2021-10-06 23:12:43 +10:30
defer os . Remove ( c . ConfigFilename ) // because we are about to create it
2021-10-04 15:38:49 +10:30
2021-10-16 16:18:02 +10:30
err = c . LoadOrInit ( )
2021-10-04 15:38:49 +10:30
if err != nil {
t . Errorf ( "unexpected failure from load: %s" , err )
}
2022-05-01 11:55:20 +09:30
if c . Config . Version != 3 {
t . Error ( "not version 3 starting config" )
2021-10-04 15:38:49 +10:30
}
2021-10-06 23:12:43 +10:30
if fileSize ( c . ConfigFilename ) < 40 {
t . Errorf ( "File is too small %d bytes" , fileSize ( c . ConfigFilename ) )
2021-10-04 15:38:49 +10:30
}
}
func TestEmptyFileConfig ( t * testing . T ) {
2021-10-06 23:12:43 +10:30
c := ConfigService { }
2021-10-04 15:38:49 +10:30
2021-10-06 23:12:43 +10:30
c . ConfigFilename = emptyTempFile ( )
defer os . Remove ( c . ConfigFilename )
2021-10-04 15:38:49 +10:30
2021-10-06 23:12:43 +10:30
err := c . LoadOrInit ( )
2021-10-04 15:38:49 +10:30
if err == nil {
t . Error ( "unexpected success from LoadOrInit()" )
}
2021-10-06 23:12:43 +10:30
}
2022-05-01 11:55:20 +09:30
func TestMigrateFromV1toV3 ( t * testing . T ) {
2021-10-06 23:12:43 +10:30
c := ConfigService { }
c . ConfigFilename = v1Config ( )
err := c . LoadOrInit ( )
if err != nil {
t . Error ( "unexpected error from LoadOrInit()" )
}
2022-05-01 11:55:20 +09:30
if c . Config . Version != 3 {
t . Errorf ( "Version %d not 3" , c . Config . Version )
}
if c . Config . OpenBrowserOnStart != true {
t . Errorf ( "Open browser on start not true" )
2021-10-06 23:12:43 +10:30
}
if len ( c . Config . Watchers ) != 1 {
t . Error ( "wrong amount of watchers" )
}
if c . Config . Watchers [ 0 ] . Path != "/private/tmp" {
t . Error ( "Wrong path" )
}
if c . Config . WatchInterval != 69 {
t . Error ( "Wrong watch interval" )
}
if c . Config . Port != 9090 {
t . Error ( "Wrong port" )
}
}
func v1Config ( ) string {
f , err := ioutil . TempFile ( "" , "dautest-*" )
if err != nil {
panic ( err )
}
config := ` { "WebHookURL":"https://discord.com/api/webhooks/abc123","Path":"/private/tmp","Watch":69,"Username":"abcdedf","NoWatermark":true,"Exclude":"ab cd ef"} `
f . Write ( [ ] byte ( config ) )
defer f . Close ( )
return f . Name ( )
2021-10-04 15:38:49 +10:30
}
func emptyTempFile ( ) string {
f , err := ioutil . TempFile ( "" , "dautest-*" )
if err != nil {
panic ( err )
}
2021-10-16 16:18:02 +10:30
f . Close ( )
2021-10-04 15:38:49 +10:30
return f . Name ( )
}
func fileSize ( file string ) int {
fi , err := os . Stat ( file )
if err != nil {
panic ( err )
}
return int ( fi . Size ( ) )
}