Skip to content

Commit

Permalink
fix: change docs command to always update all valid docs (#338)
Browse files Browse the repository at this point in the history
* fix: change swagger command to always update all valid docs

* test: update test to fit docs update
  • Loading branch information
rahulhegdee authored Jul 9, 2021
1 parent 5503c04 commit 792a2b2
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
1 change: 1 addition & 0 deletions __tests__/__fixtures__/failure-docs/fail-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Body
6 changes: 6 additions & 0 deletions __tests__/__fixtures__/failure-docs/new-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: 5ae122e10fdf4e39bb34db6f
title: This is the document title
---

Body
109 changes: 109 additions & 0 deletions __tests__/cmds/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,115 @@ describe('rdme docs', () => {
postMock.done();
});
});

it('should create only valid docs', () => {
console.log = jest.fn();
expect.assertions(2);
const slug = 'fail-doc';
const slugTwo = 'new-doc';
const doc = frontMatter(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slug}.md`)));
const docTwo = frontMatter(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slugTwo}.md`)));
const hash = crypto
.createHash('sha1')
.update(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slug}.md`)))
.digest('hex');

const hashTwo = crypto
.createHash('sha1')
.update(fs.readFileSync(path.join(fixturesDir, `/failure-docs/${slugTwo}.md`)))
.digest('hex');

const getMock = nock(config.host, {
reqheaders: {
'x-readme-version': version,
},
})
.get(`/api/v1/docs/${slug}`)
.basicAuth({ user: key })
.reply(404, {
error: 'DOC_NOTFOUND',
message: `The doc with the slug '${slug}' couldn't be found`,
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const getMockTwo = nock(config.host, {
reqheaders: {
'x-readme-version': version,
},
})
.get(`/api/v1/docs/${slugTwo}`)
.basicAuth({ user: key })
.reply(404, {
error: 'DOC_NOTFOUND',
message: `The doc with the slug '${slugTwo}' couldn't be found`,
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMock = nock(config.host, {
reqheaders: {
'x-readme-version': version,
},
})
.post(`/api/v1/docs`, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(400, {
error: 'DOC_INVALID',
message: "We couldn't save this doc (Path `category` is required.).",
});

const postMockTwo = nock(config.host, {
reqheaders: {
'x-readme-version': version,
},
})
.post(`/api/v1/docs`, { slug: slugTwo, body: docTwo.content, ...docTwo.data, lastUpdatedHash: hashTwo })
.basicAuth({ user: key })
.reply(201, {
metadata: { image: [], title: '', description: '' },
api: {
method: 'post',
url: '',
auth: 'required',
params: [],
apiSetting: '60ddf83e30681022753e27af',
},
title: 'This is the document title',
updates: [],
type: 'endpoint',
slug: slugTwo,
body: 'Body',
category: '5ae122e10fdf4e39bb34db6f',
});

return docs.run({ folder: './__tests__/__fixtures__/failure-docs', key, version }).then(message => {
expect(console.log).toHaveBeenCalledTimes(1);
expect(message).toStrictEqual([
{
metadata: { image: [], title: '', description: '' },
api: {
method: 'post',
url: '',
auth: 'required',
params: [],
apiSetting: '60ddf83e30681022753e27af',
},
title: 'This is the document title',
updates: [],
type: 'endpoint',
slug: slugTwo,
body: 'Body',
category: '5ae122e10fdf4e39bb34db6f',
},
]);
getMock.done();
getMockTwo.done();
postMock.done();
postMockTwo.done();
console.log.mockRestore();
});
});
});
});

Expand Down
20 changes: 17 additions & 3 deletions src/cmds/docs/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('colors');
const request = require('request-promise-native');
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -34,7 +35,7 @@ exports.args = [
},
];

exports.run = function (opts) {
exports.run = async function (opts) {
const { folder, key, version } = opts;

if (!key) {
Expand Down Expand Up @@ -89,7 +90,7 @@ exports.run = function (opts) {
.catch(err => Promise.reject(new APIError(err)));
}

return Promise.all(
const updatedDocs = await Promise.allSettled(
files.map(async filename => {
const file = await readFile(path.join(folder, filename), 'utf8');
const matter = frontMatter(file);
Expand All @@ -103,7 +104,20 @@ exports.run = function (opts) {
...options,
})
.then(updateDoc.bind(null, slug, matter, hash), createDoc.bind(null, slug, matter, hash))
.catch(err => Promise.reject(new APIError(err)));
.catch(err => {
console.log(`\n\`${slug}\` failed to upload. ${err.message}\n`.red);
});
})
);

for (let i = 0; i < updatedDocs.length; ) {
if (updatedDocs[i].value !== undefined) {
updatedDocs[i] = updatedDocs[i].value; // returns only the value of the response
i += 1;
} else {
updatedDocs.splice(i, 1); // we already displayed the error messages so we can filter those out
}
}

return updatedDocs;
};

0 comments on commit 792a2b2

Please sign in to comment.