| 12 | ) |
| 13 | |
| 14 | func TestConfig(t *testing.T) { |
| 15 | var cfgBlob = ` |
| 16 | debug = true |
| 17 | [server] |
| 18 | addr = "0.0.0.0" |
| 19 | port = 5000 |
| 20 | |
| 21 | [files] |
| 22 | status_file = "/tmp/tunasync.json" |
| 23 | db_file = "/var/lib/tunasync/tunasync.db" |
| 24 | ` |
| 25 | |
| 26 | Convey("toml decoding should work", t, func() { |
| 27 | |
| 28 | var conf Config |
| 29 | _, err := toml.Decode(cfgBlob, &conf) |
| 30 | ShouldEqual(err, nil) |
| 31 | ShouldEqual(conf.Server.Addr, "0.0.0.0") |
| 32 | ShouldEqual(conf.Server.Port, 5000) |
| 33 | ShouldEqual(conf.Files.StatusFile, "/tmp/tunasync.json") |
| 34 | ShouldEqual(conf.Files.DBFile, "/var/lib/tunasync/tunasync.db") |
| 35 | }) |
| 36 | |
| 37 | Convey("load Config should work", t, func() { |
| 38 | Convey("create config file & cli context", func() { |
| 39 | tmpfile, err := os.CreateTemp("", "tunasync") |
| 40 | So(err, ShouldEqual, nil) |
| 41 | defer os.Remove(tmpfile.Name()) |
| 42 | |
| 43 | err = os.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644) |
| 44 | So(err, ShouldEqual, nil) |
| 45 | defer tmpfile.Close() |
| 46 | |
| 47 | app := cli.NewApp() |
| 48 | app.Flags = []cli.Flag{ |
| 49 | &cli.StringFlag{ |
| 50 | Name: "config", |
| 51 | Aliases: []string{"c"}, |
| 52 | }, |
| 53 | &cli.StringFlag{ |
| 54 | Name: "addr", |
| 55 | }, |
| 56 | &cli.IntFlag{ |
| 57 | Name: "port", |
| 58 | }, |
| 59 | &cli.StringFlag{ |
| 60 | Name: "cert", |
| 61 | }, |
| 62 | &cli.StringFlag{ |
| 63 | Name: "key", |
| 64 | }, |
| 65 | &cli.StringFlag{ |
| 66 | Name: "status-file", |
| 67 | }, |
| 68 | &cli.StringFlag{ |
| 69 | Name: "db-file", |
| 70 | }, |
| 71 | } |