nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: looplab/eventhorizon
Pull request #374 (2021-12-30 11:40:31)

feat(commands): add a list func to export registered commands
### Description

Just exposes registered commands which i need for generating code 

### Affected Components

- eh commands

### Related Issues


### Solution and Design


### Steps to test and verify

1. registrate commands
2. validate commands exists in list



Patch:
diff --git a/command.go b/command.go
index fcbae4a..edcea14 100644
--- a/command.go
+++ b/command.go
@@ -120,5 +120,18 @@ func CreateCommand(commandType CommandType) (Command, error) {
 	return nil, ErrCommandNotRegistered
 }
 
+// ListCommands returns a list of all registered command types.
+func RegisteredCommands() map[CommandType]func() Command {
+	commandsMu.Lock()
+	defer commandsMu.Unlock()
+
+	mapCopy := make(map[CommandType]func() Command)
+	for key, val := range commands {
+		mapCopy[key] = val
+	}
+
+	return mapCopy
+}
+
 var commands = make(map[CommandType]func() Command)
 var commandsMu sync.RWMutex


Test patch:
diff --git a/command_test.go b/command_test.go
index 56fcf68..818f49b 100644
--- a/command_test.go
+++ b/command_test.go
@@ -28,6 +28,7 @@ func TestCreateCommand(t *testing.T) {
 	}
 
 	RegisterCommand(func() Command { return &TestCommandRegister{} })
+	defer UnregisterCommand(TestCommandRegisterType)
 
 	cmd, err := CreateCommand(TestCommandRegisterType)
 	if err != nil {
@@ -87,6 +88,17 @@ func TestUnregisterCommandTwice(t *testing.T) {
 	UnregisterCommand(TestCommandUnregisterTwiceType)
 }
 
+func TestRegisteredCommands(t *testing.T) {
+	commands = make(map[CommandType]func() Command)
+	RegisterCommand(func() Command { return &TestCommandRegister{} })
+	defer UnregisterCommand(TestCommandRegisterType)
+
+	commands := RegisteredCommands()
+	if len(commands) != 1 {
+		t.Error("there should be one command:", commands)
+	}
+}
+
 const (
 	TestCommandRegisterType        CommandType = "TestCommandRegister"
 	TestCommandRegisterEmptyType   CommandType = ""