-
Notifications
You must be signed in to change notification settings - Fork 48
Implement missing os.File methods #1553
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f1bf4a
Implement missing os.File methods
xgopilot 44cde94
Add WriteAt test with non-zero offset
xgopilot efd2774
Fix pwrite return value handling to prevent infinite loop
xgopilot d371a29
Handle pread/pwrite return value 0 with blocking mode check
xgopilot c83e1a2
Add README for osfile demo explaining Go 1.25 runtime limitation
xgopilot d78a47e
Fix Seek test to expect YYYYY after WriteAt and remove README
xgopilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,126 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Test file operations | ||
| testFile := "test_file.txt" | ||
|
|
||
| // Clean up at the end | ||
| defer os.Remove(testFile) | ||
|
|
||
| // Test Write and WriteString | ||
| f, err := os.Create(testFile) | ||
| if err != nil { | ||
| panic("Create failed: " + err.Error()) | ||
| } | ||
|
|
||
| // Test Write | ||
| data := []byte("Hello, World!\n") | ||
| n, err := f.Write(data) | ||
| if err != nil || n != len(data) { | ||
| panic("Write failed") | ||
| } | ||
|
|
||
| // Test WriteString | ||
| n, err = f.WriteString("Test WriteString\n") | ||
| if err != nil || n != 17 { | ||
| panic("WriteString failed") | ||
| } | ||
|
|
||
| f.Close() | ||
|
|
||
| // Test ReadAt | ||
| f, err = os.Open(testFile) | ||
| if err != nil { | ||
| panic("Open failed: " + err.Error()) | ||
| } | ||
|
|
||
| buf := make([]byte, 5) | ||
| n, err = f.ReadAt(buf, 0) | ||
| if err != nil || n != 5 || string(buf) != "Hello" { | ||
| panic("ReadAt failed: expected 'Hello'") | ||
| } | ||
|
|
||
| n, err = f.ReadAt(buf, 7) | ||
| if err != nil || n != 5 || string(buf) != "World" { | ||
| panic("ReadAt failed: expected 'World'") | ||
| } | ||
|
|
||
| f.Close() | ||
|
|
||
| // Test WriteAt with offset 0 | ||
| f, err = os.OpenFile(testFile, os.O_RDWR, 0644) | ||
| if err != nil { | ||
| panic("OpenFile failed: " + err.Error()) | ||
| } | ||
|
|
||
| n, err = f.WriteAt([]byte("XXXXX"), 0) | ||
| if err != nil || n != 5 { | ||
| panic("WriteAt at offset 0 failed") | ||
| } | ||
|
|
||
| // Test WriteAt with non-zero offset | ||
| n, err = f.WriteAt([]byte("YYYYY"), 7) | ||
| if err != nil || n != 5 { | ||
| panic("WriteAt at offset 7 failed") | ||
| } | ||
|
|
||
| f.Close() | ||
|
|
||
| // Verify WriteAt results | ||
| f, err = os.Open(testFile) | ||
| if err != nil { | ||
| panic("Open failed: " + err.Error()) | ||
| } | ||
|
|
||
| buf = make([]byte, 5) | ||
| n, err = f.ReadAt(buf, 0) | ||
| if err != nil || n != 5 || string(buf) != "XXXXX" { | ||
| panic("WriteAt verification at offset 0 failed: expected 'XXXXX'") | ||
| } | ||
|
|
||
| buf = make([]byte, 5) | ||
| n, err = f.ReadAt(buf, 7) | ||
| if err != nil || n != 5 || string(buf) != "YYYYY" { | ||
| panic("WriteAt verification at offset 7 failed: expected 'YYYYY'") | ||
| } | ||
|
|
||
| f.Close() | ||
|
|
||
| // Test Seek | ||
| f, err = os.Open(testFile) | ||
| if err != nil { | ||
| panic("Open failed: " + err.Error()) | ||
| } | ||
|
|
||
| // Seek to position 7 | ||
| pos, err := f.Seek(7, 0) // SEEK_SET = 0 | ||
| if err != nil || pos != 7 { | ||
| panic("Seek failed") | ||
| } | ||
|
|
||
| buf = make([]byte, 5) | ||
| n, err = f.Read(buf) | ||
| if err != nil || n != 5 || string(buf) != "YYYYY" { | ||
| panic("Seek test failed: expected 'YYYYY'") | ||
| } | ||
|
|
||
| // Seek from current position | ||
| pos, err = f.Seek(2, 1) // SEEK_CUR = 1 | ||
| if err != nil || pos != 14 { | ||
| panic("Seek from current failed") | ||
| } | ||
|
|
||
| // Seek from end | ||
| pos, err = f.Seek(-5, 2) // SEEK_END = 2 | ||
| if err != nil { | ||
| panic("Seek from end failed") | ||
| } | ||
|
|
||
| f.Close() | ||
|
|
||
| println("All os.File tests passed!") | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@xgopilot WriteAt add test offset != 0
Uh oh!
There was an error while loading. Please reload this page.
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.
WriteAt test with offset != 0 added
Added a comprehensive test for
WriteAtwith non-zero offset (offset 7) to complement the existing test at offset 0.Changes made:
WriteAttest at offset 7 that writes "YYYYY"The test now verifies that:
WriteAtworks correctly at offset 0 (writes "XXXXX")WriteAtworks correctly at offset 7 (writes "YYYYY")Changes have been committed and pushed to the PR branch.