mugit/internal/git/tree_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com test: add missing tests (#5)..., 2 months ago
olexsmir@gmail.com test: add missing tests (#5)..., 2 months ago
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "olexsmir.xyz/x/is" |
| 7 | ) |
| 8 | |
| 9 | func TestRepo_FileTree(t *testing.T) { |
| 10 | t.Run("root tree", func(t *testing.T) { |
| 11 | r := newTestRepo(t) |
| 12 | r.commitFile("README.md", "# Test", "Initial commit") |
| 13 | r.commitFile("src/main.go", "package main", "Add main.go") |
| 14 | r.commitFile("docs/guide.md", "# Guide", "Add guide") |
| 15 | |
| 16 | tree, err := r.open().FileTree(t.Context(), "") |
| 17 | is.Err(t, err, nil) |
| 18 | |
| 19 | names := make(map[string]bool) |
| 20 | for _, entry := range tree { |
| 21 | names[entry.Name] = true |
| 22 | } |
| 23 | is.Equal(t, names["README.md"], true) |
| 24 | is.Equal(t, names["src"], true) |
| 25 | is.Equal(t, names["docs"], true) |
| 26 | }) |
| 27 | |
| 28 | t.Run("subdirectory", func(t *testing.T) { |
| 29 | r := newTestRepo(t) |
| 30 | r.commitFile("src/main.go", "package main", "Add main.go") |
| 31 | r.commitFile("src/util/helper.go", "package util", "Add helper") |
| 32 | |
| 33 | tree, err := r.open().FileTree(t.Context(), "src") |
| 34 | is.Err(t, err, nil) |
| 35 | |
| 36 | names := make(map[string]bool) |
| 37 | for _, entry := range tree { |
| 38 | names[entry.Name] = true |
| 39 | } |
| 40 | |
| 41 | is.Equal(t, names["main.go"], true) |
| 42 | is.Equal(t, names["util"], true) |
| 43 | }) |
| 44 | |
| 45 | t.Run("distinguishes files and directories", func(t *testing.T) { |
| 46 | r := newTestRepo(t) |
| 47 | r.commitFile("file.txt", "content", "Add file") |
| 48 | r.commitFile("dir/nested.txt", "nested", "Add nested") |
| 49 | |
| 50 | tree, err := r.open().FileTree(t.Context(), "") |
| 51 | is.Err(t, err, nil) |
| 52 | for _, entry := range tree { |
| 53 | switch entry.Name { |
| 54 | case "file.txt": |
| 55 | is.Equal(t, entry.IsFile, true) |
| 56 | is.Equal(t, entry.Commit.Message, "Add file") |
| 57 | case "dir": |
| 58 | is.Equal(t, entry.IsFile, false) |
| 59 | is.Equal(t, entry.Commit.Message, "Add nested") |
| 60 | } |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | t.Run("includes file sizes", func(t *testing.T) { |
| 65 | r := newTestRepo(t) |
| 66 | content := "Hello, World!" |
| 67 | r.commitFile("hello.txt", content, "Add hello") |
| 68 | |
| 69 | tree, err := r.open().FileTree(t.Context(), "") |
| 70 | is.Err(t, err, nil) |
| 71 | for _, entry := range tree { |
| 72 | if entry.Name == "hello.txt" { |
| 73 | is.Equal(t, entry.Size, int64(len(content))) |
| 74 | is.Equal(t, entry.Commit.Message, "Add hello") |
| 75 | } |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | func TestRepo_FileContent(t *testing.T) { |
| 81 | t.Run("returns text file content", func(t *testing.T) { |
| 82 | r := newTestRepo(t) |
| 83 | r.commitFile("hello.txt", "Hello, World!", "Add hello") |
| 84 | |
| 85 | fc, err := r.open().FileContent("hello.txt") |
| 86 | is.Err(t, err, nil) |
| 87 | is.Equal(t, fc.String(), "Hello, World!") |
| 88 | is.Equal(t, fc.IsBinary, false) |
| 89 | is.Equal(t, fc.IsImage, false) |
| 90 | }) |
| 91 | |
| 92 | t.Run("returns file in subdirectory", func(t *testing.T) { |
| 93 | r := newTestRepo(t) |
| 94 | r.commitFile("lua/hello.lua", `vim.print "hi"`, "add stuff") |
| 95 | |
| 96 | fc, err := r.open().FileContent("lua/hello.lua") |
| 97 | is.Err(t, err, nil) |
| 98 | is.Equal(t, fc.String(), `vim.print "hi"`) |
| 99 | }) |
| 100 | |
| 101 | t.Run("returns ErrFileNotFound for missing file", func(t *testing.T) { |
| 102 | r := newTestRepo(t) |
| 103 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 104 | |
| 105 | _, err := r.open().FileContent("nonexistent.txt") |
| 106 | is.Err(t, err, ErrFileNotFound) |
| 107 | }) |
| 108 | |
| 109 | t.Run("detects mime type from extension", func(t *testing.T) { |
| 110 | r := newTestRepo(t) |
| 111 | r.commitFile("style.css", "body { color: red; }", "Add css") |
| 112 | r.commitFile("script.js", "console.log('hi')", "Add js") |
| 113 | |
| 114 | repo := r.open() |
| 115 | css, err := repo.FileContent("style.css") |
| 116 | is.Equal(t, css.Mime, "text/css; charset=utf-8") |
| 117 | is.Err(t, err, nil) |
| 118 | |
| 119 | js, err := repo.FileContent("script.js") |
| 120 | is.Equal(t, js.Mime, "text/javascript; charset=utf-8") |
| 121 | is.Err(t, err, nil) |
| 122 | }) |
| 123 | |
| 124 | t.Run("defaults to text/plain for unknown extension", func(t *testing.T) { |
| 125 | r := newTestRepo(t) |
| 126 | r.commitFile("data.mugitunknown", "some data", "Add data") |
| 127 | |
| 128 | fc, err := r.open().FileContent("data.mugitunknown") |
| 129 | is.Equal(t, fc.Mime, "text/plain") |
| 130 | is.Err(t, err, nil) |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | func TestFileContent_String(t *testing.T) { |
| 135 | t.Run("returns content for text", func(t *testing.T) { |
| 136 | is.Equal(t, (&FileContent{ |
| 137 | Content: []byte("hello"), |
| 138 | IsBinary: false, |
| 139 | IsImage: false, |
| 140 | }).String(), "hello") |
| 141 | }) |
| 142 | |
| 143 | t.Run("returns empty for binary and images", func(t *testing.T) { |
| 144 | is.Equal(t, (&FileContent{ |
| 145 | Content: []byte("binary-data"), |
| 146 | IsBinary: true, |
| 147 | IsImage: false, |
| 148 | }).String(), "") |
| 149 | |
| 150 | is.Equal(t, (&FileContent{ |
| 151 | Content: []byte("image data"), |
| 152 | IsBinary: false, |
| 153 | IsImage: true, |
| 154 | }).String(), "") |
| 155 | }) |
| 156 | } |