From 6b822d814ccd5efe71929c73ad7bb410f8cab8ff Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 20 Oct 2021 00:22:07 +0900 Subject: [PATCH] fix(node/_tools): fix node test setup script (#1422) --- _util/download_file.ts | 27 +++++++++++++++++++++++++ _util/download_file_test.ts | 30 ++++++++++++++++++++++++++++ node/_tools/setup.ts | 40 +++---------------------------------- 3 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 _util/download_file.ts create mode 100644 _util/download_file_test.ts diff --git a/_util/download_file.ts b/_util/download_file.ts new file mode 100644 index 0000000000000..9259c51146bb1 --- /dev/null +++ b/_util/download_file.ts @@ -0,0 +1,27 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +import { fromFileUrl } from "../path/mod.ts"; +import { ensureFile } from "../fs/ensure_file.ts"; + +/** Download the file at the given url to the given path. */ +export async function downloadFile(url: string, fileUrl: URL) { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Request failed with status ${response.status}`); + } else if (!response.body) { + throw new Error( + `The requested download url ${url} doesn't contain an archive to download`, + ); + } else if (response.status === 404) { + throw new Error( + `The requested url "${url}" could not be found`, + ); + } + + await ensureFile(fromFileUrl(fileUrl)); + const file = await Deno.open(fileUrl, { truncate: true, write: true }); + for await (const chunk of response.body) { + Deno.writeSync(file.rid, chunk); + } + file.close(); +} diff --git a/_util/download_file_test.ts b/_util/download_file_test.ts new file mode 100644 index 0000000000000..9d368dc2581f1 --- /dev/null +++ b/_util/download_file_test.ts @@ -0,0 +1,30 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +import { join, toFileUrl } from "../path/mod.ts"; +import { delay } from "../async/delay.ts"; +import { assertEquals } from "../testing/asserts.ts"; +import { listenAndServe } from "../http/server.ts"; + +import { downloadFile } from "./download_file.ts"; + +Deno.test("[node/_tools/setup] downloadFile", async () => { + const tmpdir = await Deno.makeTempDir(); + try { + const controller = new AbortController(); + listenAndServe(":8080", () => { + // Responds with 100KB data + return new Response("0".repeat(100_000)); + }, { signal: controller.signal }); + await delay(50); + + const downloadedFile = join(tmpdir, "downloaded.txt"); + await downloadFile( + "http://localhost:8080/dummy.txt", + toFileUrl(downloadedFile), + ); + assertEquals((await Deno.readTextFile(downloadedFile)).length, 100_000); + controller.abort(); + } finally { + await Deno.remove(tmpdir, { recursive: true }); + } +}); diff --git a/node/_tools/setup.ts b/node/_tools/setup.ts index 5c743acedc391..5f2db248f8d64 100644 --- a/node/_tools/setup.ts +++ b/node/_tools/setup.ts @@ -12,6 +12,7 @@ import { ensureFile } from "../../fs/ensure_file.ts"; import { config, ignoreList } from "./common.ts"; import { Buffer } from "../../io/buffer.ts"; import { copy, readAll, writeAll } from "../../streams/conversion.ts"; +import { downloadFile } from "../../_util/download_file.ts"; /** * This script will download and extract the test files specified in the @@ -61,42 +62,6 @@ checkConfigTestFilesOrder([ ...Object.keys(config.tests).map((suite) => config.tests[suite]), ]); -/** - * This will overwrite the file if found - */ -async function downloadFile(url: string, path: string) { - console.log(`Downloading: ${url}...`); - const fileContent = await fetch(url) - .then((response) => { - if (response.ok) { - if (!response.body) { - throw new Error( - `The requested download url ${url} doesn't contain an archive to download`, - ); - } - return response.body.getIterator(); - } else if (response.status === 404) { - throw new Error( - `The requested version ${config.nodeVersion} could not be found for download`, - ); - } - throw new Error(`Request failed with status ${response.status}`); - }); - - const filePath = fromFileUrl(new URL(path, import.meta.url)); - - await ensureFile(filePath); - const file = await Deno.open(filePath, { - truncate: true, - write: true, - }); - for await (const chunk of fileContent) { - await Deno.write(file.rid, chunk); - } - file.close(); - console.log(`Downloaded: ${url} into ${path}`); -} - async function clearTests() { console.log("Cleaning up previous tests"); @@ -231,7 +196,8 @@ try { if (shouldDownload) { console.log(`Downloading ${url} in path "${archivePath}" ...`); - await downloadFile(url, archivePath); + await downloadFile(url, new URL(archivePath, import.meta.url)); + console.log(`Downloaded: ${url} into ${archivePath}`); } let shouldDecompress = false;