From ba418843e840476478b26de3998fe8df54cea21c Mon Sep 17 00:00:00 2001 From: Etienne Date: Wed, 2 Jun 2021 14:07:15 +0800 Subject: [PATCH] test: add tests for #1191 and #1192 --- test/.gitignore | 1 + test/test-1135-issue/main.js | 1 - test/test-1191/.gitignore | 3 ++ test/test-1191/index.js | 4 ++ test/test-1191/main.js | 68 +++++++++++++++++++++++++++ test/test-1191/package.json | 15 ++++++ test/test-1192/.gitignore | 1 + test/test-1192/main.js | 66 ++++++++++++++++++++++++++ test/test-1192/package.json | 24 ++++++++++ test/test-1192/public/views/index.pug | 8 ++++ test/test-1192/src/index.js | 47 ++++++++++++++++++ test/test.js | 2 + test/utils.js | 6 +-- 13 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 test/.gitignore create mode 100644 test/test-1191/.gitignore create mode 100644 test/test-1191/index.js create mode 100644 test/test-1191/main.js create mode 100644 test/test-1191/package.json create mode 100644 test/test-1192/.gitignore create mode 100644 test/test-1192/main.js create mode 100644 test/test-1192/package.json create mode 100644 test/test-1192/public/views/index.pug create mode 100644 test/test-1192/src/index.js diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 000000000..c2658d7d1 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/test/test-1135-issue/main.js b/test/test-1135-issue/main.js index 4fc71ae9f..cfe7ee922 100644 --- a/test/test-1135-issue/main.js +++ b/test/test-1135-issue/main.js @@ -33,7 +33,6 @@ console.log('node version = ', version); const npmlog = utils.exec.sync('npm install'); console.log('npm log :', npmlog); -// verify that we have the .pnpm folder and a symlinks module in node_modules assert(fs.lstatSync(path.join(__dirname, 'node_modules/canvas')).isDirectory()); assert( fs.lstatSync(path.join(__dirname, 'node_modules/canvas/build')).isDirectory() diff --git a/test/test-1191/.gitignore b/test/test-1191/.gitignore new file mode 100644 index 000000000..aa3001828 --- /dev/null +++ b/test/test-1191/.gitignore @@ -0,0 +1,3 @@ +output* +test.sqlite +package-lock* diff --git a/test/test-1191/index.js b/test/test-1191/index.js new file mode 100644 index 000000000..0691c3e2a --- /dev/null +++ b/test/test-1191/index.js @@ -0,0 +1,4 @@ +'use strict'; + +require('better-sqlite3')('test.sqlite'); +console.log('42'); diff --git a/test/test-1191/main.js b/test/test-1191/main.js new file mode 100644 index 000000000..4d2058a85 --- /dev/null +++ b/test/test-1191/main.js @@ -0,0 +1,68 @@ +#!/usr/bin/env node + +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const utils = require('../utils.js'); + +assert(!module.parent); +assert(__dirname === process.cwd()); + +/* eslint-disable no-unused-vars */ +const target = process.argv[2] || 'host'; +const ext = process.platform === 'win32' ? '.exe' : ''; +const output1 = './output1' + ext; +const output2 = './output2' + ext; +const input = './index.js'; + +// remove any possible left-over +utils.vacuum.sync('./node_modules'); + +const version = utils.exec.sync('node --version'); +console.log('node version = ', version); + +// launch `yarn` +const yarnlog = utils.exec.sync('yarn'); +console.log('yarn log :', yarnlog); + +// ----------------------------------------------------------------------- +// Execute programm outside pjg +const logRef = utils.spawn.sync('node', [path.join(__dirname, input)], { + cwd: __dirname, + expect: 0, +}); + +if (logRef.replace(/\r|\n/g, '') !== '42') { + console.log(`expecting 42 but got ${logRef}`); + process.exit(1); +} + +function doTestNoCompression() { + console.log('doTestNoCompression'); + utils.pkg.sync(['--target', target, '--output', output2, input], { + // expect: 0, + }); + const log = utils.spawn.sync(path.join(__dirname, output2), [], { + cwd: __dirname, + expect: 0, + stdio: ['inherit', 'pipe', 'pipe'], + }); + return log; +} + +const logNoCompression = doTestNoCompression(); +if (logNoCompression.stderr !== '') { + console.log('NO COMPRESSION: expecting no error'); + console.log('but got =', logNoCompression.stderr); + process.exit(1); +} + +// now with compress + +utils.vacuum.sync(output1); +utils.vacuum.sync(output2); +utils.vacuum.sync('node_modules'); +utils.vacuum.sync('package-lock.json'); +utils.vacuum.sync('test.sqlite'); +console.log('Done'); diff --git a/test/test-1191/package.json b/test/test-1191/package.json new file mode 100644 index 000000000..d21a2b210 --- /dev/null +++ b/test/test-1191/package.json @@ -0,0 +1,15 @@ +{ + "name": "test-1191", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "better-sqlite3": "^7.4.0" + } +} diff --git a/test/test-1192/.gitignore b/test/test-1192/.gitignore new file mode 100644 index 000000000..849ddff3b --- /dev/null +++ b/test/test-1192/.gitignore @@ -0,0 +1 @@ +dist/ diff --git a/test/test-1192/main.js b/test/test-1192/main.js new file mode 100644 index 000000000..e3e5c8ee4 --- /dev/null +++ b/test/test-1192/main.js @@ -0,0 +1,66 @@ +#!/usr/bin/env node + +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const utils = require('../utils.js'); + +assert(!module.parent); +assert(__dirname === process.cwd()); + +/* eslint-disable no-unused-vars */ +const target = process.argv[2] || 'host'; + +const ext = process.platform === 'win32' ? '.exe' : ''; +const cmd = process.platform === 'win32' ? '.cmd' : ''; +const output = './output' + ext; +const input = './package.json'; + +// remove any possible left-over +utils.vacuum.sync('./node_modules'); + +const version = utils.exec.sync('node --version'); +console.log('node version = ', version); + +// launch `yarn` +const yarnlog = utils.exec.sync('yarn'); +console.log('yarn log :', yarnlog); + +utils.pkg.sync(['--target', target, '--output', output, input], { + expect: 0, +}); + +// ----------------------------------------------------------------------- +// Execute programm outside pjg +const logRef = utils.spawn.sync( + 'node', + [path.join(__dirname, 'src/index.js')], + { + cwd: __dirname, + expect: 0, + stdio: ['inherit', 'pipe', 'pipe'], + } +); + +const log = utils.spawn.sync(path.join(__dirname, output), [], { + cwd: __dirname, + // expect: 0, + stdio: ['inherit', 'pipe', 'pipe'], +}); + +if (logRef.stdout !== log.stdout) { + console.log('expecting', logRef.stdout); + console.log('but got =', log.stdout); + process.exit(1); +} +if (logRef.stderr !== log.stderr) { + console.log('expecting', logRef.stderr); + console.log('but got =', log.stderr); + process.exit(1); +} + +utils.vacuum.sync(output); +utils.vacuum.sync('node_modules'); +utils.vacuum.sync('package-lock.json'); +console.log('Done'); diff --git a/test/test-1192/package.json b/test/test-1192/package.json new file mode 100644 index 000000000..6f8c74a8f --- /dev/null +++ b/test/test-1192/package.json @@ -0,0 +1,24 @@ +{ + "name": "pkg-express", + "version": "1.0.0", + "description": "Illustrates issue between pkg 5.2.X and express (works with with pkg pre 5.2.X).", + "repository": "", + "main": "src/index.js", + "bin": "src/index.js", + "license": "MIT", + "scripts": { + "postinstall": "npm run make", + "start": "node .", + "make": "pkg . -o ./dist/pkg-express.exe" + }, + "dependencies": { + "express": "^4.17.1", + "pug": "^3.0.2" + }, + "devDependencies": { + "pkg": "5.2.1" + }, + "pkg": { + "assets": "./public/views/*.pug" + } +} diff --git a/test/test-1192/public/views/index.pug b/test/test-1192/public/views/index.pug new file mode 100644 index 000000000..843b9e845 --- /dev/null +++ b/test/test-1192/public/views/index.pug @@ -0,0 +1,8 @@ + +html + head + title Hello World! + body + p Hello World! + + \ No newline at end of file diff --git a/test/test-1192/src/index.js b/test/test-1192/src/index.js new file mode 100644 index 000000000..e6d0775b0 --- /dev/null +++ b/test/test-1192/src/index.js @@ -0,0 +1,47 @@ +'use strict'; + +const http = require('http'); +const path = require('path'); +const express = require('express'); + +// Express app +const app = express(); +// Express views +app.set('views', path.join(__dirname, '../public/views')); +// Use pug as Express view engine +app.set('view engine', 'pug'); +// Match all routes +app.use('*', (_req, res) => { + res.render('index.pug'); +}); + +// Start HTTP server +const listener = http.createServer(app).listen(8080, () => { + console.info('Server started, listening on %d', listener.address().port); +}); + +// ------------------ now query he server +(async () => { + const options = { + hostname: '127.0.0.1', + path: '/', + method: 'GET', + port: 8080, + }; + + const req = http.request(options, (res) => { + console.log(`statusCode: ${res.statusCode}`); + res.on('data', (d) => { + process.stdout.write(d); + }); + }); + + req.on('error', (error) => { + console.error(error); + process.exit(1); + }); + req.on('close', () => { + process.exit(0); + }); + req.end(); +})(); diff --git a/test/test.js b/test/test.js index 62c324ab6..9ff18cf32 100644 --- a/test/test.js +++ b/test/test.js @@ -51,6 +51,8 @@ if (flavor === 'only-npm') { list.push('!' + joinAndForward('test-46-multi-arch')); list.push('!' + joinAndForward('test-46-multi-arch-2')); list.push('!' + joinAndForward('test-79-npm')); + list.push('!' + joinAndForward('test-1191')); + list.push('!' + joinAndForward('test-1192')); } } diff --git a/test/utils.js b/test/utils.js index 467c04aae..f53edb805 100644 --- a/test/utils.js +++ b/test/utils.js @@ -5,9 +5,9 @@ const path = require('path'); const mkdirp = require('mkdirp'); const rimraf = require('rimraf'); const globby = require('globby'); -const execSync = require('child_process').execSync; -const spawnSync = require('child_process').spawnSync; -const existsSync = require('fs').existsSync; +const { execSync } = require('child_process'); +const { spawnSync } = require('child_process'); +const { existsSync } = require('fs'); const stableStringify = require('json-stable-stringify'); module.exports.mkdirp = mkdirp;