Add Lua configuration support (#2845)

* lua opt trial

* go mod tidy
This commit is contained in:
Jo authored and GitHub committed 2026-06-09 00:31:26 +02:00
1 parent 4c5fda7944
commit ecae3e601e
13 files changed
+525 -53

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
package lua
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testConfig struct {
BuildDir string `json:"buildDir" lua:"build_dir"`
RequestSplitN int `json:"requestsplitn" lua:"request_split_n"`
Devel bool `json:"devel" lua:"devel"`
AnswerClean string `json:"answerclean"`
Ignored string `json:"-" lua:"-"`
}
func TestApply(t *testing.T) {
e := New()
defer e.Close()
require.NoError(t, e.L.DoString(`
yay.opt.build_dir = "/tmp/yay"
yay.opt.request_split_n = 200
yay.opt.devel = true
`))
cfg := &testConfig{}
unknown, errs := e.Apply(cfg)
assert.Empty(t, unknown)
assert.Empty(t, errs)
assert.Equal(t, "/tmp/yay", cfg.BuildDir)
assert.Equal(t, 200, cfg.RequestSplitN)
assert.True(t, cfg.Devel)
}
func TestApplyUnknownAndTypeMismatch(t *testing.T) {
e := New()
defer e.Close()
require.NoError(t, e.L.DoString(`
yay.opt.does_not_exist = "x"
yay.opt.buildDir = "/tmp/nope"
yay.opt.requestsplitn = 200
yay.opt.Devel = true
yay.opt.devel = "not a bool"
yay.opt.build_dir = "/tmp/ok"
`))
cfg := &testConfig{}
unknown, errs := e.Apply(cfg)
assert.ElementsMatch(t, []string{"does_not_exist", "buildDir", "requestsplitn", "Devel"}, unknown)
assert.Len(t, errs, 1)
assert.Zero(t, cfg.RequestSplitN)
assert.False(t, cfg.Devel)
assert.Equal(t, "/tmp/ok", cfg.BuildDir)
}
func TestApplyIgnoresAnswerOptionsWithoutLuaTags(t *testing.T) {
e := New()
defer e.Close()
require.NoError(t, e.L.DoString(`
yay.opt.answer_clean = "All"
`))
cfg := &testConfig{}
unknown, errs := e.Apply(cfg)
assert.Equal(t, []string{"answer_clean"}, unknown)
assert.Empty(t, errs)
assert.Empty(t, cfg.AnswerClean)
}
func TestApplyRejectsNonPointer(t *testing.T) {
e := New()
defer e.Close()
_, errs := e.Apply(testConfig{})
assert.Len(t, errs, 1)
}