Debian Codegraph @

pkg/services/standard/standard_test.go

raw
1 package standard
2
3 import (
4 "errors"
5 "fmt"
6 "log"
7 "os"
8 "strings"
9 "testing"
10
11 "github.com/onsi/ginkgo/v2"
12 "github.com/onsi/gomega"
13
14 "github.com/nicholas-fedor/shoutrrr/internal/failures"
15 "github.com/nicholas-fedor/shoutrrr/pkg/types"
16 )
17
18 var (
19 logger *Logger
20 builder *strings.Builder
21 stringLogger *log.Logger
22 )
23
24 var _ = ginkgo.Describe("the standard logging implementation", func() {
25 ginkgo.When("setlogger is called with nil", func() {
26 ginkgo.It("should provide the logging API without any errors", func() {
27 logger = &Logger{}
28 logger.SetLogger(nil)
29 logger.Log("discarded log message")
30
31 gomega.Expect(logger.logger).ToNot(gomega.BeNil())
32 })
33 })
34 ginkgo.When("setlogger is called with a proper logger", func() {
35 ginkgo.BeforeEach(func() {
36 logger = &Logger{}
37 builder = &strings.Builder{}
38 stringLogger = log.New(builder, "", 0)
39 })
40 ginkgo.When("when logger.Log is called", func() {
41 ginkgo.It("should log messages", func() {
42 logger.SetLogger(stringLogger)
43 logger.Log("foo")
44 logger.Log("bar")
45
46 gomega.Expect(builder.String()).To(gomega.Equal("foo\nbar\n"))
47 })
48 })
49 ginkgo.When("when logger.Logf is called", func() {
50 ginkgo.It("should log messages", func() {
51 logger.SetLogger(stringLogger)
52 logger.Logf("foo %d", 7)
53
54 gomega.Expect(builder.String()).To(gomega.Equal("foo 7\n"))
55 })
56 })
57 })
58 })
59
60 var _ = ginkgo.Describe("the standard template implementation", func() {
61 ginkgo.When("a template is being set from a file", func() {
62 ginkgo.It("should load the template without any errors", func() {
63 file, err := os.CreateTemp("", "")
64 if err != nil {
65 ginkgo.Skip(fmt.Sprintf("Could not create temp file: %s", err))
66
67 return
68 }
69
70 fileName := file.Name()
71
72 defer func() { _ = file.Close() }()
73 defer func() { _ = os.Remove(fileName) }()
74
75 _, err = file.WriteString("template content")
76 if err != nil {
77 ginkgo.Skip(fmt.Sprintf("Could not write to temp file: %s", err))
78
79 return
80 }
81
82 templater := &Templater{}
83 err = templater.SetTemplateFile("foo", fileName)
84 gomega.Expect(err).ToNot(gomega.HaveOccurred())
85 })
86 })
87 ginkgo.When("a template is being set from a file that does not exist", func() {
88 ginkgo.It("should return an error", func() {
89 templater := &Templater{}
90 err := templater.SetTemplateFile("foo", "filename_that_should_not_exist")
91 gomega.Expect(err).To(gomega.HaveOccurred())
92 })
93 })
94 ginkgo.When("a template is being set with a badly formatted string", func() {
95 ginkgo.It("should return an error", func() {
96 templater := &Templater{}
97 err := templater.SetTemplateString("foo", "template {{ missing end tag")
98 gomega.Expect(err).To(gomega.HaveOccurred())
99 })
100 })
101 ginkgo.When("a template is being retrieved with a present ID", func() {
102 ginkgo.It("should return the corresponding template", func() {
103 templater := &Templater{}
104 err := templater.SetTemplateString("bar", "template body")
105 gomega.Expect(err).NotTo(gomega.HaveOccurred())
106
107 tpl, found := templater.GetTemplate("bar")
108 gomega.Expect(tpl).ToNot(gomega.BeNil())
109 gomega.Expect(found).To(gomega.BeTrue())
110 })
111 })
112 ginkgo.When("a template is being retrieved with an invalid ID", func() {
113 ginkgo.It("should return an error", func() {
114 templater := &Templater{}
115 err := templater.SetTemplateString("bar", "template body")
116 gomega.Expect(err).NotTo(gomega.HaveOccurred())
117
118 tpl, found := templater.GetTemplate("bad ID")
119 gomega.Expect(tpl).To(gomega.BeNil())
120 gomega.Expect(found).ToNot(gomega.BeTrue())
121 })
122 })
123 })
124
125 var _ = ginkgo.Describe("the standard enumless config implementation", func() {
126 ginkgo.When("it's enum method is called", func() {
127 ginkgo.It("should return an empty map", func() {
128 gomega.Expect((&EnumlessConfig{}).Enums()).To(gomega.BeEmpty())
129 })
130 })
131 })
132
133 var _ = ginkgo.Describe("the standard failure implementation", func() {
134 ginkgo.Describe("Failure function", func() {
135 ginkgo.When("called with FailParseURL", func() {
136 ginkgo.It("should return a failure with the correct message", func() {
137 err := errors.New("invalid URL")
138 failure := Failure(FailParseURL, err)
139 gomega.Expect(failure.ID()).To(gomega.Equal(FailParseURL))
140 gomega.Expect(failure.Error()).
141 To(gomega.ContainSubstring("error parsing Service URL"))
142 gomega.Expect(failure.Error()).To(gomega.ContainSubstring("invalid URL"))
143 })
144 })
145 ginkgo.When("called with FailUnknown", func() {
146 ginkgo.It("should return a failure with the unknown error message", func() {
147 err := errors.New("something went wrong")
148 failure := Failure(FailUnknown, err)
149 gomega.Expect(failure.ID()).To(gomega.Equal(FailUnknown))
150 gomega.Expect(failure.Error()).
151 To(gomega.ContainSubstring("an unknown error occurred"))
152 gomega.Expect(failure.Error()).To(gomega.ContainSubstring("something went wrong"))
153 })
154 })
155 ginkgo.When("called with an unrecognized FailureID", func() {
156 ginkgo.It("should fallback to the unknown error message", func() {
157 err := errors.New("unrecognized error")
158 failure := Failure(failures.FailureID(999), err) // Arbitrary unknown ID
159 gomega.Expect(failure.ID()).To(gomega.Equal(failures.FailureID(999)))
160 gomega.Expect(failure.Error()).
161 To(gomega.ContainSubstring("an unknown error occurred"))
162 gomega.Expect(failure.Error()).To(gomega.ContainSubstring("unrecognized error"))
163 })
164 })
165 ginkgo.When("called with additional arguments", func() {
166 ginkgo.It("should include formatted arguments in the error", func() {
167 err := errors.New("base error")
168 failure := Failure(FailParseURL, err, "extra info: %s", "details")
169 gomega.Expect(failure.Error()).
170 To(gomega.ContainSubstring("error parsing Service URL extra info: details"))
171 gomega.Expect(failure.Error()).To(gomega.ContainSubstring("base error"))
172 })
173 })
174 })
175
176 ginkgo.Describe("IsTestSetupFailure function", func() {
177 ginkgo.When("called with a FailTestSetup failure", func() {
178 ginkgo.It("should return true and the correct message", func() {
179 err := errors.New("setup issue")
180 failure := Failure(FailTestSetup, err)
181 msg, isSetupFailure := IsTestSetupFailure(failure)
182 gomega.Expect(isSetupFailure).To(gomega.BeTrue())
183 gomega.Expect(msg).To(gomega.ContainSubstring("test setup failed: setup issue"))
184 })
185 })
186 ginkgo.When("called with a different failure", func() {
187 ginkgo.It("should return false and an empty message", func() {
188 err := errors.New("parse issue")
189 failure := Failure(FailParseURL, err)
190 msg, isSetupFailure := IsTestSetupFailure(failure)
191 gomega.Expect(isSetupFailure).To(gomega.BeFalse())
192 gomega.Expect(msg).To(gomega.BeEmpty())
193 })
194 })
195 ginkgo.When("called with nil", func() {
196 ginkgo.It("should return false and an empty message", func() {
197 msg, isSetupFailure := IsTestSetupFailure(nil)
198 gomega.Expect(isSetupFailure).To(gomega.BeFalse())
199 gomega.Expect(msg).To(gomega.BeEmpty())
200 })
201 })
202 })
203 })
204
205 var _ = ginkgo.Describe("the standard send budget", func() {
206 ginkgo.It("should report the default send timeout", func() {
207 gomega.Expect(Standard{}.ServiceTimeout(nil)).To(gomega.Equal(types.DefaultSendTimeout))
208 })
209
210 ginkgo.It("should promote the budget through embedding", func() {
211 var budget types.ServiceTimeout = &struct{ Standard }{}
212
213 gomega.Expect(budget.ServiceTimeout(nil)).To(gomega.Equal(types.DefaultSendTimeout))
214 })
215 })
216
217 func TestStandard(t *testing.T) {
218 t.Parallel()
219 gomega.RegisterFailHandler(ginkgo.Fail)
220 ginkgo.RunSpecs(t, "Shoutrrr Standard Suite")
221 }