-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug - cmd with quotes doesnt work (#17)
The LLMs sometimes suggested commands which required shell quoting, which got escaped into commands that didn't act as the llm expected. Example, LLM suggested: find ./ -name "testfile", which will be executed as find ./ -name \"testfile\" since the quotes becomes escaped. * Setup tests to confirm bug * Fixed issue by removing all single and double quotes before executing command
- Loading branch information
Showing
2 changed files
with
103 additions
and
10 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
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,78 @@ | ||
package text | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/baalimago/clai/internal/models" | ||
"github.com/baalimago/go_away_boilerplate/pkg/testboil" | ||
) | ||
|
||
type mockCompleter struct{} | ||
|
||
func (m mockCompleter) Setup() error { | ||
return nil | ||
} | ||
|
||
func (m mockCompleter) StreamCompletions(ctx context.Context, c models.Chat) (chan models.CompletionEvent, error) { | ||
return nil, nil | ||
} | ||
|
||
func Test_executeAiCmd(t *testing.T) { | ||
testCases := []struct { | ||
description string | ||
setup func(t *testing.T) | ||
given string | ||
want string | ||
wantErr error | ||
}{ | ||
{ | ||
description: "it should run shell cmd", | ||
given: "printf 'test'", | ||
want: fmt.Sprintf(okFormat, "test"), | ||
wantErr: nil, | ||
}, | ||
{ | ||
description: "it should work with quotes", | ||
setup: func(t *testing.T) { | ||
t.Helper() | ||
os.Chdir(filepath.Dir(testboil.CreateTestFile(t, "testfile").Name())) | ||
}, | ||
given: "find ./ -name \"testfile\"", | ||
want: fmt.Sprintf(okFormat, "./testfile\n"), | ||
wantErr: nil, | ||
}, | ||
{ | ||
description: "it should work without quotes", | ||
setup: func(t *testing.T) { | ||
t.Helper() | ||
os.Chdir(filepath.Dir(testboil.CreateTestFile(t, "testfile").Name())) | ||
}, | ||
given: "find ./ -name testfile", | ||
want: fmt.Sprintf(okFormat, "./testfile\n"), | ||
wantErr: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
q := Querier[mockCompleter]{} | ||
if tc.setup != nil { | ||
tc.setup(t) | ||
} | ||
q.fullMsg = tc.given | ||
gotFormated, gotErr := q.executeLlmCmd() | ||
|
||
if gotFormated != tc.want { | ||
t.Fatalf("expected: %v, got: %v", tc.want, gotFormated) | ||
} | ||
|
||
if gotErr != tc.wantErr { | ||
t.Fatalf("expected error: %v, got: %v", tc.wantErr, gotErr) | ||
} | ||
}) | ||
} | ||
} |