|
| 1 | +package ledger_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/git-hook/git-ledger" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "os" |
| 10 | + "fmt" |
| 11 | + "path" |
| 12 | +) |
| 13 | +// Move the current git-ledger to a new location so testing can create |
| 14 | +// a temporary one |
| 15 | +func pushGitLedger() { |
| 16 | + os.Rename(Path(), fmt.Sprintf("%s.backup", Path())) |
| 17 | + f, _ := os.Create(Path()) |
| 18 | + defer f.Close() |
| 19 | +} |
| 20 | + |
| 21 | +// Remove the temporary git-ledger and replace it with the backup |
| 22 | +func popGitLedger() { |
| 23 | + os.Rename(fmt.Sprintf("%s.backup", Path()), Path()) |
| 24 | +} |
| 25 | + |
| 26 | +// Create a dummy ledger-file to act as the device-under-test. |
| 27 | +func createDummyLedger() { |
| 28 | + r = Record{Path: "/tmp", Slug: "pony/fill"} |
| 29 | + r.AddToLedger() |
| 30 | + records, _ := GetRecords() |
| 31 | + Expect(len(records) == 1) |
| 32 | + |
| 33 | + s = Record{Path: "/home", Slug: "badger/bear"} |
| 34 | + s.AddToLedger() |
| 35 | + records, _ = GetRecords() |
| 36 | + Expect(len(records) == 2) |
| 37 | +} |
| 38 | + |
| 39 | +var ( |
| 40 | + r Record |
| 41 | + s Record |
| 42 | +) |
| 43 | + |
| 44 | +var _ = Describe("GitLedger", func() { |
| 45 | + |
| 46 | + BeforeEach(func() { |
| 47 | + pushGitLedger() |
| 48 | + createDummyLedger() |
| 49 | + }) |
| 50 | + AfterEach(popGitLedger) |
| 51 | + |
| 52 | + // TODO: test that git-ledger silently creates the ledger if asked to read it |
| 53 | + |
| 54 | + Describe("CLI", func() { |
| 55 | + Context("add", func() { |
| 56 | + Specify("should accept fully-initialized records", func() { |
| 57 | + Record{Path: "/tmp", Slug: "pony/fill"}.AddToLedger() |
| 58 | + records, _ := GetRecords() |
| 59 | + Expect(len(records) == 1) |
| 60 | + |
| 61 | + Record{Path: "/home", Slug: "badger/bear"}.AddToLedger() |
| 62 | + records, _ = GetRecords() |
| 63 | + Expect(len(records) == 2) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + Context("remove", func() { |
| 68 | + Specify("should accept fully-initialized records", func() { |
| 69 | + s.RemoveFromLedger() |
| 70 | + records, _ := GetRecords() |
| 71 | + Expect(len(records) == 1) |
| 72 | + r.RemoveFromLedger() |
| 73 | + records, _ = GetRecords() |
| 74 | + Expect(len(records) == 0) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + // Context("find", func() { |
| 79 | + // var ( |
| 80 | + // r Record |
| 81 | + // s Record |
| 82 | + // ) |
| 83 | + |
| 84 | + // Specify("") |
| 85 | + // }) |
| 86 | + }) |
| 87 | + |
| 88 | + Context("The ledger", func() { |
| 89 | + Specify("should reside in the user's home directory", func() { |
| 90 | + Expect(path.Join(os.Getenv("HOME"), ".git-ledger") == Path()) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + Describe("Records", func() { |
| 95 | + |
| 96 | + Context("When the ledger is empty", func() { |
| 97 | + It("should return 0 records", func() { |
| 98 | + records, _ := GetRecords() |
| 99 | + Expect(len(records) == 0) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + // fixme: make this format more awesome |
| 104 | + Specify("should print in an expected format", func() { |
| 105 | + path := "/path/to/nowhere" |
| 106 | + slug := "sclopio/peepio" |
| 107 | + record := Record{Path: path, Slug: slug} |
| 108 | + expected := fmt.Sprintf("[[Record]]\npath = \"%s\"\nslug = \"%s\"\n\n", record.Path, record.Slug) |
| 109 | + Expect(record.String() == expected) |
| 110 | + }) |
| 111 | + |
| 112 | + Specify("should be index-able by slug", func() { |
| 113 | + rec, _ := GetBySlug("fill") |
| 114 | + Expect(rec == r) |
| 115 | + rec, _ = GetBySlug("pony/fill") |
| 116 | + Expect(rec == r) |
| 117 | + rec, _ = GetBySlug("badger") |
| 118 | + Expect(rec == s) |
| 119 | + }) |
| 120 | + |
| 121 | + Specify("should be index-able by path", func() { |
| 122 | + rec, _ := GetBySlug("/tmp") |
| 123 | + Expect(rec == r) |
| 124 | + rec, _ = GetBySlug("/home") |
| 125 | + Expect(rec == s) |
| 126 | + _, err := GetBySlug("DNE") |
| 127 | + Expect(err != nil) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + Describe("Detecting git meta-data", func() { |
| 132 | + Context("Git repositories", func() { |
| 133 | + var ( |
| 134 | + dir string |
| 135 | + gitdir string |
| 136 | + ) |
| 137 | + |
| 138 | + BeforeEach(func() { |
| 139 | + dir = "/tmp" |
| 140 | + gitdir = path.Join(dir, ".git") |
| 141 | + }) |
| 142 | + |
| 143 | + AfterEach(func() { |
| 144 | + os.Remove(gitdir) |
| 145 | + }) |
| 146 | + |
| 147 | + Specify("should identify as such", func() { |
| 148 | + os.MkdirAll(gitdir, os.ModePerm) |
| 149 | + Expect(IsGitProject(dir)) |
| 150 | + }) |
| 151 | + |
| 152 | + Specify("should be differentiable from non-git repositories", func() { |
| 153 | + Expect(!IsGitProject(dir)) |
| 154 | + }) |
| 155 | + }) |
| 156 | + }) |
| 157 | +}) |
0 commit comments