From e526ca61330751f700b25ae34c9f15d4029be014 Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Thu, 26 Jun 2025 23:17:09 +0800 Subject: [PATCH] :white_check_mark: Add tests for utils --- pkg/utils/BUILD.bazel | 12 ++- pkg/utils/utils_test.go | 204 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 pkg/utils/utils_test.go diff --git a/pkg/utils/BUILD.bazel b/pkg/utils/BUILD.bazel index 95d4fd7..0b436aa 100644 --- a/pkg/utils/BUILD.bazel +++ b/pkg/utils/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_go//go:def.bzl", "go_library") +load("@rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "utils", @@ -10,3 +10,13 @@ go_library( "@in_gopkg_yaml_v3//:yaml_v3", ], ) + +go_test( + name = "utils_test", + srcs = ["utils_test.go"], + embed = [":utils"], + deps = [ + "@com_github_spf13_cobra//:cobra", + "@com_github_spf13_pflag//:pflag", + ], +) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 0000000..87e02e7 --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,204 @@ +package utils + +import ( + "bytes" + "fmt" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "reflect" + "testing" +) + +func TestBoolPtr(t *testing.T) { + type args struct { + b string + } + tests := []struct { + name string + args args + want *bool + }{ + { + name: "true", + args: args{b: "true"}, + want: func() *bool { + b := true + return &b + }(), + }, + { + name: "false", + args: args{b: "false"}, + want: func() *bool { + b := false + return &b + }(), + }, + { + name: "empty", + args: args{b: ""}, + want: nil, + }, + } + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + if got := BoolPtr(tt.args.b); !reflect.DeepEqual(got, tt.want) { + t.Errorf("BoolPtr() = %v, want %v", got, tt.want) + } + }, + ) + } +} + +func TestGetFileName(t *testing.T) { + type args struct { + file string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "with extension", + args: args{file: "example.txt"}, + want: "example", + }, + { + name: "without extension", + args: args{file: "example"}, + want: "example", + }, + } + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + if got := GetFileName(tt.args.file); got != tt.want { + t.Errorf("GetFileName() = %v, want %v", got, tt.want) + } + }, + ) + } +} + +func TestIsJson(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "valid json", + args: args{s: `{"key": "value"}`}, + want: true, + }, + { + name: "invalid json", + args: args{s: `{"key": "value"`}, + want: false, + }, + } + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + if got := IsJson(tt.args.s); got != tt.want { + t.Errorf("IsJson() = %v, want %v", got, tt.want) + } + }, + ) + } +} + +func TestPrintJSON(t *testing.T) { + type args struct { + data interface{} + } + tests := []struct { + name string + args args + wantWriter string + }{ + { + name: "simple json", + args: args{data: map[string]string{"key": "value"}}, + wantWriter: fmt.Sprint("{\n \"key\": \"value\"\n}\n"), + }, + } + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + writer := &bytes.Buffer{} + PrintJSON(tt.args.data, writer) + if gotWriter := writer.String(); gotWriter != tt.wantWriter { + t.Errorf("PrintJSON() = %v, want %v", gotWriter, tt.wantWriter) + } + }, + ) + } +} + +func TestPrintYAML(t *testing.T) { + type args struct { + data interface{} + } + tests := []struct { + name string + args args + wantWriter string + }{ + { + name: "simple yaml", + args: args{data: map[string]string{"key": "value"}}, + wantWriter: "key: value\n\n", + }, + } + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + writer := &bytes.Buffer{} + PrintYAML(tt.args.data, writer) + if gotWriter := writer.String(); gotWriter != tt.wantWriter { + t.Errorf("PrintYAML() = %v, want %v", gotWriter, tt.wantWriter) + } + }, + ) + } +} + +func TestResetBool(t *testing.T) { + type args struct { + m map[string]**bool + flagSet *pflag.FlagSet + } + b := BoolPtr("true") + cmd := &cobra.Command{} + cmd.Flags().BoolVar(b, "flag", false, "") + tests := []struct { + name string + args args + }{ + { + name: "reset bool flags", + args: args{ + m: map[string]**bool{ + "flag": &b, + }, + flagSet: cmd.Flags(), + }, + }, + } + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + ResetBool(tt.args.m, tt.args.flagSet) + if b != nil { + t.Errorf("ResetBool() = %v, want nil", *b) + } + }, + ) + } +}