feat(download): create directories for saveAs (#3249)

This is to match the behavior for screenshots path added in #3247.
This commit is contained in:
Ross Wollman 2020-07-30 23:21:03 -07:00 committed by GitHub
parent 93056ed8ef
commit ce0ddd270b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 8 deletions

View File

@ -3215,7 +3215,7 @@ Returns download error if any.
Returns path to the downloaded file in case of successful download.
#### download.saveAs(path)
- `path` <[string]> Path where the download should be saved. The directory structure MUST exist as `saveAs` will not create it.
- `path` <[string]> Path where the download should be saved.
- returns: <[Promise]>
Saves the download to a user-specified path.

View File

@ -92,6 +92,8 @@ export class Download {
async _saveAs(downloadPath: string) {
const fileName = path.join(this._downloadsPath, this._uuid);
// This will harmlessly throw on windows if the dirname is the root directory.
await util.promisify(fs.mkdir)(path.dirname(downloadPath), {recursive: true}).catch(() => {});
await util.promisify(fs.copyFile)(fileName, downloadPath);
}

View File

@ -134,19 +134,17 @@ describe('Download', function() {
expect(fs.readFileSync(userPath).toString()).toBe('Hello world');
await page.close();
});
it('should error when saving to non-existent user-specified path', async({persistentDirectory, browser, server}) => {
it('should create subdirectories when saving to non-existent user-specified path', async({persistentDirectory, browser, server}) => {
const page = await browser.newPage({ acceptDownloads: true });
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
const [ download ] = await Promise.all([
page.waitForEvent('download'),
page.click('a')
]);
const nonExistentUserPath = path.join(persistentDirectory, "does-not-exist","download.txt");
const { message } = await download.saveAs(nonExistentUserPath).catch(e => e);
expect(message).toContain('ENOENT');
expect(message).toContain('copyfile');
expect(message).toContain('no such file or directory');
expect(message).toContain('does-not-exist');
const nestedPath = path.join(persistentDirectory, "these", "are", "directories", "download.txt");
await download.saveAs(nestedPath)
expect(fs.existsSync(nestedPath)).toBeTruthy();
expect(fs.readFileSync(nestedPath).toString()).toBe('Hello world');
await page.close();
});
it('should error when saving with downloads disabled', async({persistentDirectory, browser, server}) => {