| 727 | } |
| 728 | |
| 729 | func TestTextEdit(t *testing.T) { |
| 730 | if testing.Short() { |
| 731 | t.Skipf("Skipping in short mode") |
| 732 | } |
| 733 | if runtime.GOOS != "darwin" { |
| 734 | t.Skipf("Skipping Darwin-specific test.") |
| 735 | } |
| 736 | condSkip(t) |
| 737 | inEmptyMutDir(t, func(env *mountEnv, testDir string) { |
| 738 | var ( |
| 739 | testFile = filepath.Join(testDir, "some-text-file.txt") |
| 740 | content1 = []byte("Some text content.") |
| 741 | content2 = []byte("Some replacement content.") |
| 742 | ) |
| 743 | if err := os.WriteFile(testFile, content1, 0644); err != nil { |
| 744 | t.Fatal(err) |
| 745 | } |
| 746 | |
| 747 | cmd := exec.Command("osascript") |
| 748 | script := fmt.Sprintf(` |
| 749 | tell application "TextEdit" |
| 750 | activate |
| 751 | open POSIX file %q |
| 752 | tell front document |
| 753 | set paragraph 1 to %q as text |
| 754 | save |
| 755 | close |
| 756 | end tell |
| 757 | end tell |
| 758 | `, testFile, content2) |
| 759 | cmd.Stdin = strings.NewReader(script) |
| 760 | |
| 761 | if out, err := cmd.CombinedOutput(); err != nil { |
| 762 | t.Fatalf("Error running AppleScript: %v, %s", err, out) |
| 763 | } else { |
| 764 | t.Logf("AppleScript said: %q", out) |
| 765 | } |
| 766 | |
| 767 | fi, err := os.Stat(testFile) |
| 768 | if err != nil { |
| 769 | t.Errorf("Stat = %v, %v", fi, err) |
| 770 | } else if fi.Size() != int64(len(content2)) { |
| 771 | t.Errorf("Stat size = %d; want %d", fi.Size(), len(content2)) |
| 772 | } |
| 773 | slurp, err := os.ReadFile(testFile) |
| 774 | if err != nil { |
| 775 | t.Fatalf("ReadFile: %v", err) |
| 776 | } |
| 777 | if !bytes.Equal(slurp, content2) { |
| 778 | t.Errorf("File = %q; want %q", slurp, content2) |
| 779 | } |
| 780 | }) |
| 781 | } |
| 782 | |
| 783 | func not(cond func() bool) func() bool { |
| 784 | return func() bool { |