Skip to content

Commit 6ea427b

Browse files
committed
Test cache dir path creation
1 parent e6142f2 commit 6ea427b

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

lib/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ const util = require('util');
1010

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

13-
function getCacheDir() {
14-
return findCacheDir({name: 'gh-pages'});
13+
/**
14+
* Get the cache directory.
15+
* @param {string} [optPath] Optional path.
16+
* @returns {string} The full path to the cache directory.
17+
*/
18+
function getCacheDir(optPath) {
19+
const dir = findCacheDir({name: 'gh-pages'});
20+
if (!optPath) {
21+
return dir;
22+
}
23+
24+
return path.join(dir, filenamify(optPath));
1525
}
26+
exports.getCacheDir = getCacheDir;
1627

1728
function getRepo(options) {
1829
if (options.repo) {
@@ -111,7 +122,7 @@ exports.publish = function publish(basePath, config, callback) {
111122
getRepo(options)
112123
.then(repo => {
113124
repoUrl = repo;
114-
const clone = path.join(getCacheDir(), filenamify(repo));
125+
const clone = getCacheDir(repo);
115126
log('Cloning %s into %s', repo, clone);
116127
return Git.clone(repo, clone, options.branch, options);
117128
})

test/lib/index.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const path = require('path');
2+
const assert = require('../helper').assert;
3+
const index = require('../../lib/index');
4+
5+
describe('index', () => {
6+
describe('getCacheDir()', () => {
7+
it('works for [email protected]:<username>/<project>.git', () => {
8+
const dir = index.getCacheDir(
9+
'[email protected]:example-user/example-project.git'
10+
);
11+
12+
const expected = path.join(
13+
'.cache',
14+
'gh-pages',
15+
'[email protected]!example-user!example-project.git'
16+
);
17+
assert(dir.endsWith(expected), `unexpected cache dir: ${dir}`);
18+
});
19+
20+
it('works for https://github.com/<username>/<project>.git', () => {
21+
const dir = index.getCacheDir(
22+
'https://github.com/example-user/example-project.git'
23+
);
24+
25+
const expected = path.join(
26+
'.cache',
27+
'gh-pages',
28+
'https!github.com!example-user!example-project.git'
29+
);
30+
assert(dir.endsWith(expected), `unexpected cache dir: ${dir}`);
31+
});
32+
33+
it('works for https://<username>:<password>@github.com/<username>/<project>.git', () => {
34+
const dir = index.getCacheDir(
35+
'https://user:[email protected]/example-user/example-project.git'
36+
);
37+
38+
const expected = path.join(
39+
'.cache',
40+
'gh-pages',
41+
'[email protected]!example-user!example-project.git'
42+
);
43+
assert(dir.endsWith(expected), `unexpected cache dir: ${dir}`);
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)