package platform import ( "os" "path/filepath" "strings" "testing" ) func TestEnsureBlockInsertsIntoMissingFile(t *testing.T) { dir := t.TempDir() p := filepath.Join(dir, "rc") if err := EnsureBlock(p, "archduino", "export FOO=1"); err != nil { t.Fatal(err) } got, _ := os.ReadFile(p) want := "# >>> archduino >>>\nexport FOO=1\n# <<< archduino <<<\n" if string(got) != want { t.Fatalf("got %q want %q", got, want) } } func TestEnsureBlockReplacesExisting(t *testing.T) { dir := t.TempDir() p := filepath.Join(dir, "rc") initial := "alias ll='ls -l'\n# >>> archduino >>>\nold content\n# <<< archduino <<<\necho tail\n" if err := os.WriteFile(p, []byte(initial), 0o644); err != nil { t.Fatal(err) } if err := EnsureBlock(p, "archduino", "new content"); err != nil { t.Fatal(err) } got, _ := os.ReadFile(p) want := "alias ll='ls -l'\n# >>> archduino >>>\nnew content\n# <<< archduino <<<\necho tail\n" if string(got) != want { t.Fatalf("got %q want %q", got, want) } } func TestEnsureBlockIdempotent(t *testing.T) { dir := t.TempDir() p := filepath.Join(dir, "rc") _ = EnsureBlock(p, "a", "x") before, _ := os.ReadFile(p) _ = EnsureBlock(p, "a", "x") after, _ := os.ReadFile(p) if string(before) != string(after) { t.Fatalf("not idempotent: %q vs %q", before, after) } } func TestHasBlockTrueFalse(t *testing.T) { dir := t.TempDir() p := filepath.Join(dir, "rc") has, err := HasBlock(p, "a") if err != nil { t.Fatal(err) } if has { t.Fatal("missing file should report has=false") } _ = os.WriteFile(p, []byte("no blocks here\n"), 0o644) has, _ = HasBlock(p, "a") if has { t.Fatal("unblocked file should report has=false") } _ = EnsureBlock(p, "a", "x") has, _ = HasBlock(p, "a") if !has { t.Fatal("blocked file should report has=true") } } func TestRemoveBlock(t *testing.T) { dir := t.TempDir() p := filepath.Join(dir, "rc") _ = os.WriteFile(p, []byte("head\n# >>> a >>>\nmid\n# <<< a <<<\ntail\n"), 0o644) if err := RemoveBlock(p, "a"); err != nil { t.Fatal(err) } got, _ := os.ReadFile(p) if strings.Contains(string(got), ">>> a >>>") { t.Fatalf("block not removed: %q", got) } if !strings.Contains(string(got), "head") || !strings.Contains(string(got), "tail") { t.Fatalf("surrounding content lost: %q", got) } }