Skip to content

Commit

Permalink
Test cache dir path creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Jun 21, 2021
1 parent e6142f2 commit 6ea427b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ const util = require('util');

const log = util.debuglog('gh-pages');

function getCacheDir() {
return findCacheDir({name: 'gh-pages'});
/**
* Get the cache directory.
* @param {string} [optPath] Optional path.
* @returns {string} The full path to the cache directory.
*/
function getCacheDir(optPath) {
const dir = findCacheDir({name: 'gh-pages'});
if (!optPath) {
return dir;
}

return path.join(dir, filenamify(optPath));
}
exports.getCacheDir = getCacheDir;

function getRepo(options) {
if (options.repo) {
Expand Down Expand Up @@ -111,7 +122,7 @@ exports.publish = function publish(basePath, config, callback) {
getRepo(options)
.then(repo => {
repoUrl = repo;
const clone = path.join(getCacheDir(), filenamify(repo));
const clone = getCacheDir(repo);
log('Cloning %s into %s', repo, clone);
return Git.clone(repo, clone, options.branch, options);
})
Expand Down
46 changes: 46 additions & 0 deletions test/lib/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const path = require('path');
const assert = require('../helper').assert;
const index = require('../../lib/index');

describe('index', () => {
describe('getCacheDir()', () => {
it('works for git@github.com:<username>/<project>.git', () => {
const dir = index.getCacheDir(
'git@github.com:example-user/example-project.git'
);

const expected = path.join(
'.cache',
'gh-pages',
'git@github.com!example-user!example-project.git'
);
assert(dir.endsWith(expected), `unexpected cache dir: ${dir}`);
});

it('works for https://github.com/<username>/<project>.git', () => {
const dir = index.getCacheDir(
'https://github.com/example-user/example-project.git'
);

const expected = path.join(
'.cache',
'gh-pages',
'https!github.com!example-user!example-project.git'
);
assert(dir.endsWith(expected), `unexpected cache dir: ${dir}`);
});

it('works for https://<username>:<password>@github.com/<username>/<project>.git', () => {
const dir = index.getCacheDir(
'https://user:pass@github.com/example-user/example-project.git'
);

const expected = path.join(
'.cache',
'gh-pages',
'https!user!pass@github.com!example-user!example-project.git'
);
assert(dir.endsWith(expected), `unexpected cache dir: ${dir}`);
});
});
});

0 comments on commit 6ea427b

Please sign in to comment.