summaryrefslogtreecommitdiff
path: root/internal/resource/link
diff options
context:
space:
mode:
Diffstat (limited to 'internal/resource/link')
-rw-r--r--internal/resource/link/link.go5
-rw-r--r--internal/resource/link/link_test.go23
2 files changed, 28 insertions, 0 deletions
diff --git a/internal/resource/link/link.go b/internal/resource/link/link.go
index 0a6b8e6..3133514 100644
--- a/internal/resource/link/link.go
+++ b/internal/resource/link/link.go
@@ -102,6 +102,11 @@ func Have(path string, opts ...Option) resource.Resource {
return res
}
+func Absent(path string, opts ...Option) resource.Resource {
+ opts = append(opts, IsAbsent())
+ return Have(path, opts...)
+}
+
func ensureAbsent(path string) error {
log.Printf("ensuring link absent: %s", path)
diff --git a/internal/resource/link/link_test.go b/internal/resource/link/link_test.go
index 8da674b..6f1b1bc 100644
--- a/internal/resource/link/link_test.go
+++ b/internal/resource/link/link_test.go
@@ -170,6 +170,29 @@ func TestHaveAbsentSymlink(t *testing.T) {
}
}
+func TestAbsentSymlink(t *testing.T) {
+ dir := t.TempDir()
+ target := filepath.Join(dir, "target.txt")
+ link := filepath.Join(dir, "link.txt")
+ if err := os.WriteFile(target, []byte("t"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink(target, link); err != nil {
+ t.Fatal(err)
+ }
+
+ Absent(link)
+ if _, err := os.Lstat(link); !os.IsNotExist(err) {
+ t.Errorf("expected %s to be removed", link)
+ }
+
+ // Idempotent: removing a missing link is not an error (direct call to
+ // avoid duplicate registration in the one-per-process registry).
+ if err := ensureAbsent(link); err != nil {
+ t.Fatalf("absent on missing link: %v", err)
+ }
+}
+
func TestHaveAbsentWithoutKindRegistersGenericLink(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "whatever")