This commit introduces a significant refactoring of the `cmd` package to improve testability and increases test coverage across the application. Key changes include: - Refactored Cobra commands to use `RunE` for better error handling and testing. - Extracted business logic from command handlers into separate, testable functions. - Added comprehensive unit tests for the `cmd`, `compress`, `github`, `logger`, and `pwa` packages. - Added tests for missing command-line arguments, as requested. - Implemented the `borg all` command to clone all public repositories for a GitHub user or organization. - Restored and improved the `collect pwa` functionality. - Removed duplicate code and fixed various bugs.
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package logger
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"log/slog"
|
|
"testing"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
// Test non-verbose logger
|
|
var buf bytes.Buffer
|
|
oldStderr := os.Stderr
|
|
r, w, _ := os.Pipe()
|
|
os.Stderr = w
|
|
|
|
log := New(false)
|
|
log.Info("info message")
|
|
log.Debug("debug message")
|
|
|
|
w.Close()
|
|
os.Stderr = oldStderr
|
|
io.Copy(&buf, r)
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte("info message")) {
|
|
t.Errorf("expected info message to be logged")
|
|
}
|
|
if bytes.Contains(buf.Bytes(), []byte("debug message")) {
|
|
t.Errorf("expected debug message not to be logged")
|
|
}
|
|
|
|
// Test verbose logger
|
|
buf.Reset()
|
|
r, w, _ = os.Pipe()
|
|
os.Stderr = w
|
|
|
|
log = New(true)
|
|
log.Info("info message")
|
|
log.Debug("debug message")
|
|
|
|
w.Close()
|
|
os.Stderr = oldStderr
|
|
io.Copy(&buf, r)
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte("info message")) {
|
|
t.Errorf("expected info message to be logged")
|
|
}
|
|
if !bytes.Contains(buf.Bytes(), []byte("debug message")) {
|
|
t.Errorf("expected debug message to be logged")
|
|
}
|
|
}
|
|
func TestNew_Level(t *testing.T) {
|
|
log := New(true)
|
|
if !log.Enabled(context.Background(), slog.LevelDebug) {
|
|
t.Errorf("expected debug level to be enabled")
|
|
}
|
|
|
|
log = New(false)
|
|
if log.Enabled(context.Background(), slog.LevelDebug) {
|
|
t.Errorf("expected debug level to be disabled")
|
|
}
|
|
}
|