@@ -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
4650const testDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'dredd-hooks-template-test-' ) ) ;
4751run ( 'npm' , [ 'init' , '--yes' ] , { cwd : testDir } ) ;
@@ -54,6 +58,11 @@ const tgzBasename = `${packageData.name}-${packageData.version}.tgz`;
5458const tgzPath = path . join ( projectDir , tgzBasename ) ;
5559run ( 'npm' , [ 'install' , tgzPath , '--save-dev' ] , { cwd : testDir } ) ;
5660
61+
62+ /* ****************************************************************************
63+ INITIALIZE & TEST
64+ **************************************************************************** */
65+
5766// initialize the test suite template
5867run ( '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)
7483glob . 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
8190run ( '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)
85128fs . removeSync ( testDir ) ;
0 commit comments