Skip to content

Commit 4a6fc81

Browse files
test: added for the options/root.go (#638)
Signed-off-by: Rahul Vishwakarma <rahulvs2809@gmail.com>
1 parent d616c78 commit 4a6fc81

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

options/root_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025 The Witness Contributors
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 options
16+
17+
import (
18+
"testing"
19+
20+
"github.com/spf13/cobra"
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
// Test RootOptions.AddFlags method
25+
func TestRootOptions_AddFlags(t *testing.T) {
26+
cmd := &cobra.Command{
27+
Use: "test",
28+
}
29+
30+
ro := &RootOptions{}
31+
ro.AddFlags(cmd)
32+
33+
// Test presence of flags
34+
flags := []string{
35+
"config",
36+
"log-level",
37+
"debug-cpu-profile-file",
38+
"debug-mem-profile-file",
39+
}
40+
41+
for _, name := range flags {
42+
flag := cmd.PersistentFlags().Lookup(name)
43+
assert.NotNil(t, flag, "Flag '%s' should be added", name)
44+
}
45+
46+
// Test flag defaults
47+
assert.Equal(t, ".witness.yaml", cmd.PersistentFlags().Lookup("config").DefValue, "Default config path should be set correctly")
48+
assert.Equal(t, "info", cmd.PersistentFlags().Lookup("log-level").DefValue, "Default log-level should be 'info'")
49+
assert.Equal(t, "", cmd.PersistentFlags().Lookup("debug-cpu-profile-file").DefValue, "Default CPU profile file should be empty")
50+
assert.Equal(t, "", cmd.PersistentFlags().Lookup("debug-mem-profile-file").DefValue, "Default memory profile file should be empty")
51+
52+
// Test flag shorthand
53+
assert.Equal(t, "c", cmd.PersistentFlags().Lookup("config").Shorthand, "Config flag should have shorthand 'c'")
54+
assert.Equal(t, "l", cmd.PersistentFlags().Lookup("log-level").Shorthand, "Log-level flag should have shorthand 'l'")
55+
}

0 commit comments

Comments
 (0)