parser_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package config // import "miniflux.app/v2/internal/config"
  4. import (
  5. "reflect"
  6. "testing"
  7. )
  8. func TestParseBoolValue(t *testing.T) {
  9. scenarios := map[string]bool{
  10. "": true,
  11. "1": true,
  12. "Yes": true,
  13. "yes": true,
  14. "True": true,
  15. "true": true,
  16. "on": true,
  17. "false": false,
  18. "off": false,
  19. "invalid": false,
  20. }
  21. for input, expected := range scenarios {
  22. result := parseBool(input, true)
  23. if result != expected {
  24. t.Errorf(`Unexpected result for %q, got %v instead of %v`, input, result, expected)
  25. }
  26. }
  27. }
  28. func TestParseStringValueWithUnsetVariable(t *testing.T) {
  29. if parseString("", "defaultValue") != "defaultValue" {
  30. t.Errorf(`Unset variables should returns the default value`)
  31. }
  32. }
  33. func TestParseStringValue(t *testing.T) {
  34. if parseString("test", "defaultValue") != "test" {
  35. t.Errorf(`Defined variables should returns the specified value`)
  36. }
  37. }
  38. func TestParseIntValueWithUnsetVariable(t *testing.T) {
  39. if parseInt("", 42) != 42 {
  40. t.Errorf(`Unset variables should returns the default value`)
  41. }
  42. }
  43. func TestParseIntValueWithInvalidInput(t *testing.T) {
  44. if parseInt("invalid integer", 42) != 42 {
  45. t.Errorf(`Invalid integer should returns the default value`)
  46. }
  47. }
  48. func TestParseIntValue(t *testing.T) {
  49. if parseInt("2018", 42) != 2018 {
  50. t.Errorf(`Defined variables should returns the specified value`)
  51. }
  52. }
  53. func TestParseListenAddr(t *testing.T) {
  54. defaultExpected := []string{defaultListenAddr}
  55. tests := []struct {
  56. name string
  57. listenAddr string
  58. port string
  59. expected []string
  60. lines []string // Used for direct lines parsing instead of individual env vars
  61. isLineOriented bool // Flag to indicate if we use lines
  62. }{
  63. {
  64. name: "Single LISTEN_ADDR",
  65. listenAddr: "127.0.0.1:8080",
  66. expected: []string{"127.0.0.1:8080"},
  67. },
  68. {
  69. name: "Multiple LISTEN_ADDR comma-separated",
  70. listenAddr: "127.0.0.1:8080,:8081,/tmp/miniflux.sock",
  71. expected: []string{"127.0.0.1:8080", ":8081", "/tmp/miniflux.sock"},
  72. },
  73. {
  74. name: "Multiple LISTEN_ADDR with spaces around commas",
  75. listenAddr: "127.0.0.1:8080 , :8081",
  76. expected: []string{"127.0.0.1:8080", ":8081"},
  77. },
  78. {
  79. name: "Empty LISTEN_ADDR",
  80. listenAddr: "",
  81. expected: defaultExpected,
  82. },
  83. {
  84. name: "PORT overrides LISTEN_ADDR",
  85. listenAddr: "127.0.0.1:8000",
  86. port: "8082",
  87. expected: []string{":8082"},
  88. },
  89. {
  90. name: "PORT overrides empty LISTEN_ADDR",
  91. listenAddr: "",
  92. port: "8083",
  93. expected: []string{":8083"},
  94. },
  95. {
  96. name: "LISTEN_ADDR with empty segment (comma)",
  97. listenAddr: "127.0.0.1:8080,,:8081",
  98. expected: []string{"127.0.0.1:8080", ":8081"},
  99. },
  100. {
  101. name: "PORT override with lines parsing",
  102. isLineOriented: true,
  103. lines: []string{"LISTEN_ADDR=127.0.0.1:8000", "PORT=8082"},
  104. expected: []string{":8082"},
  105. },
  106. {
  107. name: "LISTEN_ADDR only with lines parsing (comma)",
  108. isLineOriented: true,
  109. lines: []string{"LISTEN_ADDR=10.0.0.1:9090,10.0.0.2:9091"},
  110. expected: []string{"10.0.0.1:9090", "10.0.0.2:9091"},
  111. },
  112. {
  113. name: "Empty LISTEN_ADDR with lines parsing (default)",
  114. isLineOriented: true,
  115. lines: []string{"LISTEN_ADDR="},
  116. expected: defaultExpected,
  117. },
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.name, func(t *testing.T) {
  121. parser := NewParser()
  122. var err error
  123. if tt.isLineOriented {
  124. err = parser.parseLines(tt.lines)
  125. } else {
  126. // Simulate os.Environ() behaviour for individual var testing
  127. var envLines []string
  128. if tt.listenAddr != "" {
  129. envLines = append(envLines, "LISTEN_ADDR="+tt.listenAddr)
  130. }
  131. if tt.port != "" {
  132. envLines = append(envLines, "PORT="+tt.port)
  133. }
  134. // Add a dummy var if both are empty to avoid empty lines slice if not intended
  135. if tt.listenAddr == "" && tt.port == "" && tt.name == "Empty LISTEN_ADDR" {
  136. // This case specifically tests empty LISTEN_ADDR resulting in default
  137. // So, we pass LISTEN_ADDR=
  138. envLines = append(envLines, "LISTEN_ADDR=")
  139. }
  140. err = parser.parseLines(envLines)
  141. }
  142. if err != nil {
  143. t.Fatalf("parseLines() error = %v", err)
  144. }
  145. opts := parser.opts
  146. if !reflect.DeepEqual(opts.ListenAddr(), tt.expected) {
  147. t.Errorf("ListenAddr() got = %v, want %v", opts.ListenAddr(), tt.expected)
  148. }
  149. })
  150. }
  151. }