| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "runtime" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestWorkspacePanelListsGenericTopLevelDirectories(t *testing.T) { |
| 11 | base := t.TempDir() |
| 12 | for _, dir := range []string{"tmp", "bin", "stage", "src", "node_modules", "dist"} { |
| 13 | if err := os.MkdirAll(filepath.Join(base, dir), 0o755); err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | } |
| 17 | if err := os.WriteFile(filepath.Join(base, "main.py"), []byte("x"), 0o644); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | |
| 21 | listed := map[string]bool{} |
| 22 | for _, e := range listDirForWorkspaceTarget(base, nil, "") { |
| 23 | listed[e.Name] = true |
| 24 | } |
| 25 | for _, want := range []string{"tmp", "bin", "stage", "src", "main.py"} { |
| 26 | if !listed[want] { |
| 27 | t.Errorf("file panel hides %q, a real workspace entry; listed %v", want, listed) |
| 28 | } |
| 29 | } |
| 30 | for _, hidden := range []string{"node_modules", "dist"} { |
| 31 | if listed[hidden] { |
| 32 | t.Errorf("file panel lists %q, which stays hidden as vendor/build output", hidden) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestWorkspaceWatchSeesChangesUnderGenericTopLevelDirectories(t *testing.T) { |
| 38 | root := t.TempDir() |
| 39 | for _, rel := range []string{"tmp/notes.py", "bin/run.sh", "stage/plan.md"} { |
| 40 | if workspaceWatchPathSkipped(root, filepath.Join(root, filepath.FromSlash(rel))) { |
| 41 | t.Errorf("a change to %s is dropped although the file panel shows it", rel) |
| 42 | } |
| 43 | } |
| 44 | if !workspaceWatchPathSkipped(root, filepath.Join(root, "node_modules", "x", "index.js")) { |
| 45 | t.Error("changes under node_modules must stay ignored") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // A link is listed as what it points at while that stays inside the |
| 50 | // workspace. One that leaves it, dangles, or loops back onto a folder it sits |
| 51 | // in is not offered, and cannot be opened or previewed by naming it. |
| 52 | func TestWorkspacePanelListsOnlyLinksInsideTheTree(t *testing.T) { |
| 53 | orig, _ := os.Getwd() |
| 54 | defer os.Chdir(orig) |
| 55 | base := t.TempDir() |
| 56 | elsewhere := t.TempDir() |
| 57 | if err := os.MkdirAll(filepath.Join(base, "docs"), 0o755); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | for _, file := range []string{filepath.Join(base, "docs", "a.md"), filepath.Join(elsewhere, "shared.md")} { |
| 61 | if err := os.WriteFile(file, []byte("x"), 0o644); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | } |
| 65 | links := map[string]string{ |
| 66 | "docs-link": filepath.Join(base, "docs"), |
| 67 | "notes.md": filepath.Join(base, "docs", "a.md"), |
| 68 | "out-link": elsewhere, |
| 69 | "out.md": filepath.Join(elsewhere, "shared.md"), |
| 70 | "dangling": filepath.Join(base, "gone"), |
| 71 | "loop": ".", |
| 72 | filepath.Join("docs", "up"): "..", |
| 73 | } |
| 74 | if runtime.GOOS != "windows" { |
| 75 | links["etc"] = "/etc" |
| 76 | } |
| 77 | for name, target := range links { |
| 78 | if err := os.Symlink(target, filepath.Join(base, name)); err != nil { |
| 79 | t.Skipf("cannot create a link here: %v", err) |
| 80 | } |
| 81 | } |
| 82 | if err := os.Chdir(base); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | |
| 86 | got := map[string]bool{} |
| 87 | for _, e := range (&App{}).ListDir("") { |
| 88 | got[e.Name] = e.IsDir |
| 89 | } |
| 90 | if isDir, ok := got["docs-link"]; !ok || !isDir { |
| 91 | t.Errorf("linked folder listed = %v, dir = %v; want a folder (%v)", ok, isDir, got) |
| 92 | } |
| 93 | if isDir, ok := got["notes.md"]; !ok || isDir { |
| 94 | t.Errorf("linked file listed = %v, dir = %v; want a file (%v)", ok, isDir, got) |
| 95 | } |
| 96 | for _, name := range []string{"out-link", "out.md", "etc", "dangling", "loop"} { |
| 97 | if _, ok := got[name]; ok { |
| 98 | t.Errorf("%s is listed: %v", name, got) |
| 99 | } |
| 100 | } |
| 101 | inner := (&App{}).ListDir("docs-link") |
| 102 | if len(inner) != 1 || inner[0].Name != "a.md" { |
| 103 | t.Errorf("opening the linked folder lists %v, want a.md", inner) |
| 104 | } |
| 105 | for _, e := range (&App{}).ListDir("docs") { |
| 106 | if e.Name == "up" { |
| 107 | t.Errorf("docs lists up, a link back onto its own parent") |
| 108 | } |
| 109 | } |
| 110 | for _, rel := range []string{"out-link", "etc"} { |
| 111 | if entries := (&App{}).ListDir(rel); len(entries) != 0 { |
| 112 | t.Errorf("ListDir(%q) = %d entries from outside the workspace", rel, len(entries)) |
| 113 | } |
| 114 | } |
| 115 | if preview := (&App{}).ReadFile("notes.md"); preview.Err != "" { |
| 116 | t.Errorf("preview of an in-tree link failed: %s", preview.Err) |
| 117 | } |
| 118 | for _, rel := range []string{"out.md", "out-link/shared.md", "etc/hosts"} { |
| 119 | if preview := (&App{}).ReadFile(rel); preview.Err == "" { |
| 120 | t.Errorf("ReadFile(%q) previewed a file outside the workspace", rel) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 |