Skip to content

Commit 1c676ee

Browse files
refactored all of the tests and test fixtures
1 parent 4b4b47a commit 1c676ee

26 files changed

Lines changed: 1340 additions & 355 deletions

test/fixtures/bin/git

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/fixtures/clean-temp-dir.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const del = require('del');
5+
6+
/**
7+
* Clean the .tmp directory before each test
8+
*/
9+
beforeEach(() => {
10+
// Delete the .tmp directory, if it exists
11+
del.sync('test/.tmp');
12+
13+
// Re-create the .tmp directory
14+
fs.mkdirSync('test/.tmp');
15+
});

test/fixtures/cli.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const spawnSync = require('child_process').spawnSync;
5+
6+
const cliPath = path.resolve('bin/bump.js');
7+
8+
module.exports = { exec };
9+
10+
/**
11+
* Runs bump with the given arguments.
12+
*
13+
* @param {string} args - The args to run bump with
14+
*/
15+
function exec (args) {
16+
// Run bump
17+
args = [cliPath].concat(args.split(' '));
18+
let output = spawnSync('node', args, {
19+
encoding: 'utf8',
20+
cwd: path.resolve('test/.tmp'),
21+
});
22+
23+
// Check for errors
24+
if (output.error) {
25+
throw output.error;
26+
}
27+
28+
// Create an array containing each line of output
29+
output.lines = [];
30+
if (output.stdout) {
31+
output.lines = output.stdout.split('\n');
32+
33+
let lastLine = output.lines[output.lines.length - 1];
34+
if (lastLine === '') {
35+
output.lines.pop();
36+
}
37+
}
38+
39+
return output;
40+
}

test/fixtures/files/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
This file is for testing the `--grep` option.
22

3-
If all goes well, version 1.2.3 and v1.2.3 should both get updated, but version 5.6.7 and v8.9.10 shouldn't.
3+
If all goes well, version 1.2.3 and v1.2.3 should both get updated, but version 5.6.7 and v8.9.10 should not be changed.

test/fixtures/files/index.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
1-
(function() {
2-
'use strict';
1+
'use strict';
32

4-
// This file is for testing the --grep option
5-
// to make sure v1.2.3 gets replaced correctly
6-
var version = '1.2.3';
3+
const fs = require('fs');
4+
const path = require('path');
75

8-
})();
6+
const srcDir = __dirname;
7+
const destDir = path.join(__dirname, '../../.tmp');
8+
9+
const files = module.exports = {
10+
/**
11+
* Creates a file in the "test/.tmp" directory with the given contents
12+
*
13+
* @param {string} name - The file name (e.g. "package.json")
14+
* @param {string|object} [contents] - The file contents
15+
*/
16+
create (name, contents) {
17+
if (typeof contents === 'object') {
18+
contents = JSON.stringify(contents, null, 2);
19+
}
20+
fs.writeFileSync(path.join(destDir, name), contents);
21+
},
22+
23+
/**
24+
* Copies a file from the "test/fixtures/files" directory to the "test/.tmp" directory.
25+
*
26+
* @param {string} name - The name of the file to copy (e.g. "README.md", "script1.js")
27+
*/
28+
copy (name) {
29+
let contents = fs.readFileSync(path.join(srcDir, name), 'utf8');
30+
files.create(name, contents);
31+
},
32+
33+
/**
34+
* Reads a file in the "test/.tmp" directory, and returns its contents as a string.
35+
*
36+
* @param {string} name - The file name (e.g. "README.md", "script1.js")
37+
* @returns {object}
38+
*/
39+
text (name) {
40+
try {
41+
return fs.readFileSync(path.join(destDir, name), 'utf8');
42+
}
43+
catch (e) {
44+
return '';
45+
}
46+
},
47+
48+
/**
49+
* Parses a JSON file in the "test/.tmp" directory, and returns its contents
50+
* as a JavaScript object.
51+
*
52+
* @param {string} name - The file name (e.g. "package.json")
53+
* @returns {object|undefined}
54+
*/
55+
json (name) {
56+
try {
57+
let json = files.text(name);
58+
return JSON.parse(json);
59+
}
60+
catch (e) {
61+
return undefined;
62+
}
63+
},
64+
};

test/fixtures/files/script1.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(function () {
2+
'use strict';
3+
4+
// This file is for testing the --grep option
5+
// to make sure v1.2.3 gets replaced correctly
6+
let version = '1.2.3';
7+
return version;
8+
9+
}());
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
(function() {
1+
(function () {
22
'use strict';
33

44
// This file should not get updated when the --grep option is used,
55
// because version 3.2.1 and v8.9.10 don't match the old version number
66

7-
})();
7+
}());

test/fixtures/helper.js

Lines changed: 0 additions & 138 deletions
This file was deleted.

test/fixtures/mocks/git

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
*
5+
* This is a mock of the `git` CLI, for testing purposes.
6+
* It just records the CLI arguments in a JSON file.
7+
*
8+
*/
9+
10+
const mocks = require('./index');
11+
mocks.record('git', process.argv.slice(2));

0 commit comments

Comments
 (0)