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

fix: replace deprecated ioutil readfile function #1075

Merged
merged 1 commit into from
Aug 21, 2023
Merged
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
182 changes: 90 additions & 92 deletions filesystem/replicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,102 +18,101 @@ package filesystem

import (
"bytes"
"io/ioutil"
"os"
"testing"
)
func TestReplicateProcessFileCallBack(t *testing.T) {
t.Run("test for source file does not exist, should return an error", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/non_existent_file.txt"
destinationFilePath := "testdata/replicator_test/destination.txt"
err := replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err == nil {
t.Errorf("Expected error for non-existent source file, got nil")
}
})
t.Run("test for destination file doesn't exist, should copy the source file", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/emptyfile.txt"
destinationFilePath := "testdata/replicator_test/destination_file.txt"
err := replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

os.Remove(destinationFilePath)
})
t.Run("test for destination file exists but is different, should copy the source file", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/source_file.txt"
destinationFilePath := "testdata/replicator_test/destination_file.txt"
sourceFile, err := os.Create(sourceFilePath)
if err != nil {
t.Fatal(err)
}
defer func() {
sourceFile.Close()
os.Remove(sourceFilePath)
}()
_, err = sourceFile.WriteString("hello, world!")
if err != nil {
t.Fatal(err)
}
destinationFile, err := os.Create(destinationFilePath)
if err != nil {
t.Fatal(err)
}
defer func() {
destinationFile.Close()
os.Remove(destinationFilePath)
}()
_, err = destinationFile.WriteString("hello, go!")
if err != nil {
t.Fatal(err)
}
err = replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}
// Check if the source file content matches the destination file content after copying
sourceContent, err := ioutil.ReadFile(sourceFilePath)
if err != nil {
t.Fatal(err)
}
destinationContent, err := ioutil.ReadFile(destinationFilePath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sourceContent, destinationContent) {
t.Errorf("Expected destination content to be equal to source content, but they are different.")
}
})
t.Run("test for destination file exists and is the same, should return nil without copying", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/source_same.txt"
destinationFilePath := "testdata/replicator_test/destination_same.txt"
// Store the destination file's modification time before calling replicateProcessFileCallBack

func TestReplicateProcessFileCallBack(t *testing.T) {
t.Run("test for source file does not exist, should return an error", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/non_existent_file.txt"
destinationFilePath := "testdata/replicator_test/destination.txt"
err := replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err == nil {
t.Errorf("Expected error for non-existent source file, got nil")
}
})

t.Run("test for destination file doesn't exist, should copy the source file", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/emptyfile.txt"
destinationFilePath := "testdata/replicator_test/destination_file.txt"

err := replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

os.Remove(destinationFilePath)
})

t.Run("test for destination file exists but is different, should copy the source file", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/source_file.txt"
destinationFilePath := "testdata/replicator_test/destination_file.txt"

sourceFile, err := os.Create(sourceFilePath)
if err != nil {
t.Fatal(err)
}
defer func() {
sourceFile.Close()
os.Remove(sourceFilePath)
}()

_, err = sourceFile.WriteString("hello, world!")
if err != nil {
t.Fatal(err)
}

destinationFile, err := os.Create(destinationFilePath)
if err != nil {
t.Fatal(err)
}
defer func() {
destinationFile.Close()
os.Remove(destinationFilePath)
}()

_, err = destinationFile.WriteString("hello, go!")
if err != nil {
t.Fatal(err)
}

err = replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

// Check if the source file content matches the destination file content after copying
sourceContent, err := os.ReadFile(sourceFilePath)
if err != nil {
t.Fatal(err)
}

destinationContent, err := os.ReadFile(destinationFilePath)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(sourceContent, destinationContent) {
t.Errorf("Expected destination content to be equal to source content, but they are different.")
}
})

t.Run("test for destination file exists and is the same, should return nil without copying", func(t *testing.T) {
sourceFilePath := "testdata/replicator_test/source_same.txt"
destinationFilePath := "testdata/replicator_test/destination_same.txt"

// Store the destination file's modification time before calling replicateProcessFileCallBack
info, err := os.Stat(destinationFilePath)
if err != nil {
t.Errorf("Destination file should exist, but got an error: %s", err)
}
originalModTime := info.ModTime()
err = replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}
originalModTime := info.ModTime()
err = replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

// Check if the destination file still exists after calling replicateProcessFileCallBack
// Check if the destination file still exists after calling replicateProcessFileCallBack
info, err = os.Stat(destinationFilePath)
if err != nil {
t.Errorf("Destination file should exist, but got an error: %s", err)
Expand All @@ -126,14 +125,13 @@ import (
}

// Check if the destination file content remains unchanged
destinationContent, err := ioutil.ReadFile(destinationFilePath)
destinationContent, err := os.ReadFile(destinationFilePath)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(destinationContent, []byte("hello, world!")) {
t.Errorf("Expected destination content to remain unchanged, but got different content.")
}
})
}

})
}
Loading