data_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package config
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. )
  7. func TestShellScriptRunInlineScript(t *testing.T) {
  8. s := ShellScript{Executable: "echo -n hello world!"}
  9. doTestShellScriptRun(t, &s)
  10. }
  11. func TestShellScriptRunExecutable(t *testing.T) {
  12. file := writeToTempFile(t, "papero.*.sh", "#!/usr/bin/env sh\necho -n hello world!\n")
  13. s := ShellScript{Executable: file.Name()}
  14. doTestShellScriptRun(t, &s)
  15. }
  16. func TestShellScriptRunExecutableWithArgs(t *testing.T) {
  17. file := writeToTempFile(t, "papero.*.sh", "#!/usr/bin/env sh\necho -n ${@}\n")
  18. s := ShellScript{Executable: fmt.Sprintf("%s %s %s", file.Name(), "hello", "world!")}
  19. doTestShellScriptRun(t, &s)
  20. }
  21. func TestParseFile(t *testing.T) {
  22. expected := &fileConfig{
  23. Default: "First Account",
  24. MailboxPath: "../base/mailbox",
  25. DefaultMessages: initMaybeInt(50),
  26. Accounts: []account{
  27. {
  28. Name: "First Account",
  29. MailboxPath: "/other/path",
  30. ExcludedFolders: []string{"Draft", "Junk"},
  31. Messages: initMaybeInt(30),
  32. ConnectionInfo: &connectionInfo{
  33. Host: "mx.example.com",
  34. Port: 993,
  35. Username: "first@example.com",
  36. Password: "",
  37. PasswordFile: "",
  38. PasswordExec: &ShellScript{Executable: "echo -n 123qweasdzxc"},
  39. },
  40. },
  41. {
  42. Name: "Other Account",
  43. MailboxPath: "",
  44. ExcludedFolders: []string{},
  45. Messages: maybeInt{},
  46. ConnectionInfo: &connectionInfo{
  47. Host: "mail.personal.me",
  48. Port: 666,
  49. Username: "h4x0R@personal.me",
  50. Password: "mySup3r5ekre7p4ssw0rd123",
  51. PasswordFile: "",
  52. PasswordExec: nil,
  53. },
  54. },
  55. },
  56. }
  57. file := writeToTempFile(t, "papero.*.toml", testConfig)
  58. result, err := parseFile(file.Name())
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. opt := cmp.Comparer(func(a, b maybeInt) bool {
  63. if a.empty() {
  64. if b.empty() {
  65. return true
  66. }
  67. return false
  68. }
  69. return a.value() == b.value()
  70. })
  71. if cmp.Equal(result, expected, opt) {
  72. t.Errorf("Result and expected result differ: %+v\n", cmp.Diff(*result, *expected))
  73. }
  74. }