papero/config/data_test.go

84 lines
2.1 KiB
Go

package config
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestShellScriptRunInlineScript(t *testing.T) {
s := ShellScript{Executable: "echo -n hello world!"}
doTestShellScriptRun(t, &s)
}
func TestShellScriptRunExecutable(t *testing.T) {
file := writeToTempFile(t, "papero.*.sh", "#!/usr/bin/env sh\necho -n hello world!\n")
s := ShellScript{Executable: file.Name()}
doTestShellScriptRun(t, &s)
}
func TestShellScriptRunExecutableWithArgs(t *testing.T) {
file := writeToTempFile(t, "papero.*.sh", "#!/usr/bin/env sh\necho -n ${@}\n")
s := ShellScript{Executable: fmt.Sprintf("%s %s %s", file.Name(), "hello", "world!")}
doTestShellScriptRun(t, &s)
}
func TestParseFile(t *testing.T) {
expected := &fileConfig{
Default: "First Account",
MailboxPath: "../base/mailbox",
DefaultMessages: initMaybeInt(50),
Accounts: []account{
{
Name: "First Account",
MailboxPath: "/other/path",
ExcludedFolders: []string{"Draft", "Junk"},
Messages: initMaybeInt(30),
ConnectionInfo: &connectionInfo{
Host: "mx.example.com",
Port: 993,
Username: "first@example.com",
Password: "",
PasswordFile: "",
PasswordExec: &ShellScript{Executable: "echo -n 123qweasdzxc"},
},
},
{
Name: "Other Account",
MailboxPath: "",
ExcludedFolders: []string{},
Messages: maybeInt{},
ConnectionInfo: &connectionInfo{
Host: "mail.personal.me",
Port: 666,
Username: "h4x0R@personal.me",
Password: "mySup3r5ekre7p4ssw0rd123",
PasswordFile: "",
PasswordExec: nil,
},
},
},
}
file := writeToTempFile(t, "papero.*.toml", testConfig)
result, err := parseFile(file.Name())
if err != nil {
t.Fatal(err)
}
opt := cmp.Comparer(func(a, b maybeInt) bool {
if a.empty() {
if b.empty() {
return true
}
return false
}
return a.value() == b.value()
})
if cmp.Equal(result, expected, opt) {
t.Errorf("Result and expected result differ: %+v\n", cmp.Diff(*result, *expected))
}
}