Skip to content

Commit e3f108b

Browse files
committed
fixes UT issues on windows.
Signed-off-by: Eric Wang <[email protected]>
1 parent 863157a commit e3f108b

File tree

9 files changed

+81
-14
lines changed

9 files changed

+81
-14
lines changed

lib/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ core.exportProblem = function(problem, opts) {
109109
const data = _.extend({}, problem);
110110

111111
// unify format before rendering
112-
data.app = require('./config').app;
112+
data.app = require('./config').app || 'leetcode';
113113
if (!data.fid) data.fid = data.id;
114114
if (!data.lang) data.lang = opts.lang;
115115
data.code = (opts.code || data.code || '').replace(/\r\n/g, '\n');

lib/file.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ file.data = function(fullpath) {
110110
/// templates & metadata ///
111111
file.render = function(tpl, data) {
112112
const tplfile = path.join(this.codeDir('templates'), tpl + '.tpl');
113-
let result = _.template(this.data(tplfile))(data);
113+
let result = _.template(this.data(tplfile).replace(/\r\n/g, '\n'))(data);
114114

115115
if (this.isWindows()) {
116116
result = result.replace(/\n/g, '\r\n');

lib/plugin.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,25 @@ Plugin.copy = function(src, cb) {
150150
const dst = file.pluginFile(src);
151151

152152
const srcstream = src.startsWith('https://') ? request(src) : fs.createReadStream(src);
153+
const dststream = fs.createWriteStream(dst);
154+
let error;
155+
153156
srcstream.on('response', function(resp) {
154157
if (resp.statusCode !== 200)
155158
srcstream.emit('error', 'HTTP Error: ' + resp.statusCode);
156159
});
157160
srcstream.on('error', function(e) {
158-
spin.stop();
159-
file.rm(dst);
160-
return cb(e);
161+
dststream.emit('error', e);
161162
});
162163

163-
const dststream = fs.createWriteStream(dst);
164+
dststream.on('error', function(e) {
165+
error = e;
166+
dststream.end();
167+
});
164168
dststream.on('close', function() {
165169
spin.stop();
166-
return cb(null, dst);
170+
if (error) file.rm(dst);
171+
return cb(error, dst);
167172
});
168173

169174
log.debug('copying from ' + src);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"scripts": {
1313
"lint": "eslint lib/ test/",
14-
"test": "npm run lint && nyc mocha test/** && nyc report --reporter=lcov",
14+
"test": "npm run lint && nyc mocha test test/plugins && nyc report --reporter=lcov",
1515
"pkg": "pkg . --out-path=dist/ --targets"
1616
},
1717
"pkg": {

test/helper.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ const h = {
88
h.clean = function() {
99
if (!fs.existsSync(this.DIR))
1010
fs.mkdirSync(this.DIR);
11-
for (let f of fs.readdirSync(this.DIR))
12-
fs.unlinkSync(this.DIR + f);
11+
for (let f of fs.readdirSync(this.DIR)) {
12+
const fullpath = this.DIR + f;
13+
if (fs.statSync(fullpath).isDirectory())
14+
fs.rmdirSync(fullpath);
15+
else
16+
fs.unlinkSync(fullpath);
17+
}
1318
};
1419

1520
module.exports = h;

test/test_core.js

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ describe('core', function() {
151151
});
152152

153153
it('should codeonly ok', function() {
154+
file.isWindows = () => false;
155+
154156
const expected = [
155157
'/**',
156158
' * Definition for singly-linked list.',
@@ -209,6 +211,8 @@ describe('core', function() {
209211
});
210212

211213
it('should detailed ok with cpp', function() {
214+
file.isWindows = () => false;
215+
212216
const expected = [
213217
'/*',
214218
' * @lc app=leetcode id=2 lang=cpp',
@@ -257,6 +261,8 @@ describe('core', function() {
257261
});
258262

259263
it('should detailed ok with ruby', function() {
264+
file.isWindows = () => false;
265+
260266
const expected = [
261267
'#',
262268
'# @lc app=leetcode id=2 lang=ruby',

test/test_file.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict';
2+
const fs = require('fs');
23
const path = require('path');
34

45
const assert = require('chai').assert;
56
const rewire = require('rewire');
67

8+
const th = require('./helper');
9+
710
describe('file', function() {
811
let file;
912

@@ -14,7 +17,8 @@ describe('file', function() {
1417
describe('#dirAndFiles', function() {
1518
const HOME = path.join(__dirname, '..');
1619

17-
it('should ok', function() {
20+
it('should ok on linux', function() {
21+
if (file.isWindows()) this.skip();
1822
process.env.HOME = '/home/skygragon';
1923

2024
assert.equal(file.userHomeDir(), '/home/skygragon');
@@ -23,10 +27,18 @@ describe('file', function() {
2327
assert.equal(file.cacheFile('xxx'), '/home/skygragon/.lc/leetcode/cache/xxx.json');
2428
assert.equal(file.configFile(), '/home/skygragon/.lc/config.json');
2529
assert.equal(file.name('/home/skygragon/.lc/leetcode/cache/xxx.json'), 'xxx');
30+
});
2631

32+
it('should ok on windows', function() {
33+
if (!file.isWindows()) this.skip();
2734
process.env.HOME = '';
2835
process.env.USERPROFILE = 'C:\\Users\\skygragon';
2936
assert.equal(file.userHomeDir(), 'C:\\Users\\skygragon');
37+
assert.equal(file.homeDir(), 'C:\\Users\\skygragon\\.lc');
38+
assert.equal(file.cacheDir(), 'C:\\Users\\skygragon\\.lc\\leetcode\\cache');
39+
assert.equal(file.cacheFile('xxx'), 'C:\\Users\\skygragon\\.lc\\leetcode\\cache\\xxx.json');
40+
assert.equal(file.configFile(), 'C:\\Users\\skygragon\\.lc\\config.json');
41+
assert.equal(file.name('C:\\Users\\skygragon\\.lc\\leetcode\\cache\\xxx.json'), 'xxx');
3042
});
3143

3244
it('should codeDir ok', function() {
@@ -98,5 +110,42 @@ describe('file', function() {
98110
assert.equal(meta.id, '222');
99111
assert.equal(meta.lang, 'python3');
100112
});
113+
114+
it('should fmt ok', function() {
115+
file.init();
116+
const data = file.fmt('${id}', {id: 123});
117+
assert.equal(data, '123');
118+
});
101119
}); // #meta
120+
121+
describe('#genneral', function() {
122+
beforeEach(function() {
123+
th.clean();
124+
});
125+
afterEach(function() {
126+
th.clean();
127+
});
128+
129+
it('should mkdir ok', function() {
130+
const dir = th.DIR + 'dir';
131+
assert.equal(fs.existsSync(dir), false);
132+
file.mkdir(dir);
133+
assert.equal(fs.existsSync(dir), true);
134+
file.mkdir(dir);
135+
assert.equal(fs.existsSync(dir), true);
136+
});
137+
138+
it('should mv ok', function() {
139+
const SRC = th.Dir + 'src';
140+
const DST = th.DIR + 'dst';
141+
assert.equal(fs.existsSync(SRC), false);
142+
assert.equal(fs.existsSync(DST), false);
143+
file.mkdir(SRC);
144+
assert.equal(fs.existsSync(SRC), true);
145+
assert.equal(fs.existsSync(DST), false);
146+
file.mv(SRC, DST);
147+
assert.equal(fs.existsSync(SRC), false);
148+
assert.equal(fs.existsSync(DST), true);
149+
});
150+
}); // #general
102151
});

test/test_icon.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ describe('icon', function() {
3030
assert.equal(icon.unlike, 'unlike');
3131
});
3232

33-
it('should ok with unknown theme', function() {
33+
it('should ok with unknown theme on linux', function() {
34+
file.isWindows = () => false;
35+
3436
icon.setTheme('non-exist');
3537
assert.equal(icon.yes, '✔');
3638
assert.equal(icon.no, '✘');

test/test_plugin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ describe('plugin', function() {
146146
];
147147
fs.writeFileSync(SRC, data.join('\n'));
148148

149-
Plugin.install(SRC, function(e, p) {
149+
Plugin.copy(SRC, function(e, fullpath) {
150150
assert.notExists(e);
151-
assert.equal(p.x, 123);
151+
assert.equal(fullpath, DST);
152152
assert.equal(fs.existsSync(DST), true);
153153
done();
154154
});

0 commit comments

Comments
 (0)