Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 1b6edae

Browse files
authored
Merge pull request #59 from apiaryio/honzajavorek/test-upgrade
Test and fix the 'upgrade' command
2 parents d01c17f + e15641d commit 1b6edae

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

cli/copyFeatures.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const glob = require('glob');
1010
*/
1111
module.exports = function copyFeatures(srcDir, dstDir, transformBasename = basename => basename) {
1212
glob.sync(path.join(srcDir, '*.feature')).forEach((featureSrcPath) => {
13-
const featureContent = fs.readFileSync(featureSrcPath, { encoding: 'utf-8' });
13+
const featureContent = fs.readFileSync(featureSrcPath, 'utf8');
1414
const featureBasename = path.basename(featureSrcPath);
1515
const featurePath = path.join(dstDir, transformBasename(featureBasename));
16-
fs.writeFileSync(featurePath, featureContent, { encoding: 'utf-8' });
16+
fs.writeFileSync(featurePath, featureContent, 'utf8');
1717
});
1818
};

cli/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ function upgrade() {
3939
// read the project's package.json and get currently installed version
4040
// of the 'dredd-hooks-template' package
4141
const packageDataPath = path.join(PROJECT_DIR, 'package.json');
42-
const packageData = JSON.parse(fs.readFileSync(packageDataPath, { encoding: 'utf-8' }));
42+
const packageData = JSON.parse(fs.readFileSync(packageDataPath, 'utf8'));
4343
const currentVersion = packageData.devDependencies['dredd-hooks-template'];
4444

4545
// ask npm about the latest published version of the 'dredd-hooks-template'
4646
// package
47-
const proc = run('npm', ['view', 'dredd-hooks-template', 'version'], { cwd: PROJECT_DIR });
47+
const proc = run('npm', ['view', 'dredd-hooks-template', 'version'], {
48+
cwd: PROJECT_DIR,
49+
stdio: 'pipe',
50+
});
4851
const version = proc.stdout.toString().trim();
4952

5053
// halt in case the project already depends on the latest version
@@ -60,7 +63,17 @@ function upgrade() {
6063
// copy '*.feature' files from the upgraded 'dredd-hooks-template' package
6164
// to the project, but don't overwrite the existing feature files, add these
6265
// as new ones, suffixed with the 'dredd-hooks-template' version
63-
copyFeatures(FEATURES_SRC_DIR, FEATURES_DIR, basename => `${basename}~${version}`);
66+
copyFeatures(FEATURES_SRC_DIR, FEATURES_DIR, basename => `${basename}~v${version}`);
67+
68+
// inform user about what has just happened and what's the next step
69+
console.log(`\
70+
The test suite template has been upgraded. New feature files have been copied
71+
to the 'features' directory, suffixed with '~v${version}'. Please go through
72+
them manually and update your test suite with the latest changes. Seeing the
73+
changes on GitHub might help as well:
74+
75+
https://github.com/apiaryio/dredd-hooks-template/compare/v${currentVersion}...v${version}
76+
`);
6477
}
6578

6679

cli/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { spawnSync } = require('child_process');
66
* by default and throws in case the command failed
77
*/
88
module.exports = function run(command, args, options) {
9-
const proc = spawnSync(command, args, { ...options, stdio: 'inherit' });
9+
const proc = spawnSync(command, args, { stdio: 'inherit', ...options });
1010
if (proc.error) throw proc.error;
1111
if (proc.status) throw new Error(`'${[command].concat(args).join(' ')}' failed`);
1212
return proc;

scripts/smoke-test.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ function parseShebang(contents) {
4242
}
4343

4444

45+
/* ****************************************************************************
46+
SET UP
47+
**************************************************************************** */
48+
4549
// create a temporary directory and init an npm package there
4650
const testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dredd-hooks-template-test-'));
4751
run('npm', ['init', '--yes'], { cwd: testDir });
@@ -54,6 +58,11 @@ const tgzBasename = `${packageData.name}-${packageData.version}.tgz`;
5458
const tgzPath = path.join(projectDir, tgzBasename);
5559
run('npm', ['install', tgzPath, '--save-dev'], { cwd: testDir });
5660

61+
62+
/* ****************************************************************************
63+
INITIALIZE & TEST
64+
**************************************************************************** */
65+
5766
// initialize the test suite template
5867
run('npx', ['dredd-hooks-template', 'init'], { cwd: testDir });
5968

@@ -72,14 +81,48 @@ const handlerCommand = `${path.relative(relativeBase, pythonPath)} ${path.relati
7281
// make custom changes to the '*.feature' files so they're able to test
7382
// the Python hooks (reference implementation)
7483
glob.sync(path.join(testDir, '**/*.feature')).forEach((featurePath) => {
75-
const content = fs.readFileSync(featurePath, { encoding: 'utf-8' });
84+
const content = fs.readFileSync(featurePath, 'utf8');
7685
const modifiedContent = uncommentPythonCodeBlocks(replacePlaceholders(content, handlerCommand));
77-
fs.writeFileSync(featurePath, modifiedContent, { encoding: 'utf-8' });
86+
fs.writeFileSync(featurePath, modifiedContent, 'utf8');
7887
});
7988

8089
// run 'dredd-hooks-template test', should pass
8190
run('npx', ['dredd-hooks-template', 'test'], { cwd: testDir });
8291

92+
93+
/* ****************************************************************************
94+
UPGRADE
95+
**************************************************************************** */
96+
97+
// pretend 'dredd-hooks-template' is in one of the previous versions
98+
const projectPackagePath = path.join(testDir, 'package.json');
99+
const projectPackageData = fs.readJSONSync(projectPackagePath);
100+
projectPackageData.devDependencies['dredd-hooks-template'] = '1.0.0';
101+
fs.writeJSONSync(projectPackagePath, projectPackageData);
102+
103+
// run 'dredd-hooks-template upgrade'
104+
const output = run('npx', ['dredd-hooks-template', 'upgrade'], {
105+
cwd: testDir,
106+
stdio: 'pipe',
107+
}).stdout.toString();
108+
109+
// the upgrade output should contain hints
110+
const containsHint = output.includes('copied') && output.includes('suffixed') && output.includes('manually');
111+
if (!containsHint) throw new Error("Output of 'dredd-hooks-template upgrade' doesn't include hint text");
112+
113+
// the upgrade output should contain a link to the relevant GitHub diff
114+
const containsLink = output.includes('https://github.com/apiaryio/dredd-hooks-template/compare/v1.0.0...');
115+
if (!containsLink) throw new Error("Output of 'dredd-hooks-template upgrade' doesn't include link to GitHub");
116+
117+
// the upgrade should copy the newest files over with suffixed extensions
118+
const filesCopied = glob.sync(path.join(testDir, '**/*.feature~v*')).length;
119+
if (!filesCopied) throw new Error("Running 'dredd-hooks-template upgrade' didn't copy feature files");
120+
121+
122+
/* ****************************************************************************
123+
TEAR DOWN
124+
**************************************************************************** */
125+
83126
// cleanup (intentionally doesn't cleanup on exception, as then one can 'cd'
84127
// to the temporary directory and play with it to debug problems)
85128
fs.removeSync(testDir);

0 commit comments

Comments
 (0)