|
| 1 | +var assert = require("assert"); |
| 2 | +var path = require("path"); |
| 3 | +var promisify = require("promisify-node"); |
| 4 | +var Promise = require("nodegit-promise"); |
| 5 | +var fse = promisify(require("fs-extra")); |
| 6 | +var local = path.join.bind(path, __dirname); |
| 7 | + |
| 8 | +describe("Stash", function() { |
| 9 | + var NodeGit = require("../../"); |
| 10 | + var Repository = NodeGit.Repository; |
| 11 | + var Stash = NodeGit.Stash; |
| 12 | + |
| 13 | + var reposPath = local("../repos/workdir"); |
| 14 | + |
| 15 | + before(function() { |
| 16 | + var test = this; |
| 17 | + return Repository.open(reposPath) |
| 18 | + .then(function(repository) { |
| 19 | + test.repository = repository; |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + it("gets no stashes on clean working directory", function() { |
| 24 | + var stashes = []; |
| 25 | + var stashCb = function(index, message, oid) { |
| 26 | + stashes.push({index: index, message: message, oid: oid}); |
| 27 | + }; |
| 28 | + |
| 29 | + return Stash.foreach(this.repository, stashCb) |
| 30 | + .then(function() { |
| 31 | + assert.equal(stashes.length, 0); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("can save and drop a stash", function() { |
| 36 | + var fileName = "README.md"; |
| 37 | + var fileContent = "Cha-cha-cha-chaaaaaangessssss"; |
| 38 | + var repo = this.repository; |
| 39 | + var filePath = path.join(repo.workdir(), fileName); |
| 40 | + var oldContent; |
| 41 | + var stashes = []; |
| 42 | + var stashOid; |
| 43 | + var stashMessage = "stash test"; |
| 44 | + |
| 45 | + return fse.readFile(filePath) |
| 46 | + .then(function(content) { |
| 47 | + oldContent = content; |
| 48 | + return fse.writeFile(filePath, fileContent); |
| 49 | + }) |
| 50 | + .then(function() { |
| 51 | + return Stash.save(repo, repo.defaultSignature(), stashMessage, 0); |
| 52 | + }) |
| 53 | + .then(function(oid) { |
| 54 | + stashOid = oid; |
| 55 | + var stashCb = function(index, message, oid) { |
| 56 | + stashes.push({index: index, message: message, oid: oid}); |
| 57 | + }; |
| 58 | + |
| 59 | + return Stash.foreach(repo, stashCb); |
| 60 | + }) |
| 61 | + .then(function() { |
| 62 | + assert.equal(stashes.length, 1); |
| 63 | + assert.equal(stashes[0].index, 0); |
| 64 | + assert.equal(stashes[0].message, "On master: " + stashMessage); |
| 65 | + assert.equal(stashes[0].oid.toString(), stashOid.toString()); |
| 66 | + |
| 67 | + return Stash.drop(repo, 0); |
| 68 | + }) |
| 69 | + .then(function () { |
| 70 | + stashes = []; |
| 71 | + var stashCb = function(index, message, oid) { |
| 72 | + stashes.push({index: index, message: message, oid: oid}); |
| 73 | + }; |
| 74 | + |
| 75 | + return Stash.foreach(repo, stashCb); |
| 76 | + }) |
| 77 | + .then(function() { |
| 78 | + assert.equal(stashes.length, 0); |
| 79 | + }) |
| 80 | + .catch(function(e) { |
| 81 | + return fse.writeFile(filePath, oldContent) |
| 82 | + .then(function() { |
| 83 | + return Promise.reject(e); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments