forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node/_tools): fix node test setup script (denoland#1422)
- Loading branch information
1 parent
cd2bccf
commit 6b822d8
Showing
3 changed files
with
60 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters