|
| 1 | +package ledger |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "fmt" |
| 6 | + "path" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPath(t *testing.T) { |
| 11 | + expected := path.Join(os.Getenv("HOME"), ".git-ledger") |
| 12 | + path := Path() |
| 13 | + if path != expected { |
| 14 | + t.Error( |
| 15 | + "For", "Path()", |
| 16 | + "expected", expected, |
| 17 | + "got", path, |
| 18 | + ) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestIsGitProject(t *testing.T) { |
| 23 | + dir := "/tmp" |
| 24 | + gitdir := path.Join(dir, ".git") |
| 25 | + os.MkdirAll(gitdir, os.ModePerm) |
| 26 | + result := IsGitProject(dir) |
| 27 | + if result != true { |
| 28 | + t.Error( |
| 29 | + "For", "IsGitProject(dir)", |
| 30 | + "expected", true, |
| 31 | + "got", result, |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + // remove dir and test for false |
| 37 | + os.Remove(gitdir) |
| 38 | + result = IsGitProject(dir) |
| 39 | + if result != false { |
| 40 | + t.Error( |
| 41 | + "For", "IsGitProject(dir)", |
| 42 | + "expected", false, |
| 43 | + "got", result, |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// TODO: add test for nil fields, see how the toml parses? |
| 49 | +func TestRecordString(t *testing.T) { |
| 50 | + path := "/path/to/nowhere" |
| 51 | + slug := "sclopio/peepio" |
| 52 | + record := Record{Path: path, Slug: slug} |
| 53 | + expected := fmt.Sprintf("[[Record]]\npath = \"%s\"\nslug = \"%s\"\n\n", record.Path, record.Slug) |
| 54 | + if record.String() != expected { |
| 55 | + t.Error( |
| 56 | + "For", "Record.String()", |
| 57 | + "expected", expected, |
| 58 | + "got", record.String(), |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Move the current git-ledger to a new location so testing can create |
| 64 | +// a temporary one |
| 65 | +func pushGitLedger() { |
| 66 | + os.Rename(Path(), fmt.Sprintf("%s.backup", Path())) |
| 67 | + f, _ := os.Create(Path()) |
| 68 | + defer f.Close() |
| 69 | +} |
| 70 | + |
| 71 | +// Remove the temporary git-ledger and replace it with the backup |
| 72 | +func popGitLedger() { |
| 73 | + os.Rename(fmt.Sprintf("%s.backup", Path()), Path()) |
| 74 | +} |
| 75 | + |
| 76 | +func TestGetRecordsEmpty( t *testing.T) { |
| 77 | + pushGitLedger() |
| 78 | + records, _ := GetRecords() |
| 79 | + if len(records) != 0 { |
| 80 | + t.Error( |
| 81 | + "For", "len(GetRecords())", |
| 82 | + "expected", 0, |
| 83 | + "got", len(records), |
| 84 | + ) |
| 85 | + } |
| 86 | + popGitLedger() |
| 87 | +} |
| 88 | + |
| 89 | +func TestAddToLedger(t *testing.T) { |
| 90 | + pushGitLedger() |
| 91 | + r := Record{Path: "/tmp", Slug: "pony/fill"} |
| 92 | + r.AddToLedger() |
| 93 | + records, _ := GetRecords() |
| 94 | + if len(records) != 1 { |
| 95 | + t.Error( |
| 96 | + "For", "len(GetRecords())", |
| 97 | + "expected", 1, |
| 98 | + "got", len(records), |
| 99 | + ) |
| 100 | + } |
| 101 | + s := Record{Path: "/home", Slug: "badger/bear"} |
| 102 | + s.AddToLedger() |
| 103 | + records, _ = GetRecords() |
| 104 | + if len(records) != 2 { |
| 105 | + t.Error( |
| 106 | + "For", "len(GetRecords())", |
| 107 | + "expected", 2, |
| 108 | + "got", len(records), |
| 109 | + ) |
| 110 | + } |
| 111 | + popGitLedger() |
| 112 | +} |
| 113 | + |
| 114 | +func TestRemoveFromLedger(t *testing.T) { |
| 115 | + pushGitLedger() |
| 116 | + r := Record{Path: "/tmp", Slug: "pony/fill"} |
| 117 | + r.AddToLedger() |
| 118 | + records, _ := GetRecords() |
| 119 | + if len(records) != 1 { |
| 120 | + t.Error( |
| 121 | + "For", "len(GetRecords())", |
| 122 | + "expected", 1, |
| 123 | + "got", len(records), |
| 124 | + ) |
| 125 | + } |
| 126 | + s := Record{Path: "/home", Slug: "badger/bear"} |
| 127 | + s.AddToLedger() |
| 128 | + records, _ = GetRecords() |
| 129 | + if len(records) != 2 { |
| 130 | + t.Error( |
| 131 | + "For", "len(GetRecords())", |
| 132 | + "expected", 2, |
| 133 | + "got", len(records), |
| 134 | + ) |
| 135 | + } |
| 136 | + s.RemoveFromLedger() |
| 137 | + records, _ = GetRecords() |
| 138 | + if len(records) != 1 { |
| 139 | + t.Error( |
| 140 | + "For", "len(GetRecords())", |
| 141 | + "expected", 1, |
| 142 | + "got", len(records), |
| 143 | + ) |
| 144 | + } |
| 145 | + r.RemoveFromLedger() |
| 146 | + records, _ = GetRecords() |
| 147 | + if len(records) != 0 { |
| 148 | + t.Error( |
| 149 | + "For", "len(GetRecords())", |
| 150 | + "expected", 0, |
| 151 | + "got", len(records), |
| 152 | + ) |
| 153 | + } |
| 154 | + popGitLedger() |
| 155 | +} |
| 156 | + |
| 157 | +func TestGetBySlug(t *testing.T) { |
| 158 | + pushGitLedger() |
| 159 | + r := Record{Path: "/tmp", Slug: "pony/fill"} |
| 160 | + r.AddToLedger() |
| 161 | + s := Record{Path: "/home", Slug: "badger/bear"} |
| 162 | + s.AddToLedger() |
| 163 | + rec, err := GetBySlug("fill") |
| 164 | + if rec != r { |
| 165 | + t.Error( |
| 166 | + "For", "GetBySlug(fill)", |
| 167 | + "expected", r, |
| 168 | + "got", rec, |
| 169 | + ) |
| 170 | + } |
| 171 | + rec, err = GetBySlug("pony/fill") |
| 172 | + if rec != r || err != nil { |
| 173 | + t.Error( |
| 174 | + "For", "GetBySlug(pony/fill)", |
| 175 | + "expected", r, |
| 176 | + "got", rec, |
| 177 | + ) |
| 178 | + } |
| 179 | + rec2, err2 := GetBySlug("badger") |
| 180 | + if rec2 != s || err2 != nil { |
| 181 | + t.Error( |
| 182 | + "For", "GetBySlug(badger)", |
| 183 | + "expected", s, |
| 184 | + "got", rec2, |
| 185 | + ) |
| 186 | + } |
| 187 | + popGitLedger() |
| 188 | +} |
| 189 | + |
| 190 | +func TestGetByPath(t *testing.T) { |
| 191 | + pushGitLedger() |
| 192 | + r := Record{Path: "/tmp", Slug: "pony/fill"} |
| 193 | + r.AddToLedger() |
| 194 | + s := Record{Path: "/home", Slug: "badger/bear"} |
| 195 | + s.AddToLedger() |
| 196 | + rec, err := GetByPath("/tmp") |
| 197 | + if rec != r { |
| 198 | + t.Error( |
| 199 | + "For", "GetByPath(/tmp)", |
| 200 | + "expected", r, |
| 201 | + "got", rec, |
| 202 | + ) |
| 203 | + } |
| 204 | + rec, err = GetByPath("/home") |
| 205 | + if rec != s || err != nil { |
| 206 | + t.Error( |
| 207 | + "For", "GetByPath(/home)", |
| 208 | + "expected", s, |
| 209 | + "got", rec, |
| 210 | + ) |
| 211 | + } |
| 212 | + rec2, err2 := GetByPath("DNE") |
| 213 | + if err2 == nil { |
| 214 | + t.Error( |
| 215 | + "For", "GetByPath(DNE)", |
| 216 | + "expected", "an error", |
| 217 | + "got", rec2, |
| 218 | + ) |
| 219 | + } |
| 220 | + popGitLedger() |
| 221 | +} |
0 commit comments