summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/examples.go3
-rw-r--r--internal/resource/dir/dir.go5
-rw-r--r--internal/resource/dir/dir_test.go22
-rw-r--r--internal/resource/file/file.go5
-rw-r--r--internal/resource/file/file_test.go19
-rw-r--r--internal/resource/link/link.go5
-rw-r--r--internal/resource/link/link_test.go23
7 files changed, 82 insertions, 0 deletions
diff --git a/examples/examples.go b/examples/examples.go
index 3fe2d50..6b97ab3 100644
--- a/examples/examples.go
+++ b/examples/examples.go
@@ -33,6 +33,7 @@ func Run() error {
// 7. Ensuring something is absent
file.Have("/tmp/gonf_old.txt", file.IsAbsent())
+ file.Absent("/tmp/gonf_old.txt") // Alternative way
// 8. A directory tree copied from source, reconciled, with a distinct
// file mode from the directory's own mode
@@ -49,6 +50,7 @@ func Run() error {
// something real to demonstrate.
_ = os.MkdirAll("/tmp/gonf_stale_dir/nested", 0o755)
dir.Have("/tmp/gonf_stale_dir", dir.IsAbsent(), dir.WithPrune())
+ dir.Absent("/tmp/gonf_stale_dir", dir.WithPrune()) // Alternative way
// 10. Non-recursively removing an empty directory
_ = os.Mkdir("/tmp/gonf_stale_empty_dir", 0o755)
@@ -57,6 +59,7 @@ func Run() error {
// 11. Ensuring a symlink is absent
_ = os.Symlink("/tmp/gonf_hello.txt", "/tmp/gonf_stale_link")
link.Have("/tmp/gonf_stale_link", link.IsAbsent())
+ link.Absent("/tmp/gonf_stale_link") // Alternative way
// 12. Ensuring a hardlink is absent
_ = os.Link("/tmp/gonf_hello.txt", "/tmp/gonf_stale_hardlink")
diff --git a/internal/resource/dir/dir.go b/internal/resource/dir/dir.go
index 0024c6c..9c34486 100644
--- a/internal/resource/dir/dir.go
+++ b/internal/resource/dir/dir.go
@@ -222,3 +222,8 @@ 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...)
+}
diff --git a/internal/resource/dir/dir_test.go b/internal/resource/dir/dir_test.go
index b93564a..ca01535 100644
--- a/internal/resource/dir/dir_test.go
+++ b/internal/resource/dir/dir_test.go
@@ -99,6 +99,28 @@ func TestHaveAbsentPruneDirectoryRecursive(t *testing.T) {
}
}
+func TestAbsentPruneDirectoryRecursive(t *testing.T) {
+ tmp := t.TempDir()
+ target := filepath.Join(tmp, "d")
+ if err := os.MkdirAll(filepath.Join(target, "sub", "deep"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(target, "sub", "f.txt"), []byte("x"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ Absent(target, WithPrune())
+ if _, err := os.Stat(target); !os.IsNotExist(err) {
+ t.Errorf("expected %s to be removed recursively", target)
+ }
+
+ // Idempotent: removing a missing tree is not an error.
+ d := &Dir{path: target, absent: true, prune: true}
+ if err := ensureAbsent(d); err != nil {
+ t.Fatalf("prune on missing tree: %v", err)
+ }
+}
+
func TestHaveDirectoryWithSource(t *testing.T) {
t.Run("recursive copy", func(t *testing.T) {
tmp := t.TempDir()
diff --git a/internal/resource/file/file.go b/internal/resource/file/file.go
index b3c1e44..257c407 100644
--- a/internal/resource/file/file.go
+++ b/internal/resource/file/file.go
@@ -244,3 +244,8 @@ 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...)
+}
diff --git a/internal/resource/file/file_test.go b/internal/resource/file/file_test.go
index 662aee7..b3b7711 100644
--- a/internal/resource/file/file_test.go
+++ b/internal/resource/file/file_test.go
@@ -182,6 +182,25 @@ func TestHaveAbsent(t *testing.T) {
}
}
+func TestAbsent(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "gone.txt")
+ if err := os.WriteFile(path, []byte("bye"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ Absent(path)
+ if _, err := os.Stat(path); !os.IsNotExist(err) {
+ t.Errorf("expected %s to be removed", path)
+ }
+
+ // Idempotent: removing a missing file is not an error (direct call to
+ // avoid duplicate registration in the one-per-process registry).
+ if err := ensureAbsent(path); err != nil {
+ t.Fatalf("absent on missing file: %v", err)
+ }
+}
+
func TestResolveStripsTmplSuffixWhenSourceHasTmplSuffix(t *testing.T) {
dir := t.TempDir()
sourcePath := filepath.Join(dir, "foo.conf.tmpl")
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")