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