mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
[file/bandwidth] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases [file/perm] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases - ADD function to parse form "rwx-wxr-x" or "-rwx-w-r-x" - ADD function to ParseFileMode to convert os.FileMode to file.Perm [file/progress] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases [ioutils/...] - UPDATE documentation: update enhanced README and TESTING guidelines - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases [logger/...] - UPDATE documentation: update enhanced README and TESTING guidelines - ADD documentation: add enhanced README and TESTING guidelines for sub packages - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases - UPDATE config: remove FileBufferSize from OptionFile (rework hookfile) - UPDATE fields: expose Store function in interface - REWORK hookfile: rework package, use aggregator to allow multi write and single file - FIX hookstderr: fix bug with NonColorable - FIX hookstdout: fix bug with NonColorable - FIX hookwriter: fix bug with NonColorable [network/protocol] - ADD function IsTCP, IsUDP, IsUnixLike to check type of protocol [runner] - FIX typo [socket] - UPDATE documentation: update enhanced README and TESTING guidelines - ADD documentation: add enhanced README and TESTING guidelines for sub packages - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases - REWORK server: use context compatible io.reader, io.writer, io.closer instead of reader / writer - REWORK server: simplify, optimize server - REMOVE reader, writer type - ADD context: add new interface in root socket interface to expose context interface that extend context, io reader/writer/closer, dediacted function to server (IsConnected, ...)
323 lines
8.2 KiB
Go
323 lines
8.2 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*
|
|
*/
|
|
|
|
package hookstdout_test
|
|
|
|
import (
|
|
"io"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
logcfg "github.com/nabbar/golib/logger/config"
|
|
loghko "github.com/nabbar/golib/logger/hookstdout"
|
|
)
|
|
|
|
// Test suite for HookStdOut creation and configuration.
|
|
// Tests hook instantiation, option handling, level filtering,
|
|
// formatter configuration, and hook registration.
|
|
var _ = Describe("HookStdOut Creation and Configuration", func() {
|
|
Describe("New", func() {
|
|
Context("with nil options", func() {
|
|
It("should return nil hook", func() {
|
|
hook, err := loghko.New(nil, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).To(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with DisableStandard true", func() {
|
|
It("should return nil hook", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: true,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).To(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with valid options", func() {
|
|
It("should create hook successfully", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with empty levels", func() {
|
|
It("should use all levels", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, []logrus.Level{}, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
|
|
levels := hook.Levels()
|
|
Expect(levels).To(Equal(logrus.AllLevels))
|
|
})
|
|
})
|
|
|
|
Context("with specific levels", func() {
|
|
It("should use provided levels", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
customLevels := []logrus.Level{
|
|
logrus.ErrorLevel,
|
|
logrus.WarnLevel,
|
|
logrus.InfoLevel,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, customLevels, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
|
|
levels := hook.Levels()
|
|
Expect(levels).To(Equal(customLevels))
|
|
})
|
|
})
|
|
|
|
Context("with DisableColor true", func() {
|
|
It("should use os.Stderr", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
DisableColor: true,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with DisableColor false", func() {
|
|
It("should use colorable stdout", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
DisableColor: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with DisableStack option", func() {
|
|
It("should accept DisableStack", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
DisableStack: true,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with DisableTimestamp option", func() {
|
|
It("should accept DisableTimestamp", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
DisableTimestamp: true,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with EnableTrace option", func() {
|
|
It("should accept EnableTrace", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
EnableTrace: true,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with EnableAccessLog option", func() {
|
|
It("should accept EnableAccessLog", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
EnableAccessLog: true,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with custom formatter", func() {
|
|
It("should accept JSON formatter", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
formatter := &logrus.JSONFormatter{}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, formatter)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
|
|
It("should accept Text formatter", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
formatter := &logrus.TextFormatter{}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, formatter)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("with all options combined", func() {
|
|
It("should handle all options together", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
DisableColor: true,
|
|
DisableStack: true,
|
|
DisableTimestamp: true,
|
|
EnableTrace: true,
|
|
EnableAccessLog: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, &logrus.JSONFormatter{})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("RegisterHook", func() {
|
|
Context("with valid logger", func() {
|
|
It("should register hook successfully", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
|
|
logger := logrus.New()
|
|
logger.SetOutput(io.Discard)
|
|
hook.RegisterHook(logger)
|
|
|
|
// Logger should now have the hook
|
|
Expect(logger).ToNot(BeNil())
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("Levels", func() {
|
|
Context("with default levels", func() {
|
|
It("should return all logrus levels", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
levels := hook.Levels()
|
|
Expect(levels).To(HaveLen(len(logrus.AllLevels)))
|
|
Expect(levels).To(Equal(logrus.AllLevels))
|
|
})
|
|
})
|
|
|
|
Context("with custom levels", func() {
|
|
It("should return custom levels", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
customLevels := []logrus.Level{
|
|
logrus.ErrorLevel,
|
|
logrus.FatalLevel,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, customLevels, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
levels := hook.Levels()
|
|
Expect(levels).To(Equal(customLevels))
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("Write method", func() {
|
|
Context("with valid hook", func() {
|
|
It("should implement io.Writer", func() {
|
|
opt := &logcfg.OptionsStd{
|
|
DisableStandard: false,
|
|
}
|
|
|
|
hook, err := loghko.NewWithWriter(io.Discard, opt, nil, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(hook).ToNot(BeNil())
|
|
|
|
// Test that it implements io.Writer
|
|
data := []byte("test message\n")
|
|
n, err := hook.Write(data)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(n).To(Equal(len(data)))
|
|
})
|
|
})
|
|
})
|
|
})
|