mugit/internal/git/config_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 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "olexsmir.xyz/x/is" |
| 9 | ) |
| 10 | |
| 11 | func TestRepo_IsPrivate(t *testing.T) { |
| 12 | t.Run("default is not private", func(t *testing.T) { |
| 13 | tr := newTestRepo(t) |
| 14 | private, err := tr.open().IsPrivate() |
| 15 | is.Equal(t, private, false) |
| 16 | is.Err(t, err, nil) |
| 17 | }) |
| 18 | |
| 19 | t.Run("set to private", func(t *testing.T) { |
| 20 | r := newTestRepo(t).open() |
| 21 | is.Err(t, r.SetPrivate(true), nil) |
| 22 | |
| 23 | private, err := r.IsPrivate() |
| 24 | is.Err(t, err, nil) |
| 25 | is.Equal(t, private, true) |
| 26 | }) |
| 27 | |
| 28 | t.Run("can be set back to public", func(t *testing.T) { |
| 29 | r := newTestRepo(t).open() |
| 30 | |
| 31 | is.Err(t, r.SetPrivate(true), nil) |
| 32 | is.Err(t, r.SetPrivate(false), nil) |
| 33 | |
| 34 | private, err := r.IsPrivate() |
| 35 | is.Equal(t, private, false) |
| 36 | is.Err(t, err, nil) |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | func TestRepo_Description(t *testing.T) { |
| 41 | t.Run("default description is empty description", func(t *testing.T) { |
| 42 | r := newTestRepo(t) |
| 43 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 44 | |
| 45 | descPath := filepath.Join(r.path, ".git", "description") |
| 46 | _ = os.WriteFile(descPath, []byte(defaultDescription), 0o644) |
| 47 | |
| 48 | desc, err := r.open().Description() |
| 49 | is.Err(t, err, nil) |
| 50 | is.Equal(t, desc, "") |
| 51 | }) |
| 52 | |
| 53 | t.Run("empty description returns empty string", func(t *testing.T) { |
| 54 | r := newTestRepo(t) |
| 55 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 56 | |
| 57 | desc, err := r.open().Description() |
| 58 | is.Err(t, err, nil) |
| 59 | is.Equal(t, desc, "") |
| 60 | }) |
| 61 | |
| 62 | t.Run("default git description is treated as empty", func(t *testing.T) { |
| 63 | r := newTestRepo(t) |
| 64 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 65 | |
| 66 | // Write the default git description to the .git directory |
| 67 | descPath := filepath.Join(r.path, ".git", "description") |
| 68 | err := os.WriteFile(descPath, []byte(defaultDescription), 0o644) |
| 69 | is.Err(t, err, nil) |
| 70 | |
| 71 | desc, err := r.open().Description() |
| 72 | is.Err(t, err, nil) |
| 73 | is.Equal(t, desc, "") |
| 74 | }) |
| 75 | |
| 76 | t.Run("set and get description", func(t *testing.T) { |
| 77 | r := newTestRepo(t) |
| 78 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 79 | |
| 80 | repo := r.open() |
| 81 | err := repo.SetDescription("My awesome project") |
| 82 | is.Err(t, err, nil) |
| 83 | |
| 84 | desc, err := repo.Description() |
| 85 | is.Err(t, err, nil) |
| 86 | is.Equal(t, desc, "My awesome project") |
| 87 | }) |
| 88 | |
| 89 | t.Run("description with newlines", func(t *testing.T) { |
| 90 | r := newTestRepo(t) |
| 91 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 92 | |
| 93 | repo := r.open() |
| 94 | multiLine := "My project\n\nWith multiple lines\nof description." |
| 95 | err := repo.SetDescription(multiLine) |
| 96 | is.Err(t, err, nil) |
| 97 | |
| 98 | desc, err := repo.Description() |
| 99 | is.Err(t, err, nil) |
| 100 | is.Equal(t, desc, multiLine) |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | func TestRepo_Mirror(t *testing.T) { |
| 105 | t.Run("repo without origin remote can't be a mirror", func(t *testing.T) { |
| 106 | r := newTestRepo(t) |
| 107 | r.commitFile("dummy.txt", "dummy", "Initial commit") |
| 108 | |
| 109 | _, err := r.open().IsMirror() |
| 110 | is.Err(t, err, "failed to get remote: ") |
| 111 | }) |
| 112 | |
| 113 | t.Run("set mirror remote", func(t *testing.T) { |
| 114 | r := newTestRepo(t).open() |
| 115 | |
| 116 | expectedURL := "https://github.com/example/repo.git" |
| 117 | err := r.SetMirrorRemote(expectedURL) |
| 118 | is.Err(t, err, nil) |
| 119 | |
| 120 | isMirror, err := r.IsMirror() |
| 121 | is.Equal(t, isMirror, true) |
| 122 | is.Err(t, err, nil) |
| 123 | |
| 124 | url, err := r.RemoteURL() |
| 125 | is.Err(t, err, nil) |
| 126 | is.Equal(t, url, expectedURL) |
| 127 | }) |
| 128 | } |