Skip to content

Comments

Fix file:copy to return error when target file exists without REPLACE_EXISTING#577

Open
DharshiBalasubramaniyam wants to merge 4 commits intoballerina-platform:masterfrom
DharshiBalasubramaniyam:file-copy-replace-false-fix
Open

Fix file:copy to return error when target file exists without REPLACE_EXISTING#577
DharshiBalasubramaniyam wants to merge 4 commits intoballerina-platform:masterfrom
DharshiBalasubramaniyam:file-copy-replace-false-fix

Conversation

@DharshiBalasubramaniyam
Copy link
Contributor

Purpose

Fixes ballerina-platform/ballerina-library#8316

Examples

With this implementation below code returns InvalidOperationError, if temp2.txt already exists.

public function main() returns error? {
    string sourcePath = "temp1.txt";
    string destPath = "temp2.txt";
    file:Error? copy = file:copy(sourcePath, destPath);
    if copy is file:Error {
        io:println(copy.message());
    }
    io:println("done");
}

Output for the above code:

error InvalidOperationError ("The target file already exists: temp2.txt")

Tests

I have added 4 tests to reflect the below scenarios.

  1. testCopyFileToNonExistentFileReplaceFalse - file should be copied
  2. testCopyFileToExistingFileReplaceFalse - return error
  3. testCopyFileToExistingFileReplaceTrue - file replaced
  4. testCopyFileToNonExistentFileReplaceTrue - file created

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility

@codecov
Copy link

codecov bot commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.32%. Comparing base (ad65395) to head (a8b03de).

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #577      +/-   ##
============================================
- Coverage     82.64%   82.32%   -0.32%     
  Complexity       91       91              
============================================
  Files            18       18              
  Lines           772      775       +3     
  Branches        164      164              
============================================
  Hits            638      638              
- Misses          115      118       +3     
  Partials         19       19              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DharshiBalasubramaniyam
Copy link
Contributor Author

Hi @ThisaruGuruge, can you please review this pr?

@sonarqubecloud
Copy link

@DharshiBalasubramaniyam
Copy link
Contributor Author

Hi, @ThisaruGuruge, @niveathika, Any update on this pr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

file:copy does not return FILE_ALREADY_EXISTS_ERROR when target file already exists

2 participants