-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtests.js
137 lines (116 loc) · 3.32 KB
/
tests.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const test = require('tape');
const exec = require('child_process').execSync;
const hasAnsi = require('has-ansi');
const gitUtils = require('./utils/git');
const robbyrussell = require('.');
const colors = require('./utils/colors');
/**
* Status tests
*/
test('status', (t) => {
const status = {
char: '->',
success: colors.green.open,
failure: colors.red.open,
}
const good = robbyrussell.status({ code: 0, status });
const bad = robbyrussell.status({ code: 1, status });
t.plan(4);
console.log('good:', good);
t.true(hasAnsi(good), 'should contain ANSI');
t.equal(good, `${status.success}${status.char} `, 'should write character');
console.log('bad:', bad);
t.true(hasAnsi(bad), 'should contain ANSI');
t.equal(bad, `${status.failure}${status.char} `, 'should write character');
});
/**
* Directory case
*/
test('directory', (t) => {
const dir = {
color: colors.cyan.open,
};
const directory = robbyrussell.directory({ cwd: '/path/to/dir', dir });
const home = robbyrussell.directory({ cwd: `/home/${process.env.USER}`, dir });
t.plan(3);
console.log('directory:', directory);
t.true(hasAnsi(directory), 'should contain ANSI')
t.equal(directory, `${dir.color}dir`, 'should write only directory name');
console.log('at home:', home);
t.equal(home, `${dir.color}~`, 'should write ~ when in home');
});
/**
* Git repo status. Sync.
*/
test('git', (t) => {
const branch = gitUtils.branchSync(__dirname);
const tmpFile = `test-${(new Date()).valueOf()}.tmp`;
const git = {
indicator: colors.blue.open,
branch: colors.red.open,
dirty: colors.yellow.open,
dirtyChar: 'x',
};
if (!gitUtils.dirtySync(__dirname) && !gitUtils.untrackedSync(__dirname)) {
const clean = robbyrussell.gitRepo({ cwd: __dirname, git });
console.log('clean:', clean);
t.true(hasAnsi(clean), 'clean should contain ANSI');
t.equal(
clean,
` ${git.indicator}git:(${git.branch}${branch}${git.indicator})`,
'should be clean'
);
}
// Make repo dirty
exec(`touch ${tmpFile}`);
const dirty = robbyrussell.gitRepo({ cwd: __dirname, git });
console.log('dirty:', dirty);
t.true(hasAnsi(dirty), 'dirty should contain ANSI');
t.equal(
dirty,
` ${git.indicator}git:(${git.branch}${branch}${git.indicator})${git.dirty} ${git.dirtyChar}`,
'should be dirty'
);
// Clean repo
exec(`rm ${tmpFile}`);
t.end();
});
/**
* Whole composed prompt
*/
test('prompt', (t) => {
const config = {
cwd: 'path/to/dir',
code: 0,
prompt: {
open: colors.bold.open,
close: colors.bold.close + colors.reset.close
},
status: {
char: '➜',
success: colors.green.open,
failure: colors.red.open,
},
dir: {
color: colors.cyan.open
},
git: {
indicator: colors.blue.open,
branch: colors.red.open,
dirty: colors.yellow.open,
dirtyChar: '✗'
},
}
const prompt = robbyrussell.prompt(config);
const status = robbyrussell.status(config);
const directory =robbyrussell.directory(config);
const gitRepo = robbyrussell.gitRepo(config);
t.plan(2);
console.log('prompt:', prompt);
t.true(hasAnsi(prompt), 'contains ANSI');
t.equal(
prompt,
`${colors.bold.open}${status}${directory}${gitRepo}${colors.bold.close + colors.reset.close} `,
'should use function'
);
});