|
| 1 | +var assert = require("assert"), |
| 2 | + promisify = require("promisify-node"), |
| 3 | + fse = promisify(require("fs-extra")), |
| 4 | + path = require("path"), |
| 5 | + local = path.join.bind(path, __dirname); |
| 6 | + |
| 7 | +describe.only("Filter", function() { |
| 8 | + var NodeGit = require("../../"); |
| 9 | + |
| 10 | + var emptyRepoPath = local("../repos/empty"), |
| 11 | + filterName = "psuedo_filter", |
| 12 | + // newRepoPath = local("../repos/newrepo"), |
| 13 | + Registry = NodeGit.FilterRegistry, |
| 14 | + Checkout = NodeGit.Checkout, |
| 15 | + Repository = NodeGit.Repository, |
| 16 | + Attr = NodeGit.Attr, |
| 17 | + Status = NodeGit.Status, |
| 18 | + reposPath = local("../repos/workdir"); |
| 19 | + |
| 20 | + beforeEach(function() { |
| 21 | + var test = this; |
| 22 | + |
| 23 | + return Repository.open(reposPath) |
| 24 | + .then(function(repository) { |
| 25 | + test.repository = repository; |
| 26 | + }) |
| 27 | + .then(function() { |
| 28 | + return Repository.open(emptyRepoPath); |
| 29 | + }) |
| 30 | + .then(function(emptyRepo) { |
| 31 | + test.emptyRepo = emptyRepo; |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(function() { |
| 36 | + //Unregistering the filter to avoid GIT_EEXISTS |
| 37 | + Registry.unregister(filterName); |
| 38 | + }); |
| 39 | + |
| 40 | + it("Registering filter for the first time", function() { |
| 41 | + // registering custom filter |
| 42 | + var result = Registry.register(filterName, { |
| 43 | + initialize: function() { |
| 44 | + console.log("inside INIT"); |
| 45 | + }, |
| 46 | + apply: function() { |
| 47 | + console.log("inside APPLY"); |
| 48 | + }, |
| 49 | + stream: function() { |
| 50 | + console.log("inside STREAM"); |
| 51 | + }, |
| 52 | + check: function(){ |
| 53 | + console.log("inside CHECK"); |
| 54 | + } |
| 55 | + }, 0); |
| 56 | + assert.strictEqual(result, 0); |
| 57 | + }); |
| 58 | + |
| 59 | + it("Registering filter and re-registering same filter", function() { |
| 60 | + // registering custom filter |
| 61 | + var result = Registry.register(filterName, { |
| 62 | + initialize: function() { |
| 63 | + console.log("inside INIT"); |
| 64 | + }, |
| 65 | + apply: function() { |
| 66 | + console.log("inside APPLY"); |
| 67 | + }, |
| 68 | + stream: function() { |
| 69 | + console.log("inside STREAM"); |
| 70 | + }, |
| 71 | + check: function(){ |
| 72 | + console.log("inside CHECK"); |
| 73 | + } |
| 74 | + }, 0); |
| 75 | + assert.strictEqual(result, 0); |
| 76 | + |
| 77 | + result = Registry.register(filterName, { |
| 78 | + initialize: function() { |
| 79 | + console.log("inside INIT"); |
| 80 | + }, |
| 81 | + apply: function() { |
| 82 | + console.log("inside APPLY"); |
| 83 | + }, |
| 84 | + stream: function() { |
| 85 | + console.log("inside STREAM"); |
| 86 | + }, |
| 87 | + check: function(){ |
| 88 | + console.log("inside CHECK"); |
| 89 | + } |
| 90 | + }, 0); |
| 91 | + assert.strictEqual(result, -4); |
| 92 | + }); |
| 93 | + |
| 94 | + it("Testing Initialize callback", function() { |
| 95 | + var test = this, |
| 96 | + testFilePath = path.join(reposPath, "package.json"), |
| 97 | + flags = Status.SHOW.INDEX_AND_WORKDIR; |
| 98 | + |
| 99 | + // registering custom filter |
| 100 | + var result = Registry.register(filterName, { |
| 101 | + initialize: function() { |
| 102 | + console.log("inside INIT"); |
| 103 | + }, |
| 104 | + apply: function() { |
| 105 | + console.log("inside APPLY"); |
| 106 | + }, |
| 107 | + stream: function() { |
| 108 | + console.log("inside STREAM"); |
| 109 | + }, |
| 110 | + check: function(){ |
| 111 | + console.log("inside CHECK"); |
| 112 | + } |
| 113 | + }, 0); |
| 114 | + |
| 115 | + assert.strictEqual(result, 0); |
| 116 | + |
| 117 | + // creating .gitattributes |
| 118 | + var gitattributeFilePromise = fse.writeFile( |
| 119 | + path.join(reposPath, ".gitattributes"), |
| 120 | + "* filter="+ filterName +" diff=lfs merge=lfs -text", { |
| 121 | + encoding: "utf-8", |
| 122 | + }); |
| 123 | + //creating test file that will be used to trigger custom filter |
| 124 | + var testFilePromise = fse.writeFile( |
| 125 | + testFilePath, |
| 126 | + "initial text", { |
| 127 | + encoding: "utf-8", |
| 128 | + }); |
| 129 | + |
| 130 | + Attr.cacheFlush(this.repository); |
| 131 | + |
| 132 | + // setup complete, testing initialize of custom filter |
| 133 | + return Promise.all([gitattributeFilePromise, testFilePromise]) |
| 134 | + // create necessary files |
| 135 | + .then(function() { |
| 136 | + return Attr.get( |
| 137 | + test.repository, |
| 138 | + flags, |
| 139 | + path.join(reposPath, ".gitattributes"), |
| 140 | + "filter"); |
| 141 | + }) |
| 142 | + // check attribute values |
| 143 | + .then(function(data) { |
| 144 | + console.log("data: ", data); |
| 145 | + assert.strictEqual(data, filterName); |
| 146 | + }) |
| 147 | + // modify file |
| 148 | + .then(function() { |
| 149 | + return fse.writeFile(testFilePath, |
| 150 | + "Modified Content", |
| 151 | + { |
| 152 | + encoding: "utf-8" |
| 153 | + }); |
| 154 | + }) |
| 155 | + // perform checkout, which should trigger filter |
| 156 | + .then(function() { |
| 157 | + var opts = { |
| 158 | + checkoutStrategy: Checkout.STRATEGY.FORCE, |
| 159 | + paths: "package.json" |
| 160 | + }; |
| 161 | + return Checkout.head(test.repository, opts); |
| 162 | + }) |
| 163 | + .then(function() { |
| 164 | + console.log("Post checkout"); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | +}); |
0 commit comments