|
| 1 | +//go:build unix |
| 2 | + |
| 3 | +package testing |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// Issue 125: path builtins should expand a leading "~" consistently, matching |
| 13 | +// shell behavior. These tests point $HOME at a temp dir (os.UserHomeDir reads |
| 14 | +// $HOME on unix) so "~" resolves there without touching the real home. |
| 15 | + |
| 16 | +func Test_PathTilde_WriteReadDeleteRoundTrip(t *testing.T) { |
| 17 | + tmpHome := t.TempDir() |
| 18 | + t.Setenv("HOME", tmpHome) |
| 19 | + |
| 20 | + script := ` |
| 21 | +w = write_file("~/issue125.txt", "hi there") |
| 22 | +print(w.path) |
| 23 | +print(read_file("~/issue125.txt").content) |
| 24 | +print(delete_path("~/issue125.txt")) |
| 25 | +` |
| 26 | + setupAndRunCode(t, script, "--color=never") |
| 27 | + expected := fmt.Sprintf("%s/issue125.txt\nhi there\ntrue\n", filepath.ToSlash(tmpHome)) |
| 28 | + assertOnlyOutput(t, stdOutBuffer, expected) |
| 29 | + assertNoErrors(t) |
| 30 | + |
| 31 | + if _, err := os.Stat(filepath.Join(tmpHome, "issue125.txt")); !os.IsNotExist(err) { |
| 32 | + t.Errorf("expected file to be deleted, stat err = %v", err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// The headline inconsistency from the issue: get_path("~/...").exists was false |
| 37 | +// even when full_path pointed at a real file. |
| 38 | +func Test_PathTilde_GetPathExists(t *testing.T) { |
| 39 | + tmpHome := t.TempDir() |
| 40 | + t.Setenv("HOME", tmpHome) |
| 41 | + |
| 42 | + target := filepath.Join(tmpHome, "exists_check.txt") |
| 43 | + if err := os.WriteFile(target, []byte("x"), 0644); err != nil { |
| 44 | + t.Fatalf("failed to seed file: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + script := ` |
| 48 | +p = get_path("~/exists_check.txt") |
| 49 | +print(p.exists) |
| 50 | +print(p.full_path) |
| 51 | +` |
| 52 | + setupAndRunCode(t, script, "--color=never") |
| 53 | + expected := fmt.Sprintf("true\n%s/exists_check.txt\n", filepath.ToSlash(tmpHome)) |
| 54 | + assertOnlyOutput(t, stdOutBuffer, expected) |
| 55 | + assertNoErrors(t) |
| 56 | +} |
| 57 | + |
| 58 | +func Test_PathTilde_FindPaths(t *testing.T) { |
| 59 | + tmpHome := t.TempDir() |
| 60 | + t.Setenv("HOME", tmpHome) |
| 61 | + |
| 62 | + if err := os.WriteFile(filepath.Join(tmpHome, "a.txt"), []byte("a"), 0644); err != nil { |
| 63 | + t.Fatalf("failed to seed file: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + script := ` |
| 67 | +print(find_paths("~")) |
| 68 | +` |
| 69 | + setupAndRunCode(t, script, "--color=never") |
| 70 | + assertOnlyOutput(t, stdOutBuffer, "[ \"a.txt\" ]\n") |
| 71 | + assertNoErrors(t) |
| 72 | +} |
0 commit comments