Skip to content

Commit a7f7a90

Browse files
committed
[Cache] Fix bugs and add UTs.
Signed-off-by: Eric Wang <[email protected]>
1 parent 86cfdca commit a7f7a90

7 files changed

+73
-2304
lines changed

Diff for: lib/commands/cache.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ cmd.handler = function(argv) {
3434
log.info(sprintf('%-50s %8s %s ago',
3535
f.name,
3636
h.prettySize(f.size),
37-
h.prettyTime(f.mtime)));
37+
h.prettyTime((Date.now() - f.mtime) / 1000)
38+
));
3839
});
3940
} else if (argv.all) {
4041
cache.list().forEach(function(f) {

Diff for: lib/helper.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ h.prettySize = function(size) {
3333
return size.toFixed(2) + units[i];
3434
};
3535

36-
h.prettyTime = function(t) {
36+
h.prettyTime = function(d) {
3737
var units = [
3838
[60, 'secs'],
3939
[60, 'mins'],
4040
[24, 'hours'],
41-
[7, 'weeks'],
42-
[4, 'months'],
43-
[12, 'years']
41+
[7, 'days'],
42+
[4, 'weeks'],
43+
[12, 'months'],
44+
[9999, 'years']
4445
];
4546

46-
var d = (Date.now() - t) / 1000;
4747
var i = 0;
48-
while (d > units[i][0] && i < units.length) {
48+
while (d >= units[i][0] && i < units.length) {
4949
d /= units[i][0];
5050
++i;
5151
}

Diff for: test/mock/find-the-difference.html.20160911

-815
This file was deleted.

Diff for: test/mock/find-the-difference.html.20170424

-1,097
This file was deleted.

Diff for: test/mock/two-sum.submissions.html.20161006

-385
This file was deleted.

Diff for: test/test_cache.js

+24
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,28 @@ describe('cache', function() {
3333
assert.deepEqual(cache.get(k), v);
3434
assert.equal(cache.del(k), true);
3535
});
36+
37+
it('should list ok when no cached', function() {
38+
var items = cache.list();
39+
assert.equal(items.length, 0);
40+
});
41+
42+
it('should list ok when cached', function() {
43+
assert.equal(cache.set(k, v), true);
44+
45+
var items = cache.list();
46+
assert.equal(items.length, 1);
47+
48+
assert.equal(items[0].name, k);
49+
assert.equal(items[0].size, JSON.stringify(v).length);
50+
});
51+
52+
it('should list ok when cache dir not exist', function() {
53+
h.getCacheDir = function() {
54+
return '/not-exist-dir';
55+
};
56+
57+
var items = cache.list();
58+
assert.equal(items.length, 0);
59+
});
3660
});

Diff for: test/test_helper.js

+41
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ describe('helper', function() {
4646
});
4747
}); // #prettyText
4848

49+
describe('#prettySize', function() {
50+
it('should ok', function() {
51+
assert.equal(h.prettySize(0), '0.00B');
52+
assert.equal(h.prettySize(512), '512.00B');
53+
assert.equal(h.prettySize(1024), '1.00K');
54+
assert.equal(h.prettySize(1024 * 1024), '1.00M');
55+
assert.equal(h.prettySize(1024 * 1024 * 1024), '1.00G');
56+
});
57+
}); // #prettySize
58+
59+
describe('#prettyTime', function() {
60+
it('should ok', function() {
61+
assert.equal(h.prettyTime(30), '30 secs');
62+
assert.equal(h.prettyTime(60), '1 mins');
63+
assert.equal(h.prettyTime(2400), '40 mins');
64+
assert.equal(h.prettyTime(3600), '1 hours');
65+
assert.equal(h.prettyTime(7200), '2 hours');
66+
assert.equal(h.prettyTime(86400), '1 days');
67+
assert.equal(h.prettyTime(86400 * 3), '3 days');
68+
assert.equal(h.prettyTime(86400 * 7), '1 weeks');
69+
});
70+
}); // #prettyTime
71+
4972
describe('#levelToName', function() {
5073
it('should ok', function() {
5174
assert.equal(h.levelToName(0), ' ');
@@ -161,6 +184,24 @@ describe('helper', function() {
161184
});
162185
}); // #getSetCookieValue
163186

187+
describe('#printSafeHTTP', function() {
188+
it('should hide sensitive info', function() {
189+
var raw = [
190+
"Cookie: 'xxxxxx'",
191+
"'X-CSRFToken': 'yyyyyy'",
192+
"'set-cookie': ['zzzzzz']"
193+
].join('\r\n');
194+
195+
var hide = [
196+
"Cookie: <hidden>",
197+
"'X-CSRFToken': <hidden>",
198+
"'set-cookie': <hidden>"
199+
].join('\r\n');
200+
201+
assert.equal(h.printSafeHTTP(raw), hide);
202+
});
203+
}); // #printSafeHTTP
204+
164205
describe('#readStdin', function() {
165206
function hijackStdin(data) {
166207
var stream = require('stream');

0 commit comments

Comments
 (0)