nix-amer/internal/cnf/bash_test.go

221 lines
4.5 KiB
Bash
Raw Permalink Normal View History

package cnf
import (
"bytes"
"testing"
)
func TestBashGet(t *testing.T) {
tests := []struct {
raw string
key string
}{
{"key=value;\n", "key"},
{" \t key=value;\n", "key"},
{"key=value; #comment\n", "key"},
{"\t key=value; #comment\n", "key"},
{"key=value; # comment\n", "key"},
{"\t \tkey=value; # comment\n", "key"},
}
for i, test := range tests {
parser := new(bash)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// extract value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != "value" {
t.Errorf("[%d] expected 'value' got '%s'", i, value)
}
}
}
func TestBashSetPathExists(t *testing.T) {
tests := []struct {
raw string
key string
value string
}{
{"key=value;\n", "key", "newvalue"},
{" \t key=value;\n", "key", "newvalue"},
{"key=value; #comment\n", "key", "newvalue"},
{"\t key=value; #comment\n", "key", "newvalue"},
{"key=value; # comment\n", "key", "newvalue"},
{"\t \tkey=value; # comment\n", "key", "newvalue"},
}
for i, test := range tests {
parser := new(bash)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
}
}
}
func TestBashSetCreatePath(t *testing.T) {
tests := []struct {
raw string
key string
ignore string // path to field that must be present after transformation
value string
}{
{"ignore=xxx;\n", "key", "ignore", "newvalue"},
{"unknown-line\nignore=xxx;\n", "key", "ignore", "newvalue"},
}
for i, test := range tests {
parser := new(bash)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
continue
}
// check that ignore field is still there
value, found = parser.Get(test.ignore)
if !found {
t.Errorf("[%d] expected ignore field, got none", i)
continue
}
// check value
if value != "xxx" {
t.Errorf("[%d] expected ignore value to be '%s' got '%s'", i, "xxx", value)
continue
}
}
}
func TestBashSetCreateEncode(t *testing.T) {
tests := []struct {
raw string
key string
value string
encoded string
}{
{"ignore=xxx;\n", "key", `"newvalue"`, "ignore=xxx;\nkey=\"newvalue\";\n"},
{"#!/bin/bash\n\nfunc(){\n\techo \"something\";\n}\nignore=xxx;\n", "key", `"newvalue"`, "#!/bin/bash\nfunc(){\n\techo \"something\";\n}\nignore=xxx;\nkey=\"newvalue\";\n"},
}
for i, test := range tests {
parser := new(bash)
r, w := bytes.NewBufferString(test.raw), new(bytes.Buffer)
// try to extract value
_, err := parser.ReadFrom(r)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
continue
}
// writeToBuffer
_, err = parser.WriteTo(w)
if err != nil {
t.Errorf("[%d] unexpected write error <%s>", i, err)
continue
}
encoded := w.String()
// check value
if encoded != test.encoded {
t.Errorf("[%d] wrong encoded value \n-=-=-= HAVE =-=-=-\n%s\n-=-=-= WANT =-=-=-\n%s\n-=-=-=-=-=\n", i, escape(test.encoded), escape(encoded))
continue
}
// try to write
}
}