diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2026-01-26 16:44:12 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2026-01-26 17:04:48 +0000 |
| commit | fa7953d4ea9f5b7e5147ff086ac5a67bc2d2675b (patch) | |
| tree | db709605d03477d31b9fbe151198a1787f07b8fe /pkg/aflow/tool/codeeditor/codeeditor_test.go | |
| parent | e7922f79bc8da0b8ef96a080e463141bb5e79694 (diff) | |
pkg/aflow/tool/codeeditor: add actual implementation
Diffstat (limited to 'pkg/aflow/tool/codeeditor/codeeditor_test.go')
| -rw-r--r-- | pkg/aflow/tool/codeeditor/codeeditor_test.go | 176 |
1 files changed, 170 insertions, 6 deletions
diff --git a/pkg/aflow/tool/codeeditor/codeeditor_test.go b/pkg/aflow/tool/codeeditor/codeeditor_test.go index 4ba556f1b..06a97d7af 100644 --- a/pkg/aflow/tool/codeeditor/codeeditor_test.go +++ b/pkg/aflow/tool/codeeditor/codeeditor_test.go @@ -4,6 +4,8 @@ package codeeditor import ( + "fmt" + "os" "path/filepath" "testing" @@ -15,7 +17,7 @@ import ( func TestCodeeditorEscapingPath(t *testing.T) { aflow.TestTool(t, Tool, state{ - KernelScratchSrc: "whatever", + KernelScratchSrc: t.TempDir(), }, args{ SourceFile: "../../passwd", @@ -38,25 +40,187 @@ func TestCodeeditorMissingPath(t *testing.T) { ) } +func TestCodeeditorNonSourceFile(t *testing.T) { + dir := writeTestFile(t, "src", "data") + aflow.TestTool(t, Tool, + state{ + KernelScratchSrc: dir, + }, + args{ + SourceFile: "src", + }, + struct{}{}, + `SourceFile "src" does not exist`, + ) +} + func TestCodeeditorEmptyCurrentCode(t *testing.T) { - dir := writeTestFile(t, "foo", "data") + dir := writeTestFile(t, "src.c", "data") aflow.TestTool(t, Tool, state{ KernelScratchSrc: dir, }, args{ - SourceFile: "foo", + SourceFile: "src.c", }, struct{}{}, `CurrentCode snippet is empty`, ) } +func TestCodeeditorNoMatches(t *testing.T) { + dir := writeTestFile(t, "src.c", "foo") + aflow.TestTool(t, Tool, + state{ + KernelScratchSrc: dir, + }, + args{ + SourceFile: "src.c", + CurrentCode: "foobar", + }, + struct{}{}, + `CurrentCode snippet does not match anything in the source file, provide more precise CurrentCode snippet`, + ) +} + +func TestCodeeditorMultipleMatches(t *testing.T) { + dir := writeTestFile(t, "src.c", ` +linefoo +bar +foo +bar +foo +fooline +foo`) + aflow.TestTool(t, Tool, + state{ + KernelScratchSrc: dir, + }, + args{ + SourceFile: "src.c", + CurrentCode: "foo", + }, + struct{}{}, + `CurrentCode snippet matched 3 places, increase context in CurrentCode to avoid ambiguity`, + ) +} + +func TestCodeeditorReplacement(t *testing.T) { + type Test struct { + curFile string + curCode string + newCode string + newFile string + } + tests := []Test{ + { + curFile: ` +line0 +line1 +lineee2 +lin3 +last line +`, + curCode: `line1 +lineee2 +lin3`, + newCode: `replaced line`, + newFile: ` +line0 +replaced line +last line +`, + }, + { + curFile: ` +line0 +line1 +last line +`, + curCode: `line1 +`, + newCode: `replaced line 1 +replaced line 2 +replaced line 3`, + newFile: ` +line0 +replaced line 1 +replaced line 2 +replaced line 3 +last line +`, + }, + { + curFile: ` +line0 +line1 +line2 +`, + curCode: `line2 +`, + newCode: ``, + newFile: ` +line0 +line1 +`, + }, + { + curFile: `that's it`, + curCode: `that's it`, + newCode: `that's that`, + newFile: `that's that +`, + }, + { + curFile: ` + line0 + line1 + + line2 +line3 + +line4 +`, + curCode: ` +line1 + line2 + + + line3 `, + newCode: ` replacement`, + newFile: ` + line0 + replacement + +line4 +`, + }, + } + for i, test := range tests { + t.Run(fmt.Sprint(i), func(t *testing.T) { + const filename = "src.c" + dir := writeTestFile(t, filename, test.curFile) + aflow.TestTool(t, Tool, + state{ + KernelScratchSrc: dir, + }, + args{ + SourceFile: filename, + CurrentCode: test.curCode, + NewCode: test.newCode, + }, + struct{}{}, + "") + data, err := os.ReadFile(filepath.Join(dir, filename)) + require.NoError(t, err) + require.Equal(t, test.newFile, string(data)) + }) + } +} + func writeTestFile(t *testing.T, filename, data string) string { dir := t.TempDir() - if err := osutil.WriteFile(filepath.Join(dir, filename), []byte(data)); err != nil { - t.Fatal(err) - } + require.NoError(t, osutil.WriteFile(filepath.Join(dir, filename), []byte(data))) return dir } |
