Skip to content

Commit

Permalink
fix(node/_tools): fix node test setup script (denoland#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored and traceypooh committed Nov 14, 2021
1 parent cd2bccf commit 6b822d8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
27 changes: 27 additions & 0 deletions _util/download_file.ts
Original file line number Diff line number Diff line change
@@ -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();
}
30 changes: 30 additions & 0 deletions _util/download_file_test.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
});
40 changes: 3 additions & 37 deletions node/_tools/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6b822d8

Please sign in to comment.