Fix bug migrating config, add test

This commit is contained in:
Justin Hawkins 2022-04-09 12:26:56 +09:30
parent 3bbc715e74
commit 50a6ac9e85
4 changed files with 64 additions and 3 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
## [Unreleased] ## [Unreleased]
## [v0.5.5] - 2022-04-09
- Fix a bug which would erase configuration when migrating from v1 to v2 config
## [v0.5.4] - 2022-04-07 ## [v0.5.4] - 2022-04-07
- Check the chosen command exists when configuring a profile - Check the chosen command exists when configuring a profile

View File

@ -224,6 +224,8 @@ func (cs *ConfigService) LoadConfig() error {
return fmt.Errorf("Could not read config '%s': %v", path, err) return fmt.Errorf("Could not read config '%s': %v", path, err)
} }
c := Config{} c := Config{}
cs.Config = &c
err = yaml.Unmarshal(b, &c) err = yaml.Unmarshal(b, &c)
if err != nil { if err != nil {
return fmt.Errorf("Could not parse YAML config '%s': %v", path, err) return fmt.Errorf("Could not parse YAML config '%s': %v", path, err)
@ -243,8 +245,6 @@ func (cs *ConfigService) LoadConfig() error {
cs.WriteConfig() cs.WriteConfig()
} }
cs.Config = &c
return nil return nil
} }

57
config/config_test.go Normal file
View File

@ -0,0 +1,57 @@
package config
import (
"os"
"testing"
)
func TestMigrationV1toV2(t *testing.T) {
v2Config := `config_version: 1
server:
port: 6123
address: http://localhost:6123
download_path: ./
ui:
popup_width: 500
popup_height: 500
profiles:
- name: standard video
command: youtube-dl
args:
- --newline
- --write-info-json
- -f
- bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best
- name: standard mp3
command: youtube-dl
args:
- --newline
- --write-info-json
- --extract-audio
- --audio-format
- mp3
`
cs := configServiceFromString(v2Config)
err := cs.LoadConfig()
if err != nil {
t.Errorf("got error when loading config: %s", err)
}
if cs.Config.ConfigVersion != 2 {
t.Errorf("did not migrate version (it is '%d')", cs.Config.ConfigVersion)
}
if cs.Config.Server.MaximumActiveDownloads != 2 {
t.Error("did not add MaximumActiveDownloads")
}
t.Log(cs.ConfigPath)
}
func configServiceFromString(configString string) *ConfigService {
tmpFile, _ := os.CreateTemp("", "gropple_test_*.yml")
tmpFile.Write([]byte(configString))
tmpFile.Close()
cs := ConfigService{
Config: &Config{},
ConfigPath: tmpFile.Name(),
}
return &cs
}

View File

@ -23,7 +23,7 @@ var downloads download.Downloads
var downloadId = 0 var downloadId = 0
var configService *config.ConfigService var configService *config.ConfigService
var versionInfo = version.Info{CurrentVersion: "v0.5.4"} var versionInfo = version.Info{CurrentVersion: "v0.5.5"}
//go:embed web //go:embed web
var webFS embed.FS var webFS embed.FS