Skip to content
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

test: add test for replicateAdditionCallback #1071

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions filesystem/replicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,38 @@ func TestReplicateProcessFileCallBack(t *testing.T) {
if !bytes.Equal(destinationContent, []byte("hello, world!")) {
t.Errorf("Expected destination content to remain unchanged, but got different content.")
}
})
}
func TestReplicateAdditionCallBack(t *testing.T) {
t.Run("test for destination dir is removed", func(t *testing.T) {
sourceDir := "testdata/source"
destinationDir := "testdata/destination"
destinationFilePath := destinationDir + "/existing-file.txt"
// Prepare the destination directory with an existing file
err := os.MkdirAll(destinationDir, 0755)
if err != nil {
t.Fatal(err)
}
_, err = os.Create(destinationFilePath)
if err != nil {
t.Fatal(err)
}

// Call the addition callback
err = replicateAdditionCallBack(sourceDir, destinationDir, nil)
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}

// Check if the destination directory is removed
_, err = os.Stat(destinationDir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check only for the destination directory? what about the files that were copied from source? Or the sub-directories that could have existed in the source?

if err != nil {
if os.IsExist(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the err is something else, you might want to log that as well, no?

t.Errorf("Expected destination directory to be removed, but it still exists, got error: %s", err)
}
} else {
t.Error("Expected destination directory to be removed, but it still exists")
}
})
}

1 change: 1 addition & 0 deletions filesystem/testdata/source/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file1