Skip to content

Commit b0871af

Browse files
committed
Add test for committing multiple files
1 parent 4ca361e commit b0871af

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

create-or-update-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ module.exports = function(octokit, opts) {
5656
}
5757

5858
// Create blobs
59-
const treeItems = [];
6059
for (let change of changes) {
6160
let message = change.message;
6261
if (!message) {
@@ -66,6 +65,7 @@ module.exports = function(octokit, opts) {
6665
return reject(`changes[].files is a required parameter`);
6766
}
6867

68+
const treeItems = [];
6969
for (let fileName in change.files) {
7070
let properties = change.files[fileName] || "";
7171

create-or-update-files.test.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,37 @@ test(`success (createBranch, use default base branch)`, async () => {
192192
await expect(run(body)).resolves.toEqual(branch);
193193
});
194194

195+
test(`success (createBranch, use default base branch, multiple commits)`, async () => {
196+
const body = {
197+
...validRequest,
198+
createBranch: true
199+
};
200+
201+
body.changes.push({
202+
message: "This is the second commit",
203+
files: {
204+
"second.md": "With some contents"
205+
}
206+
});
207+
delete body.base;
208+
209+
const repoDefaultBranch = "master";
210+
211+
mockGetRef(branch, `sha-${branch}`, false);
212+
mockGetRepo(repoDefaultBranch);
213+
mockGetRef(repoDefaultBranch, `sha-${repoDefaultBranch}`, true);
214+
mockCreateBlobFileOne();
215+
mockCreateBlobFileTwo();
216+
mockCreateBlobFileThree();
217+
mockCreateTree(`sha-${repoDefaultBranch}`);
218+
mockCreateTreeSecond(`ef105a72c03ce2743d90944c2977b1b5563b43c0`);
219+
mockCommit(`sha-${repoDefaultBranch}`);
220+
mockCommitSecond(`ef105a72c03ce2743d90944c2977b1b5563b43c0`);
221+
mockCreateRef(branch, `45d77edc93556e3a997bf73d5ed4d9fb57068928`);
222+
223+
await expect(run(body)).resolves.toEqual(branch);
224+
});
225+
195226
function mockGetRef(branch, sha, success) {
196227
const m = nock("https://api.github.com").get(
197228
`/repos/${owner}/${repo}/git/ref/heads/${branch}`
@@ -239,6 +270,13 @@ function mockCreateBlobFileTwo() {
239270
);
240271
}
241272

273+
function mockCreateBlobFileThree() {
274+
return mockCreateBlob(
275+
"V2l0aCBzb21lIGNvbnRlbnRz",
276+
"f65b65200aea4fecbe0db6ddac1c0848cdda1d9b"
277+
);
278+
}
279+
242280
function mockCreateTree(baseTree) {
243281
const expectedBody = {
244282
tree: [
@@ -270,6 +308,31 @@ function mockCreateTree(baseTree) {
270308
m.reply(200, body);
271309
}
272310

311+
function mockCreateTreeSecond(baseTree) {
312+
const expectedBody = {
313+
tree: [
314+
{
315+
path: "second.md",
316+
sha: "f65b65200aea4fecbe0db6ddac1c0848cdda1d9b",
317+
mode: "100644",
318+
type: "blob"
319+
}
320+
],
321+
base_tree: baseTree
322+
};
323+
324+
const m = nock("https://api.github.com").post(
325+
`/repos/${owner}/${repo}/git/trees`,
326+
expectedBody
327+
);
328+
329+
const body = {
330+
sha: "fffff6bbf5ab983d31b1cca28e204b71ab722764"
331+
};
332+
333+
m.reply(200, body);
334+
}
335+
273336
function mockCommit(baseTree) {
274337
const expectedBody = {
275338
message: "Your commit message",
@@ -289,6 +352,25 @@ function mockCommit(baseTree) {
289352
m.reply(200, body);
290353
}
291354

355+
function mockCommitSecond(baseTree) {
356+
const expectedBody = {
357+
message: "This is the second commit",
358+
tree: "fffff6bbf5ab983d31b1cca28e204b71ab722764",
359+
parents: [baseTree]
360+
};
361+
362+
const m = nock("https://api.github.com").post(
363+
`/repos/${owner}/${repo}/git/commits`,
364+
expectedBody
365+
);
366+
367+
const body = {
368+
sha: "45d77edc93556e3a997bf73d5ed4d9fb57068928"
369+
};
370+
371+
m.reply(200, body);
372+
}
373+
292374
function mockUpdateRef(branch) {
293375
const expectedBody = {
294376
force: true,
@@ -303,11 +385,11 @@ function mockUpdateRef(branch) {
303385
m.reply(200);
304386
}
305387

306-
function mockCreateRef(branch) {
388+
function mockCreateRef(branch, sha) {
307389
const expectedBody = {
308390
force: true,
309391
ref: `refs/heads/${branch}`,
310-
sha: "ef105a72c03ce2743d90944c2977b1b5563b43c0"
392+
sha: sha || "ef105a72c03ce2743d90944c2977b1b5563b43c0"
311393
};
312394

313395
const m = nock("https://api.github.com").post(

0 commit comments

Comments
 (0)