From 70b9890c0d7995a6dfb29f3256e32df5354043cb Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Wed, 30 Oct 2019 22:38:49 +1100 Subject: [PATCH] test: add header download test PR-URL: https://github.com/nodejs/node-gyp/pull/1796 Reviewed-By: Richard Lau --- test/test-configure-python.js | 1 + test/test-download.js | 70 ++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/test/test-configure-python.js b/test/test-configure-python.js index f28234134f..d08f9e5ed3 100644 --- a/test/test-configure-python.js +++ b/test/test-configure-python.js @@ -2,6 +2,7 @@ const test = require('tap').test const path = require('path') +const devDir = require('./common').devDir() const gyp = require('../lib/node-gyp') const requireInject = require('require-inject') const configure = requireInject('../lib/configure', { diff --git a/test/test-download.js b/test/test-download.js index dbffb20246..738a43f276 100644 --- a/test/test-download.js +++ b/test/test-download.js @@ -6,8 +6,13 @@ const path = require('path') const http = require('http') const https = require('https') const install = require('../lib/install') +const semver = require('semver') +const devDir = require('./common').devDir() +const rimraf = require('rimraf') +const gyp = require('../lib/node-gyp') +const log = require('npmlog') -require('npmlog').level = 'warn' +log.level = 'warn' test('download over http', function (t) { t.plan(2) @@ -103,3 +108,66 @@ test('check certificate splitting', function (t) { t.strictEqual(cas.length, 2) t.notStrictEqual(cas[0], cas[1]) }) + +// only run this test if we are running a version of Node with predictable version path behavior + +test('download headers (actual)', function (t) { + if (process.env.FAST_TEST || + process.release.name !== 'node' || + semver.prerelease(process.version) !== null || + semver.satisfies(process.version, '<10')) { + return t.skip('Skipping acutal download of headers due to test environment configuration') + } + + t.plan(17) + + const expectedDir = path.join(devDir, process.version.replace(/^v/, '')) + rimraf(expectedDir, (err) => { + t.ifError(err) + + const prog = gyp() + prog.parseArgv([]) + prog.devDir = devDir + log.level = 'warn' + install(prog, [], (err) => { + t.ifError(err) + + fs.readFile(path.join(expectedDir, 'installVersion'), 'utf8', (err, data) => { + t.ifError(err) + t.strictEqual(data, '9\n', 'correct installVersion') + }) + + fs.readdir(path.join(expectedDir, 'include/node'), (err, list) => { + t.ifError(err) + + t.ok(list.includes('common.gypi')) + t.ok(list.includes('config.gypi')) + t.ok(list.includes('node.h')) + t.ok(list.includes('node_version.h')) + t.ok(list.includes('openssl')) + t.ok(list.includes('uv')) + t.ok(list.includes('uv.h')) + t.ok(list.includes('v8-platform.h')) + t.ok(list.includes('v8.h')) + t.ok(list.includes('zlib.h')) + }) + + fs.readFile(path.join(expectedDir, 'include/node/node_version.h'), 'utf8', (err, contents) => { + t.ifError(err) + + const lines = contents.split('\n') + + // extract the 3 version parts from the defines to build a valid version string and + // and check them against our current env version + const version = ['major', 'minor', 'patch'].reduce((version, type) => { + const re = new RegExp(`^#define\\sNODE_${type.toUpperCase()}_VERSION`) + const line = lines.find((l) => re.test(l)) + const i = line ? parseInt(line.replace(/^[^0-9]+([0-9]+).*$/, '$1'), 10) : 'ERROR' + return `${version}${type !== 'major' ? '.' : 'v'}${i}` + }, '') + + t.strictEqual(version, process.version) + }) + }) + }) +})