Skip to content

Commit c48c500

Browse files
authored
Merge commit from fork
Updating link handling
2 parents 563b094 + 00de613 commit c48c500

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

pkg/downloader/manager.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,20 @@ func writeLock(chartpath string, lock *chart.Lock, legacyLockfile bool) error {
852852
lockfileName = "requirements.lock"
853853
}
854854
dest := filepath.Join(chartpath, lockfileName)
855+
856+
info, err := os.Lstat(dest)
857+
if err != nil && !os.IsNotExist(err) {
858+
return fmt.Errorf("error getting info for %q: %w", dest, err)
859+
} else if err == nil {
860+
if info.Mode()&os.ModeSymlink != 0 {
861+
link, err := os.Readlink(dest)
862+
if err != nil {
863+
return fmt.Errorf("error reading symlink for %q: %w", dest, err)
864+
}
865+
return fmt.Errorf("the %s file is a symlink to %q", lockfileName, link)
866+
}
867+
}
868+
855869
return os.WriteFile(dest, data, 0644)
856870
}
857871

pkg/downloader/manager_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ import (
2121
"path/filepath"
2222
"reflect"
2323
"testing"
24+
"time"
2425

26+
"github.com/stretchr/testify/assert"
2527
"helm.sh/helm/v3/pkg/chart"
2628
"helm.sh/helm/v3/pkg/chart/loader"
2729
"helm.sh/helm/v3/pkg/chartutil"
2830
"helm.sh/helm/v3/pkg/getter"
2931
"helm.sh/helm/v3/pkg/repo/repotest"
32+
"sigs.k8s.io/yaml"
3033
)
3134

3235
func TestVersionEquals(t *testing.T) {
@@ -598,3 +601,94 @@ func TestKey(t *testing.T) {
598601
}
599602
}
600603
}
604+
605+
func TestWriteLock(t *testing.T) {
606+
fixedTime, err := time.Parse(time.RFC3339, "2025-07-04T00:00:00Z")
607+
assert.NoError(t, err)
608+
lock := &chart.Lock{
609+
Generated: fixedTime,
610+
Digest: "sha256:12345",
611+
Dependencies: []*chart.Dependency{
612+
{
613+
Name: "fantastic-chart",
614+
Version: "1.2.3",
615+
Repository: "https://example.com/charts",
616+
},
617+
},
618+
}
619+
expectedContent, err := yaml.Marshal(lock)
620+
assert.NoError(t, err)
621+
622+
t.Run("v2 lock file", func(t *testing.T) {
623+
dir := t.TempDir()
624+
err := writeLock(dir, lock, false)
625+
assert.NoError(t, err)
626+
627+
lockfilePath := filepath.Join(dir, "Chart.lock")
628+
_, err = os.Stat(lockfilePath)
629+
assert.NoError(t, err, "Chart.lock should exist")
630+
631+
content, err := os.ReadFile(lockfilePath)
632+
assert.NoError(t, err)
633+
assert.Equal(t, expectedContent, content)
634+
635+
// Check that requirements.lock does not exist
636+
_, err = os.Stat(filepath.Join(dir, "requirements.lock"))
637+
assert.Error(t, err)
638+
assert.True(t, os.IsNotExist(err))
639+
})
640+
641+
t.Run("v1 lock file", func(t *testing.T) {
642+
dir := t.TempDir()
643+
err := writeLock(dir, lock, true)
644+
assert.NoError(t, err)
645+
646+
lockfilePath := filepath.Join(dir, "requirements.lock")
647+
_, err = os.Stat(lockfilePath)
648+
assert.NoError(t, err, "requirements.lock should exist")
649+
650+
content, err := os.ReadFile(lockfilePath)
651+
assert.NoError(t, err)
652+
assert.Equal(t, expectedContent, content)
653+
654+
// Check that Chart.lock does not exist
655+
_, err = os.Stat(filepath.Join(dir, "Chart.lock"))
656+
assert.Error(t, err)
657+
assert.True(t, os.IsNotExist(err))
658+
})
659+
660+
t.Run("overwrite existing lock file", func(t *testing.T) {
661+
dir := t.TempDir()
662+
lockfilePath := filepath.Join(dir, "Chart.lock")
663+
assert.NoError(t, os.WriteFile(lockfilePath, []byte("old content"), 0644))
664+
665+
err = writeLock(dir, lock, false)
666+
assert.NoError(t, err)
667+
668+
content, err := os.ReadFile(lockfilePath)
669+
assert.NoError(t, err)
670+
assert.Equal(t, expectedContent, content)
671+
})
672+
673+
t.Run("lock file is a symlink", func(t *testing.T) {
674+
dir := t.TempDir()
675+
dummyFile := filepath.Join(dir, "dummy.txt")
676+
assert.NoError(t, os.WriteFile(dummyFile, []byte("dummy"), 0644))
677+
678+
lockfilePath := filepath.Join(dir, "Chart.lock")
679+
assert.NoError(t, os.Symlink(dummyFile, lockfilePath))
680+
681+
err = writeLock(dir, lock, false)
682+
assert.Error(t, err)
683+
assert.Contains(t, err.Error(), "the Chart.lock file is a symlink to")
684+
})
685+
686+
t.Run("chart path is not a directory", func(t *testing.T) {
687+
dir := t.TempDir()
688+
filePath := filepath.Join(dir, "not-a-dir")
689+
assert.NoError(t, os.WriteFile(filePath, []byte("file"), 0644))
690+
691+
err = writeLock(filePath, lock, false)
692+
assert.Error(t, err)
693+
})
694+
}

0 commit comments

Comments
 (0)