-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42eda17
commit 870342e
Showing
3 changed files
with
111 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func TestGetCommandsFromConfig(t *testing.T) { | ||
// Mocking the TOML config data | ||
mockData := ` | ||
[components] | ||
[components.celestia] | ||
[components.celestia.blah] | ||
binary = "/tmp/vimana/celestia/celestia" | ||
download = "/tmp/vimana/celestia/init.sh" | ||
[components.celestia.bridge] | ||
binary = "/tmp/vimana/celestia/celestia" | ||
download = "/tmp/vimana/celestia/init.sh" | ||
[components.berachain] | ||
[components.berachain.light] | ||
binary = "berachain-light" | ||
download = "/tmp/vimana/berachain/init.sh" | ||
` | ||
// Write mockData to a temporary file | ||
tmpfile, err := ioutil.TempFile("", "example.toml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tmpfile.Name()) // cleanup | ||
if _, err := tmpfile.Write([]byte(mockData)); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := tmpfile.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Define a mock commanderRegistry | ||
var mockCommanderRegistry = map[string]NodeCommander{ | ||
"celestia-light": NewMockCommander(), | ||
"celestia-bridge": NewMockCommander(), | ||
"avail-light": NewMockCommander(), | ||
} | ||
|
||
// Call GetCommandsFromConfig | ||
commands, err := GetCommandsFromConfig(tmpfile.Name(), mockCommanderRegistry) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(commands) != 1 { | ||
t.Fatalf("Expected 1 main command but got %d", len(commands)) | ||
} | ||
|
||
runCmd := commands[0] | ||
if runCmd.Use != "run" { | ||
t.Fatalf("Expected 'run' command but got '%s'", runCmd.Use) | ||
} | ||
|
||
if len(runCmd.Commands()) != 2 { | ||
t.Fatalf("Expected 2 component commands but got %d", len(runCmd.Commands())) | ||
} | ||
} | ||
|
||
type MockCommander struct{ BaseCommander } | ||
|
||
func NewMockCommander() *MockCommander { | ||
return &MockCommander{ | ||
BaseCommander: BaseCommander{NodeType: "light"}, | ||
} | ||
} | ||
|
||
func (m *MockCommander) Init(cmd *cobra.Command, args []string, mode Mode) error { | ||
fmt.Println("MockCommander.Init() called") | ||
return nil | ||
} | ||
|
||
func (m *MockCommander) Start(cmd *cobra.Command, args []string, mode Mode) { | ||
} | ||
|
||
func (m *MockCommander) Status(cmd *cobra.Command, args []string, mode Mode) { | ||
} | ||
|
||
func (m *MockCommander) Stop(cmd *cobra.Command, args []string, mode Mode) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters