Skip to content

Commit

Permalink
add symlink test case
Browse files Browse the repository at this point in the history
Signed-off-by: nitishfy <[email protected]>
  • Loading branch information
nitishfy committed Dec 16, 2024
1 parent 9866605 commit c013076
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
10 changes: 8 additions & 2 deletions internal/directives/file_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package directives
import (
"context"
"fmt"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"os"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/xeipuuv/gojsonschema"
"os"

kargoapi "github.com/akuity/kargo/api/v1alpha1"
)

func init() {
Expand Down Expand Up @@ -71,6 +73,10 @@ func removePath(path string) error {
return err
}

if fi.Mode()&os.ModeSymlink != 0 {
return os.Remove(path)
}

if fi.IsDir() {
return os.RemoveAll(path)
}
Expand Down
42 changes: 37 additions & 5 deletions internal/directives/file_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package directives

import (
"context"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

kargoapi "github.com/akuity/kargo/api/v1alpha1"
)

func Test_fileDeleter_runPromotionStep(t *testing.T) {
Expand All @@ -30,7 +32,7 @@ func Test_fileDeleter_runPromotionStep(t *testing.T) {
cfg: DeleteConfig{
Path: "input.txt",
},
assertions: func(t *testing.T, workDir string, result PromotionStepResult, err error) {
assertions: func(t *testing.T, _ string, result PromotionStepResult, err error) {
assert.NoError(t, err)
assert.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, result)

Expand Down Expand Up @@ -70,8 +72,38 @@ func Test_fileDeleter_runPromotionStep(t *testing.T) {
assert.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, result)
},
},
}
{
name: "removes symlink only",
setupFiles: func(t *testing.T) string {
tmpDir := t.TempDir()

inDir := filepath.Join(tmpDir, "input")
require.NoError(t, os.Mkdir(inDir, 0o755))

filePath := filepath.Join(inDir, "input.txt")
require.NoError(t, os.WriteFile(filePath, []byte("test content"), 0o600))

symlinkPath := filepath.Join(inDir, "symlink.txt")
require.NoError(t, os.Symlink("input.txt", symlinkPath))

return tmpDir
},
cfg: DeleteConfig{
Path: "input/symlink.txt",
},
assertions: func(t *testing.T, workDir string, result PromotionStepResult, err error) {
assert.NoError(t, err)
require.Equal(t, PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, result)

_, statErr := os.Stat(filepath.Join(workDir, "input.txt"))
assert.NoError(t, statErr)

_, statErr = os.Lstat(filepath.Join(workDir, "symlink.txt"))
assert.Error(t, statErr)
assert.True(t, os.IsNotExist(statErr))
},
},
}
runner := &fileDeleter{}

for _, tt := range tests {
Expand Down

0 comments on commit c013076

Please sign in to comment.