chore: adjust parsing for existing default and addtional commands

tauraamui created

Change summary

internal/agent/tools/bash.go      | 10 +++-
internal/agent/tools/bash_test.go | 60 +++++++++++++++++++++++++++++++++
internal/config/config.go         |  6 +-
3 files changed, 70 insertions(+), 6 deletions(-)

Detailed changes

internal/agent/tools/bash.go 🔗

@@ -186,15 +186,19 @@ func blockFuncs(bannedCommands []string) []shell.BlockFunc {
 	}
 }
 
-func resolveBlockFuncs(cfg config.ToolBash) []shell.BlockFunc {
+func resolveBannedCommandsList(cfg config.ToolBash) []string {
 	bannedCommands := cfg.BannedCommands
 	if !cfg.DisableDefaults {
 		if len(bannedCommands) == 0 {
-			return blockFuncs(defaultBannedCommands)
+			return defaultBannedCommands
 		}
 		bannedCommands = append(bannedCommands, defaultBannedCommands...)
 	}
-	return blockFuncs(bannedCommands)
+	return bannedCommands
+}
+
+func resolveBlockFuncs(cfg config.ToolBash) []shell.BlockFunc {
+	return blockFuncs(resolveBannedCommandsList(cfg))
 }
 
 func NewBashTool(

internal/agent/tools/bash_test.go 🔗

@@ -0,0 +1,60 @@
+package tools
+
+import (
+	"testing"
+
+	"github.com/charmbracelet/crush/internal/config"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func TestResolveBlockFuncsFromEmptyConfig(t *testing.T) {
+	cfg := config.ToolBash{
+		DisableDefaults:   false,
+		BannedCommands:    []string{},
+		BannedSubCommands: []config.BannedToolArgsAndOrParams{},
+	}
+
+	bannedCmds := resolveBannedCommandsList(cfg)
+	require.Len(t, bannedCmds, len(defaultBannedCommands))
+	assert.Equal(t, defaultBannedCommands, bannedCmds)
+}
+
+func TestResolveBlockFuncsFromEmptyConfigWithDefaultDisabled(t *testing.T) {
+	cfg := config.ToolBash{
+		DisableDefaults:   true,
+		BannedCommands:    []string{},
+		BannedSubCommands: []config.BannedToolArgsAndOrParams{},
+	}
+
+	bannedCmds := resolveBannedCommandsList(cfg)
+	require.Len(t, bannedCmds, 0)
+	assert.Equal(t, []string{}, bannedCmds)
+}
+
+func TestResolveBlockFuncsDefaultDisabledWithBannedCommands(t *testing.T) {
+	cfg := config.ToolBash{
+		DisableDefaults: true,
+		BannedCommands: []string{
+			"pacman",
+			"mount",
+		},
+		BannedSubCommands: []config.BannedToolArgsAndOrParams{},
+	}
+
+	bannedCmds := resolveBannedCommandsList(cfg)
+	require.Len(t, bannedCmds, 2)
+	assert.Equal(t, []string{"pacman", "mount"}, bannedCmds)
+}
+
+func TestResolveBlockFuncsWithDefaultAndAddtionalBannedCommands(t *testing.T) {
+	additionalBannedCommands := []string{"lazygit", "btop"}
+	cfg := config.ToolBash{
+		DisableDefaults:   false,
+		BannedCommands:    additionalBannedCommands,
+		BannedSubCommands: []config.BannedToolArgsAndOrParams{},
+	}
+
+	bannedCmds := resolveBannedCommandsList(cfg)
+	require.Len(t, bannedCmds, len(defaultBannedCommands)+len(additionalBannedCommands))
+}

internal/config/config.go 🔗

@@ -323,9 +323,9 @@ type ToolLs struct {
 }
 
 type ToolBash struct {
-	DisableDefaults bool                      `json:"disable_banned_defaults,omitempty"`
-	BannedCommands  []string                  `json:"banned_commands,omitempty"`
-	SubCommands     BannedToolArgsAndOrParams `json:"banned_sub_commands"`
+	DisableDefaults   bool                        `json:"disable_banned_defaults,omitempty"`
+	BannedCommands    []string                    `json:"banned_commands,omitempty"`
+	BannedSubCommands []BannedToolArgsAndOrParams `json:"banned_sub_commands"`
 }
 
 type BannedToolArgsAndOrParams struct {