Skip to content

Commit

Permalink
nits & rewrote failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Aug 25, 2018
1 parent 8bf3a53 commit 0fa667a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
Empty file.
5 changes: 5 additions & 0 deletions lib/core/__tests__/__fixtures__/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Don't edit this
---

Do not edit this file
26 changes: 22 additions & 4 deletions lib/core/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,27 @@ describe('utils', () => {
});

test('getGitUpdateTime', () => {
const filepath =
'/home/shubham/Documents/Docusaurus/lib/core/__tests__/__fixtures__/test-doc.md';
expect(utils.getGitLastUpdated(filepath)).toBe('8/24/2018, 11:56:00 PM');
expect(utils.getGitLastUpdated(filepath + 'random')).toBeNull(); // Modify filepath to make it any invalid path
// existing test file in repository with git timestamp
const existingFilePath = path.join(__dirname, '__fixtures__', 'test.md');
const gitUpdateTime = utils.getGitLastUpdated(existingFilePath);
expect(typeof gitUpdateTime).toBe('string');
expect(Date.parse(gitUpdateTime)).not.toBeNaN();
expect(gitUpdateTime).not.toBeNull();

// non existing file
const nonExistingFilePath = path.join(
__dirname,
'__fixtures__',
'.nonExisting'
);
expect(utils.getGitLastUpdated(null)).toBeNull();
expect(utils.getGitLastUpdated(undefined)).toBeNull();
expect(utils.getGitLastUpdated(nonExistingFilePath)).toBeNull();

// temporary created file that has no git timestamp
const tempFilePath = path.join(__dirname, '__fixtures__', '.temp');
fs.writeFileSync(tempFilePath, 'Lorem ipsum :)');
expect(utils.getGitLastUpdated(tempFilePath)).toBeNull();
fs.unlinkSync(tempFilePath);
});
});
4 changes: 2 additions & 2 deletions lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const spawn = require('cross-spawn');

const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;

Expand All @@ -29,7 +30,6 @@ function getPath(path, cleanUrl = false) {
}

function getGitLastUpdated(filepath) {
const spawn = require('cross-spawn');
const timeSpan = spawn
.sync('git', ['log', '-1', '--format=%ct', filepath])
.stdout.toString('utf-8');
Expand All @@ -43,7 +43,7 @@ function getGitLastUpdated(filepath) {
module.exports = {
blogPostHasTruncateMarker,
extractBlogPostBeforeTruncate,
getGitLastUpdated,
getPath,
removeExtension,
getGitLastUpdated,
};

0 comments on commit 0fa667a

Please sign in to comment.