-
Notifications
You must be signed in to change notification settings - Fork 2.6k
test: add command line test for multi-search-replace #2220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1355,6 +1355,121 @@ function five() { | |
| expect(result.success).toBe(false) | ||
| expect(result.error).toContain("'=======' found in your diff content") | ||
| }) | ||
|
|
||
| describe("command line processing", () => { | ||
| let strategy: MultiSearchReplaceDiffStrategy | ||
| beforeEach(() => { | ||
| strategy = new MultiSearchReplaceDiffStrategy() | ||
| }) | ||
|
|
||
| it("should process diff from command line arguments", async () => { | ||
| // This test is designed to be run from the command line with file arguments | ||
| // Example: npx jest src/core/diff/strategies/__tests__/multi-search-replace.test.ts -t "should process diff" -- file.ts diff.diff | ||
|
|
||
| // Get command line arguments | ||
| const args = process.argv.slice(2) | ||
|
|
||
| // Skip test if not run with arguments | ||
| // Parse command line arguments for --source and --diff flags | ||
| let sourceFile: string | undefined | ||
| let diffFile: string | undefined | ||
|
|
||
| for (let i = 0; i < args.length; i++) { | ||
| if (args[i] === "--source" && i + 1 < args.length) { | ||
| sourceFile = args[i + 1] | ||
| i++ // Skip the next argument as it's the value | ||
| } else if (args[i] === "--diff" && i + 1 < args.length) { | ||
| diffFile = args[i + 1] | ||
| i++ // Skip the next argument as it's the value | ||
| } | ||
| } | ||
|
|
||
| if (!sourceFile || !diffFile) { | ||
| console.debug( | ||
| `Optional debug usage: npx jest multi-search-replace.test.ts -- --source <file.ts> --diff <diff.diff>\n`, | ||
| ) | ||
| // console.debug('All args:', args); | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| // Read files | ||
| const fs = require("fs") | ||
| const sourceContent = fs.readFileSync(sourceFile, "utf8") | ||
| let diffContent = fs.readFileSync(diffFile, "utf8") | ||
|
|
||
| // Show first 50 lines of source content | ||
| process.stdout.write( | ||
| `\n\n====================================================================\n`, | ||
| ) | ||
| process.stdout.write(`== ${sourceFile} first 50 lines ==\n`) | ||
| process.stdout.write( | ||
| `====================================================================\n`, | ||
| ) | ||
| sourceContent | ||
| .split("\n") | ||
| .slice(0, 50) | ||
| .forEach((line: string) => { | ||
| process.stdout.write(`${line}\n`) | ||
| }) | ||
| process.stdout.write( | ||
| `=============================== END ================================\n`, | ||
| ) | ||
|
|
||
| process.stdout.write( | ||
| `\n\n====================================================================\n`, | ||
| ) | ||
| process.stdout.write(`== ${diffFile} first 50 lines ==\n`) | ||
| process.stdout.write( | ||
| `====================================================================\n`, | ||
| ) | ||
|
|
||
| // Show first 50 lines of diff content | ||
| diffContent | ||
| .split("\n") | ||
| .slice(0, 50) | ||
| .forEach((line: string) => { | ||
| process.stdout.write(`${line}\n`) | ||
| }) | ||
| process.stdout.write( | ||
| `=============================== END ================================\n`, | ||
| ) | ||
|
|
||
| // Apply the diff | ||
| const result = await strategy.applyDiff(sourceContent, diffContent) | ||
|
|
||
| if (result.success) { | ||
| process.stdout.write( | ||
| `\n\n====================================================================\n`, | ||
| ) | ||
| process.stdout.write(`== Diff applied successfully ==\n`) | ||
| process.stdout.write( | ||
| `====================================================================\n`, | ||
| ) | ||
| process.stdout.write(result.content + "\n") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @KJ7LNW, for this line where result.content is printed: does it make sense to print the whole content here since it might be long? Or perhaps print only the first 50 lines, similar to how the input source (line 1411) and diff (line 1430) are truncated?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really a debugging tool wrapped in jest, not a test itself: this is just for debugging for direct user interact , it is not intended for automated testing. I wrote this because I was troubleshooting some Maybe the line limit should be trimmed, but I specifically remember not wanting it to trim: the model originally wrote this trimming the output, but I had to tell it not to because I needed to see it all at once to compare what was happening. |
||
| process.stdout.write( | ||
| `=============================== END ================================\n`, | ||
| ) | ||
| expect(result.success).toBe(true) | ||
| } else { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure the test fails when diff application is unsuccessful by adding an assertion or calling |
||
| process.stdout.write( | ||
| `\n\n====================================================================\n`, | ||
| ) | ||
| process.stdout.write(`== Failed to apply diff ==\n`) | ||
| process.stdout.write( | ||
| `====================================================================\n\n\n`, | ||
| ) | ||
| console.error(result) | ||
| process.stdout.write( | ||
| `=============================== END ================================\n\n\n`, | ||
| ) | ||
| } | ||
| } catch (err) { | ||
| console.error("Error processing files:", err.message) | ||
| console.error("Stack trace:", err.stack) | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| it("should not strip content that starts with pipe but no line number", async () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid silently returning when required file arguments are missing. Instead, consider using
test.skipor explicitly failing so that missing arguments don't yield a false positive.