/
init.test.js
88 lines (70 loc) · 3.62 KB
/
init.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const fs = require('fs');
const path = require('path');
const rimraf = require('mz-modules/rimraf');
const assert = require('assert');
const Helper = require('./helper');
const tmp = path.join(__dirname, '../.tmp');
const Command = require('..');
describe('test/init.test.js', () => {
let command;
let helper;
before(function* () {
yield rimraf(tmp);
command = new Command();
helper = new Helper(command);
});
beforeEach(() => rimraf(tmp));
afterEach(function* () {
yield rimraf(tmp);
helper.restore();
});
it('should work', function* () {
const boilerplatePath = path.join(__dirname, 'fixtures/simple-test');
yield command.run(tmp, [ 'simple-app', '--template=' + boilerplatePath, '--silent' ]);
assert(fs.existsSync(path.join(command.targetDir, '.gitignore')));
assert(fs.existsSync(path.join(command.targetDir, '.eslintrc')));
assert(fs.existsSync(path.join(command.targetDir, '.npmignore')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
assert(fs.existsSync(path.join(command.targetDir, 'simple-app')));
assert(fs.existsSync(path.join(command.targetDir, 'test', 'simple-app.test.js')));
const content = fs.readFileSync(path.join(command.targetDir, 'README.md'), 'utf-8');
assert(/# simple-app/.test(content));
});
it('should work with prompt', function* () {
helper.mock([[ 'simple-app', 'this is xxx', 'TZ', helper.KEY_ENTER, 'test' ]]);
const boilerplatePath = path.join(__dirname, 'fixtures/simple-test');
yield command.run(tmp, [ 'simple-app', '--force', '--template=' + boilerplatePath ]);
assert(fs.existsSync(path.join(command.targetDir, '.gitignore')));
assert(fs.existsSync(path.join(command.targetDir, '.eslintrc')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
assert(fs.existsSync(path.join(command.targetDir, 'simple-app')));
assert(fs.existsSync(path.join(command.targetDir, 'test', 'simple-app.test.js')));
const content = fs.readFileSync(path.join(command.targetDir, 'README.md'), 'utf-8');
assert(/default-simple-app/.test(content));
assert(/filter-test/.test(content));
});
it('should prompt', function* () {
helper.mock([ helper.KEY_DOWN, [ 'test', 'this is xxx', 'TZ', helper.KEY_ENTER ]]);
yield command.run(tmp, [ 'prompt-app', '--force' ]);
assert(fs.existsSync(path.join(command.targetDir, '.gitignore')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
const content = fs.readFileSync(path.join(command.targetDir, 'README.md'), 'utf-8');
assert(/Development/.test(content));
});
it('.replaceTemplate', () => {
assert(command.replaceTemplate('hi, {{ user }}', { user: 'egg' }) === 'hi, egg');
assert(command.replaceTemplate('hi, {{ user }}\n{{type}} {{user}}', { user: 'egg', type: 'init' }) === 'hi, egg\ninit egg');
assert(command.replaceTemplate('hi, {{ user }}', {}) === 'hi, {{ user }}');
assert(command.replaceTemplate('hi, \\{{ user }}', { user: 'egg' }) === 'hi, {{ user }}');
});
it('should works with remote boilerplate', function* () {
yield command.run(tmp, [ 'simple-app', '--type=simple', '--silent' ]);
assert(fs.existsSync(path.join(command.targetDir, '.gitignore')));
assert(fs.existsSync(path.join(command.targetDir, '.eslintrc')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
assert(fs.existsSync(path.join(command.targetDir, 'package.json')));
assert(fs.existsSync(path.join(command.targetDir, 'test/app/controller/home.test.js')));
assert(fs.existsSync(path.join(command.targetDir, 'app/controller/home.js')));
});
});