From 2e45b112bdb6e88d32fa09b3eab2482637493b6c Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Thu, 27 Oct 2022 09:06:53 -0700 Subject: [PATCH] chore: use a local instead of remote file for test I was able to replicate the same behavior from #332 by piping a readable stream with a `highWaterMark` of 1 to extract. I confirmed this test still failed without the fixes from #332 and now passes. --- package.json | 1 + test/extract.js | 57 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 526e3adc..033d914e 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "end-of-stream": "^1.4.3", "events-to-array": "^1.1.2", "mutate-fs": "^2.1.1", + "nock": "^13.2.9", "rimraf": "^3.0.2", "tap": "^16.0.1" }, diff --git a/test/extract.js b/test/extract.js index 5bbffeec..e6c21d81 100644 --- a/test/extract.js +++ b/test/extract.js @@ -1,6 +1,7 @@ 'use strict' const t = require('tap') +const nock = require('nock') const x = require('../lib/extract.js') const path = require('path') const fs = require('fs') @@ -11,7 +12,17 @@ const { promisify } = require('util') const rimraf = promisify(require('rimraf')) const mutateFS = require('mutate-fs') const pipeline = promisify(require('stream').pipeline) -const https = require('https') +const http = require('http') + +const tnock = (t, host, opts) => { + nock.disableNetConnect() + const server = nock(host, opts) + t.teardown(function () { + nock.enableNetConnect() + server.done() + }) + return server +} t.teardown(_ => rimraf(extractdir)) @@ -57,6 +68,9 @@ t.test('basic extracting', t => { }) t.test('ensure an open stream is not prematuraly closed', t => { + t.plan(1) + + const file = path.resolve(tars, 'long-paths.tar') const dir = path.resolve(extractdir, 'basic-with-stream') t.beforeEach(async () => { @@ -65,16 +79,51 @@ t.test('ensure an open stream is not prematuraly closed', t => { }) const check = async t => { - fs.lstatSync(dir + '/node-tar-main/LICENSE') + t.ok(fs.lstatSync(dir + '/long-path')) + await rimraf(dir) + t.end() + } + + t.test('async promisey', t => { + const stream = fs.createReadStream(file, { + highWaterMark: 1, + }) + pipeline( + stream, + x({ cwd: dir }) + ).then(_ => check(t)) + }) + + t.end() +}) + +t.test('ensure an open stream is not prematuraly closed http', t => { + t.plan(1) + + const file = path.resolve(tars, 'long-paths.tar') + const dir = path.resolve(extractdir, 'basic-with-stream-http') + + t.beforeEach(async () => { + await rimraf(dir) + await mkdirp(dir) + }) + + const check = async t => { + t.ok(fs.lstatSync(dir + '/long-path')) await rimraf(dir) t.end() } t.test('async promisey', t => { - https.get('https://codeload.github.com/npm/node-tar/tar.gz/main', (stream) => { + tnock(t, 'http://codeload.github.com/') + .get('/npm/node-tar/tar.gz/main') + .delay(250) + .reply(200, () => fs.createReadStream(file)) + + http.get('http://codeload.github.com/npm/node-tar/tar.gz/main', (stream) => { return pipeline( stream, - x({ cwd: dir }, ['node-tar-main/LICENSE']) + x({ cwd: dir }) ).then(_ => check(t)) }) })