-
Notifications
You must be signed in to change notification settings - Fork 42
test(file): add unit tests for file.go #740
Copy link
Copy link
Description
The internal/librarian/file.go file contains utility functions for file operations, but it currently lacks any unit tests. This makes it difficult to ensure the correctness of these functions and prevent regressions.
This issue tracks the work to add comprehensive unit tests for all functions in file.go.
Implementation Guidance
The functions to be tested are:
readAllBytesFromFileappendToFilecreateAndWriteToFilecreateAndWriteBytesToFile
A recommended approach is to use the t.TempDir() function, which is available in Go's testing package. This will create a temporary directory for each test, ensuring that tests are isolated and do not leave artifacts on the filesystem.
For each function, the tests should cover:
- Success cases:
- For write operations (
appendToFile,createAndWriteToFile,createAndWriteBytesToFile), write a file to the temporary directory and then read it back to verify its contents are correct. - For read operations (
readAllBytesFromFile), create a file with known content in the temporary directory, call the function, and assert that the returned content is correct.
- For write operations (
- Edge cases: Test appending to an existing file, overwriting a file, and handling empty files.
- Error cases: Test scenarios like attempting to read a non-existent file.
Acceptance Criteria
- All functions in
internal/librarian/file.gohave unit tests. - The new tests cover success, edge, and error cases as described above.
- The overall test coverage for
file.go, as measured bygo test -cover, is at least 90%.
Reactions are currently unavailable