pcmt/config/config_test.go
leo 3a2f85f683
All checks were successful
continuous-integration/drone/push Build is passing
feat: add license headers (+spdx id)
2023-05-20 20:15:57 +02:00

120 lines
2.6 KiB
Go

// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: AGPL-3.0-only
package config
import (
"os"
"testing"
"git.dotya.ml/mirre-mt/pcmt/slogging"
)
func TestConfig(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skip("we're running in CI apparently, skipping these tests")
}
confPath := "./testdata/"
ts := []struct {
name string
fails bool
isPath bool
conf string
}{
{
// incorrect type of the `Host` field.
name: "1.dhall",
fails: true,
isPath: true,
conf: "1.dhall",
},
{
name: "2.dhall",
fails: false,
isPath: true,
conf: "2.dhall",
},
{
name: "3.dhall",
fails: false,
isPath: false,
conf: `
let ConfigSchema =
https://git.dotya.ml/mirre-mt/pcmt-config-schema/raw/tag/0.0.0/package.dhall
sha256:c82b0904c261d442e5765c2f47d76ad53c3c3ed16ace1b33416cedf98f2f5df0
? https://git.dotya.ml/mirre-mt/pcmt-config-schema/raw/tag/0.0.0/package.dhall
let Config = ConfigSchema.Schema
let c = Config::{ DevelMode = True }
in c
`,
},
{
// misses final `in`.
name: "4.dhall",
fails: true,
isPath: false,
conf: `
let ConfigSchema =
https://git.dotya.ml/mirre-mt/pcmt-config-schema/raw/tag/0.0.0/package.dhall
sha256:c82b0904c261d442e5765c2f47d76ad53c3c3ed16ace1b33416cedf98f2f5df0
? https://git.dotya.ml/mirre-mt/pcmt-config-schema/raw/tag/0.0.0/package.dhall
let Config = ConfigSchema.Schema
let c = Config::{ DevelMode = True }
`,
},
{
name: "5.dhall",
fails: false,
isPath: false,
conf: `
let ConfigSchema =
https://git.dotya.ml/mirre-mt/pcmt-config-schema/raw/tag/0.0.0/package.dhall
sha256:c82b0904c261d442e5765c2f47d76ad53c3c3ed16ace1b33416cedf98f2f5df0
in ConfigSchema.Schema::{ DevelMode = True, Port = 5555 }
`,
},
{
name: "6.dhall",
fails: false,
isPath: false,
conf: `
let ConfigSchema =
https://git.dotya.ml/mirre-mt/pcmt-config-schema/raw/tag/0.0.0/package.dhall
sha256:c82b0904c261d442e5765c2f47d76ad53c3c3ed16ace1b33416cedf98f2f5df0
in ConfigSchema.Schema.default // { DevelMode = True }
`,
},
}
_ = slogging.Init(false)
for _, tc := range ts {
t.Logf("running test case %s", tc.name)
var err error
shouldFail := tc.fails
if tc.isPath {
_, err = Load(confPath+tc.conf, tc.isPath)
} else {
_, err = Load(tc.conf, tc.isPath)
}
switch {
case err == nil && shouldFail:
t.Errorf("test case '%s' should have failed", tc.name)
case err != nil && !shouldFail:
t.Log(err)
t.Errorf("test case '%s' should not have failed", tc.name)
}
}
}