From 3da8a07892075123839d794bdfebbaf0822295ba Mon Sep 17 00:00:00 2001 From: Satyajit Behera <105061492+satyazzz123@users.noreply.github.com> Date: Fri, 5 Jan 2024 15:06:14 +0530 Subject: [PATCH] added additional check and fixed comment (#1125) Signed-off-by: satyazzz123 --- filesystem/merger_test.go | 57 ++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/filesystem/merger_test.go b/filesystem/merger_test.go index 097b1a253..38b5b4e69 100644 --- a/filesystem/merger_test.go +++ b/filesystem/merger_test.go @@ -16,9 +16,9 @@ package filesystem import ( + "io/ioutil" "os" "testing" - "io/ioutil" ) func TestMergeDeletionCallBack(t *testing.T) { @@ -40,13 +40,59 @@ func TestMergeDeletionCallBack(t *testing.T) { t.Fatalf("Expected source file to not exist, but got error: %v", err) } - _, destErr := os.Stat(destination) if !os.IsNotExist(destErr) { t.Error("Expected destination directory not to exist, but it exists") } - + }) + t.Run("test for scenario which checks for permissions of the destination directory to match the permissions of the source file when both the source file and destination directory exist", func(t *testing.T) { + sourceDir, err := ioutil.TempDir("", "sourceDir") + if err != nil { + t.Fatalf("Failed to create source directory: %v", err) + } + defer os.RemoveAll(sourceDir) + + // Create a temporary file within the source directory + sourceFile, err := ioutil.TempFile(sourceDir, "sourceFile.txt") + if err != nil { + t.Fatalf("Failed to create source file: %v", err) + } + defer os.Remove(sourceFile.Name()) + + // Create a temporary destination directory + destinationDir, err := ioutil.TempDir("", "destinationDir") + if err != nil { + t.Fatalf("Failed to create destination directory: %v", err) + } + defer os.RemoveAll(destinationDir) + + // Call the mergedeletion callback function + err = mergeDeletionCallBack(sourceFile.Name(), destinationDir, nil) + if err != nil { + t.Fatalf("Unexpected error during merge: %v", err) + } + + // Check if the destination directory exists + destInfo, destErr := os.Stat(destinationDir) + if destErr != nil { + t.Fatalf("Failed to get destination directory info: %v", destErr) + } + + // Checking if the destination is indeed a directory or not + if !destInfo.IsDir() { + t.Fatalf("Expected destination to be a directory but it is not.") + } + + // Check if the destination directory permissions match the source file + sourceFileInfo, sourceFileErr := os.Stat(sourceFile.Name()) + if sourceFileErr != nil { + t.Fatalf("Failed to get source file info: %v", sourceFileErr) + } + + if destInfo.Mode().Perm() != sourceFileInfo.Mode().Perm() { + t.Errorf("Expected destination directory permissions to be %s, but got %s", sourceFileInfo.Mode().Perm(), destInfo.Mode().Perm()) + } }) } @@ -60,7 +106,7 @@ func TestMergeProcessFileCallBack_SameContent(t *testing.T) { } defer os.Remove(sourceFile.Name()) - destinationFile, err := ioutil.TempFile(nonExistentPath , "destination") + destinationFile, err := ioutil.TempFile(nonExistentPath, "destination") if err != nil { t.Fatalf("Failed to create destination file: %v", err) } @@ -77,7 +123,6 @@ func TestMergeProcessFileCallBack_SameContent(t *testing.T) { t.Fatalf("Failed to write to destination file: %v", err) } - err = mergeProcessFileCallBack(sourceFile.Name(), destinationFile.Name(), false) if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -92,4 +137,4 @@ func TestMergeProcessFileCallBack_SameContent(t *testing.T) { t.Errorf("Destination file content should not be updated") } }) -} \ No newline at end of file +}