Files
mark/util/cli_test.go

122 lines
3.3 KiB
Go
Raw Normal View History

2025-09-11 13:25:40 +02:00
package util
import (
"context"
"testing"
2026-03-28 09:55:58 +01:00
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
2025-09-11 13:25:40 +02:00
"github.com/urfave/cli/v3"
)
func runWithArgs(args []string) error {
cmd := &cli.Command{
Flags: []cli.Flag{
&cli.BoolFlag{Name: "title-from-h1"},
&cli.BoolFlag{Name: "title-from-filename"},
&cli.StringFlag{Name: "content-appearance"},
2025-09-11 13:25:40 +02:00
},
Before: CheckFlags,
2025-09-11 13:25:40 +02:00
Action: func(ctx context.Context, cmd *cli.Command) error {
return nil
},
}
return cmd.Run(context.Background(), args)
}
func TestCheckMutuallyExclusiveTitleFlags(t *testing.T) {
t.Run("neither flag set", func(t *testing.T) {
err := runWithArgs([]string{"cmd"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("only title-from-h1 set", func(t *testing.T) {
err := runWithArgs([]string{"cmd", "--title-from-h1"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("only title-from-filename set", func(t *testing.T) {
err := runWithArgs([]string{"cmd", "--title-from-filename"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("both flags set", func(t *testing.T) {
err := runWithArgs([]string{"cmd", "--title-from-h1", "--title-from-filename"})
if err == nil {
t.Errorf("expected error, got nil")
}
})
}
func TestContentAppearanceFlagValidation(t *testing.T) {
t.Run("fixed is accepted", func(t *testing.T) {
err := runWithArgs([]string{"cmd", "--content-appearance", "fixed"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("full-width is accepted", func(t *testing.T) {
err := runWithArgs([]string{"cmd", "--content-appearance", "full-width"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("invalid value is rejected", func(t *testing.T) {
err := runWithArgs([]string{"cmd", "--content-appearance", "nope"})
if err == nil {
t.Errorf("expected error, got nil")
}
})
}
func Test_setLogLevel(t *testing.T) {
type args struct {
lvl string
}
tests := map[string]struct {
args args
2026-03-28 09:55:58 +01:00
want zerolog.Level
expectedErr string
}{
2026-03-28 09:55:58 +01:00
"invalid": {args: args{lvl: "INVALID"}, want: zerolog.InfoLevel, expectedErr: "unknown log level: INVALID"},
"empty": {args: args{lvl: ""}, want: zerolog.InfoLevel, expectedErr: "unknown log level: "},
"info": {args: args{lvl: "INFO"}, want: zerolog.InfoLevel},
"debug": {args: args{lvl: "DEBUG"}, want: zerolog.DebugLevel},
"trace": {args: args{lvl: "TRACE"}, want: zerolog.TraceLevel},
"warning": {args: args{lvl: "WARNING"}, want: zerolog.WarnLevel},
"error": {args: args{lvl: "ERROR"}, want: zerolog.ErrorLevel},
"fatal": {args: args{lvl: "FATAL"}, want: zerolog.FatalLevel},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
2026-03-28 09:55:58 +01:00
prev := zerolog.GlobalLevel()
t.Cleanup(func() { zerolog.SetGlobalLevel(prev) })
cmd := &cli.Command{
Name: "test",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Value: tt.args.lvl,
Usage: "set the log level. Possible values: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL.",
},
},
}
err := SetLogLevel(cmd)
if tt.expectedErr != "" {
assert.EqualError(t, err, tt.expectedErr)
} else {
assert.NoError(t, err)
2026-03-28 09:55:58 +01:00
assert.Equal(t, tt.want, zerolog.GlobalLevel())
}
})
}
}