Files
samba-configer/parser_test.go
2026-03-19 15:00:10 +00:00

97 lines
2.0 KiB
Go

package main
import (
"strings"
"testing"
)
func TestParseConfigFindsShares(t *testing.T) {
doc, err := ParseConfig(`
# comment
[global]
workgroup = WORKGROUP
[media]
path = /srv/media
valid users = alice, bob
guest ok = no
`)
if err != nil {
t.Fatalf("ParseConfig() error = %v", err)
}
shares := doc.ShareSections()
if len(shares) != 1 {
t.Fatalf("expected 1 share, got %d", len(shares))
}
cfg := ShareFromSection(shares[0])
if cfg.Name != "media" {
t.Fatalf("expected share name media, got %q", cfg.Name)
}
if cfg.Path != "/srv/media" {
t.Fatalf("expected path /srv/media, got %q", cfg.Path)
}
if strings.Join(cfg.ValidUsers, ",") != "alice,bob" {
t.Fatalf("expected users alice,bob, got %v", cfg.ValidUsers)
}
}
func TestBuildShareSectionUpdatesValues(t *testing.T) {
section := &Section{
Name: "public",
Lines: []Line{
{Key: "path", Value: "/old", IsKV: true},
{Key: "comment", Value: "old", IsKV: true},
},
}
updated := BuildShareSection(section, ShareConfig{
Name: "public",
Path: "/srv/public",
Comment: "",
Browseable: "yes",
ReadOnly: "no",
GuestOK: "yes",
ValidUsers: []string{"alice"},
})
cfg := ShareFromSection(updated)
if cfg.Path != "/srv/public" {
t.Fatalf("expected updated path, got %q", cfg.Path)
}
if cfg.Comment != "" {
t.Fatalf("expected comment removed, got %q", cfg.Comment)
}
if strings.Join(cfg.ValidUsers, ",") != "alice" {
t.Fatalf("expected alice, got %v", cfg.ValidUsers)
}
}
func TestSerializeProducesSectionHeaders(t *testing.T) {
doc := &Document{
Preamble: []string{"# smb.conf"},
Sections: []*Section{
BuildShareSection(nil, ShareConfig{
Name: "docs",
Path: "/srv/docs",
Browseable: "yes",
ReadOnly: "no",
GuestOK: "no",
}),
},
}
out := doc.Serialize()
for _, want := range []string{
"# smb.conf",
"[docs]",
"path = /srv/docs",
"browseable = yes",
} {
if !strings.Contains(out, want) {
t.Fatalf("serialized output missing %q:\n%s", want, out)
}
}
}