Skip to content

Commit e037a42

Browse files
alacukupoiana
authored andcommitted
new(tests): add unit tests for the output package.
Signed-off-by: Aldo Lacuku <[email protected]>
1 parent dd68f13 commit e037a42

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

pkg/output/output_suite_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2022 The Falco Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package output
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestOutput(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Output Suite")
27+
}

pkg/output/output_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2022 The Falco Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package output
16+
17+
import (
18+
"bytes"
19+
"io"
20+
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
var _ = Describe("Output", func() {
26+
var (
27+
printer *Printer
28+
scope string
29+
verbose bool
30+
writer io.Writer
31+
)
32+
33+
JustBeforeEach(func() {
34+
printer = NewPrinter(scope, verbose, writer)
35+
})
36+
37+
JustAfterEach(func() {
38+
printer = nil
39+
scope = ""
40+
verbose = false
41+
writer = nil
42+
})
43+
44+
Context("a new printer is created", func() {
45+
Context("with scope", func() {
46+
BeforeEach(func() {
47+
scope = "CustomScope"
48+
})
49+
50+
It("should correctly set the scope for each printer object", func() {
51+
Expect(printer.Error.Scope.Text).Should(Equal(scope))
52+
Expect(printer.Info.Scope.Text).Should(Equal(scope))
53+
Expect(printer.Warning.Scope.Text).Should(Equal(scope))
54+
})
55+
})
56+
57+
Context("with writer", func() {
58+
BeforeEach(func() {
59+
writer = &bytes.Buffer{}
60+
})
61+
62+
It("should correctly set the writer for each printer object", func() {
63+
Expect(printer.Error.Writer).Should(Equal(writer))
64+
Expect(printer.Info.Writer).Should(Equal(writer))
65+
Expect(printer.Warning.Writer).Should(Equal(writer))
66+
Expect(printer.DefaultText.Writer).Should(Equal(writer))
67+
})
68+
})
69+
70+
Context("with verbose", func() {
71+
BeforeEach(func() {
72+
verbose = true
73+
})
74+
It("should correctly set the verbose variable to true", func() {
75+
Expect(printer.verbose).Should(BeTrue())
76+
})
77+
})
78+
})
79+
80+
Context("testing output using the verbose function", func() {
81+
var (
82+
msg = "Testing verbose mode"
83+
customWriter *bytes.Buffer
84+
)
85+
86+
BeforeEach(func() {
87+
// set the output writer.
88+
customWriter = &bytes.Buffer{}
89+
writer = customWriter
90+
})
91+
92+
JustBeforeEach(func() {
93+
// call the output function
94+
printer.Verbosef("%s", msg)
95+
})
96+
97+
Context("verbose mode is disabled", func() {
98+
It("should not output the message", func() {
99+
Expect(customWriter.String()).Should(BeEmpty())
100+
})
101+
})
102+
103+
Context("verbose mode is enabled", func() {
104+
BeforeEach(func() {
105+
verbose = true
106+
})
107+
108+
It("should output the message", func() {
109+
Expect(customWriter.String()).Should(ContainSubstring(msg))
110+
})
111+
})
112+
})
113+
})

0 commit comments

Comments
 (0)