Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "os"
version = "1.10.0"
version = "1.10.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
Expand Down
49 changes: 34 additions & 15 deletions ballerina/tests/file-test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ function testCreateDirWithoutParentDir() {
}

@test:Config {}
function testCopyFile() {
function testCopyFileToNonExistentFileReplaceFalse() {
error? removeResult = remove(tmpdir + copyFile);
if removeResult is error && removeResult !is FileNotFoundError {
io:println(">>>> " + removeResult.toString());
test:assertFail("Error removing test resource!");
}

MetaData|error srcmetadata = getMetaData(srcFile);
if srcmetadata is MetaData {
srcFileLength = srcmetadata.size;
Expand All @@ -267,41 +273,54 @@ function testCopyFile() {
}
}

@test:Config {dependsOn: [testCopyFile]}
function testCopyFileReplaceFalse() {
MetaData|error srcMmetadata = getMetaData(srcModifiedFile);
if srcMmetadata is MetaData {
srcModifiedFileLength = srcMmetadata.size;
@test:Config {dependsOn: [testCopyFileToNonExistentFileReplaceFalse]}
function testCopyFileToExistingFileReplaceFalse() {
error? copyResult = copy(srcFile, tmpdir + copyFile);
if copyResult is error {
string expectedErrMsg = "The target file already exists";
test:assertTrue(copyResult.message().includes(expectedErrMsg));
} else {
test:assertFail("Error retrieving source file size!");
test:assertFail("File copy succeeded to an existing file when replace false!");
}
}

error? copyResult = copy(srcModifiedFile, tmpdir + copyFile);
@test:Config {dependsOn: [testCopyFileToExistingFileReplaceFalse]}
function testCopyFileToExistingFileReplaceTrue() {
MetaData|error srcMetadata = getMetaData(srcModifiedFile);
if srcMetadata is MetaData {
srcModifiedFileLength = srcMetadata.size;
} else {
test:assertFail("Error retrieving source file size!");
}
error? copyResult = copy(srcModifiedFile, tmpdir + copyFile, REPLACE_EXISTING);
if copyResult is error {
test:assertFail("File not copied!");
} else {
MetaData|error metadata = getMetaData(tmpdir + copyFile);
if metadata is MetaData {
int destFileLength = metadata.size;
test:assertEquals(destFileLength, srcFileLength, "File size mismatch!");
test:assertNotEquals(destFileLength, srcModifiedFileLength);
test:assertEquals(destFileLength, srcModifiedFileLength, "File size mismatch!");
test:assertNotEquals(destFileLength, srcFileLength);
error? removeResult = remove(tmpdir + copyFile);
if removeResult is error {
test:assertFail("Error removing test resource!");
}
} else {
test:assertFail("Error retrieving destination file size!");
}
}
}

@test:Config {dependsOn: [testCopyFileReplaceFalse]}
function testCopyFileReplaceTrue() {
error? copyResult = copy(srcModifiedFile, tmpdir + copyFile, REPLACE_EXISTING);
@test:Config {dependsOn: [testCopyFileToExistingFileReplaceTrue]}
function testCopyFileToNonExistentFileReplaceTrue() {
error? copyResult = copy(srcFile, tmpdir + copyFile, REPLACE_EXISTING);
if copyResult is error {
test:assertFail("File not copied!");
} else {
MetaData|error metadata = getMetaData(tmpdir + copyFile);
if metadata is MetaData {
int destFileLength = metadata.size;
test:assertEquals(destFileLength, srcModifiedFileLength, "File size mismatch!");
test:assertNotEquals(destFileLength, srcFileLength);
test:assertEquals(destFileLength, srcFileLength, "File size mismatch!");
error? removeResult = remove(tmpdir + copyFile);
if removeResult is error {
test:assertFail("Error removing test resource!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ public static Object copy(BString sourcePath, BString destinationPath, BString..
} catch (NoSuchFileException ex) {
return FileUtils.getBallerinaError(FileConstants.FILE_NOT_FOUND_ERROR,
"The target directory does not exist: " + ex.getMessage());
} catch (FileAlreadyExistsException ex) {
return FileUtils.getBallerinaError(FileConstants.INVALID_OPERATION_ERROR,
"The target file already exists: " + ex.getMessage());
} catch (IOException ex) {
return FileUtils.getBallerinaError(FileConstants.FILE_SYSTEM_ERROR,
"An error occurred when copying the file/s: " + ex.getMessage());
Expand Down Expand Up @@ -335,7 +338,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
Path newFile = target.resolve(source.relativize(file));
try {
Files.copy(file, newFile, copyOptions);
} catch (NoSuchFileException e) {
} catch (IOException e) {
throw e;
} catch (Exception e) {
log.debug(e.getMessage());
Expand Down