forked from sindresorhus/conduct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
111 lines (95 loc) · 3.87 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {globbySync} from 'globby';
import test from 'ava';
import execa from 'execa';
import tempy from 'tempy';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const bin = path.join(__dirname, 'cli.js');
const fixture = fs.readFileSync(path.join(__dirname, 'fixtures/code-of-conduct.md'), 'utf8');
const expectedString = fixture.slice(0, 15);
const expectedStringES = 'Nosotros, como miembros, contribuyentes y administradores';
const setLanguage = (language, cwd) => execa(bin, [`--language=${language}`], {cwd});
const posixJoin = (cwd, file) => {
const temporaryFileLocation = process.platform === 'win32' ? cwd.split(path.sep) : [cwd];
return path.posix.join(...temporaryFileLocation, file);
};
// It's serial as it's affected by the `update` test:
// https://github.com/sindresorhus/conduct/pull/12/files#r209551337
test.serial('generate', async t => {
const cwd = tempy.directory();
await execa(bin, ['[email protected]'], {cwd});
const src = fs.readFileSync(path.join(cwd, 'code-of-conduct.md'), 'utf8');
t.true(src.includes(expectedString));
t.true(src.includes('[email protected]'));
});
test('update', async t => {
const cwd = tempy.directory();
const filepath = path.join(cwd, 'CODE_OF_CONDUCT.markdown');
fs.writeFileSync(filepath, fixture);
await execa(bin, {cwd});
const src = fs.readFileSync(filepath, 'utf8');
t.true(src.includes(expectedString));
t.true(src.includes('[email protected]'));
});
test('readme filename', async t => {
const cwd = tempy.directory();
const filepath = path.join(cwd, 'readme.md');
fs.writeFileSync(filepath, '');
await execa(bin, {cwd});
const generatedFile = globbySync(posixJoin(cwd, 'code-of-conduct.md'))[0];
t.is(path.parse(generatedFile).base, 'code-of-conduct.md');
});
test('README filename', async t => {
const cwd = tempy.directory();
const filepath = path.join(cwd, 'README.md');
fs.writeFileSync(filepath, '');
await execa(bin, {cwd});
const generatedFile = globbySync(posixJoin(cwd, 'CODE-OF-CONDUCT.md'))[0];
t.is(path.parse(generatedFile).base, 'CODE-OF-CONDUCT.md');
});
test('filename --uppercase', async t => {
const cwd = tempy.directory();
await execa(bin, ['--uppercase'], {cwd});
const generatedFile = globbySync(posixJoin(cwd, 'CODE-OF-CONDUCT.md'))[0];
t.is(path.parse(generatedFile).base, 'CODE-OF-CONDUCT.md');
});
test.serial('set language', async t => {
const cwd = tempy.directory();
await setLanguage('es', cwd);
const src = fs.readFileSync(posixJoin(cwd, 'code-of-conduct.md'), 'utf8');
t.true(src.includes(expectedStringES));
// Cleanup
await setLanguage('en', cwd);
});
test.serial('unsupported language', async t => {
const cwd = tempy.directory();
await t.throwsAsync(setLanguage('unicorn', cwd), {message: /Unsupported language 'unicorn'/});
});
test.serial('update language', async t => {
const cwd = tempy.directory();
const filepath = path.join(cwd, 'CODE_OF_CONDUCT.markdown');
fs.writeFileSync(filepath, fixture);
await setLanguage('es', cwd);
const src = fs.readFileSync(filepath, 'utf8');
t.true(src.includes(expectedStringES));
// Cleanup
await setLanguage('en', cwd);
});
test.serial('generate with directory', async t => {
const cwd = tempy.directory();
fs.mkdirSync(path.join(cwd, 'test'));
await execa(bin, ['[email protected]', '--directory=test'], {cwd});
const src = fs.readFileSync(path.join(cwd, 'test', 'code-of-conduct.md'), 'utf8');
t.true(src.includes(expectedString));
t.true(src.includes('[email protected]'));
});
test.serial('generate with directory (directory missing)', async t => {
const cwd = tempy.directory();
await execa(bin, ['[email protected]', '--directory=test'], {cwd});
const src = fs.readFileSync(path.join(cwd, 'test', 'code-of-conduct.md'), 'utf8');
t.true(src.includes(expectedString));
t.true(src.includes('[email protected]'));
});