From 31126195d15a5e79a6d74858bb45b8a1683994c2 Mon Sep 17 00:00:00 2001 From: Artur Jankowski Date: Wed, 22 Nov 2023 12:32:08 +0100 Subject: [PATCH] fix: Fix the functionality of the overwrite flag (#513) --- src/commands/files/download.js | 8 ++- src/commands/files/versions/download.js | 5 ++ src/commands/files/zip.js | 8 ++- test/commands/files.test.js | 77 +++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/commands/files/download.js b/src/commands/files/download.js index 4d429fa8..484f55b7 100644 --- a/src/commands/files/download.js +++ b/src/commands/files/download.js @@ -28,6 +28,11 @@ class FilesDownloadCommand extends BoxCommand { if (!flags.overwrite && fs.existsSync(filePath)) { /* eslint-enable no-sync */ + if (flags.overwrite === false) { + this.info(`Downloading the file will not occur because the file ${filePath} already exists, and the --no-overwrite flag is set.`); + return; + } + let shouldOverwrite = await this.confirm(`File ${filePath} already exists — overwrite?`); if (!shouldOverwrite) { @@ -99,8 +104,7 @@ FilesDownloadCommand.flags = { }), overwrite: flags.boolean({ description: 'Overwrite a file if it already exists', - allowNo: true, - default: false + allowNo: true }), 'save-as': flags.string({ description: 'The filename used when saving the file' diff --git a/src/commands/files/versions/download.js b/src/commands/files/versions/download.js index 75f3eb5b..807daf61 100644 --- a/src/commands/files/versions/download.js +++ b/src/commands/files/versions/download.js @@ -29,6 +29,11 @@ class FilesVersionsDownloadCommand extends BoxCommand { if (!flags.overwrite && fs.existsSync(filePath)) { /* eslint-enable no-sync */ + if (flags.overwrite === false) { + this.info(`Downloading the file will not occur because the file ${filePath} already exists, and the --no-overwrite flag is set.`); + return; + } + let shouldOverwrite = await this.confirm(`File ${filePath} already exists — overwrite?`); if (!shouldOverwrite) { diff --git a/src/commands/files/zip.js b/src/commands/files/zip.js index 8c32b37d..f7b0fe67 100644 --- a/src/commands/files/zip.js +++ b/src/commands/files/zip.js @@ -28,6 +28,11 @@ class FilesZipCommand extends BoxCommand { if (!flags.overwrite && fs.existsSync(filePath)) { /* eslint-enable no-sync */ + if (flags.overwrite === false) { + this.info(`Downloading the file will not occur because the file ${filePath} already exists, and the --no-overwrite flag is set.`); + return; + } + let shouldOverwrite = await this.confirm(`File ${filePath} already exists — overwrite?`); if (!shouldOverwrite) { @@ -89,8 +94,7 @@ FilesZipCommand.flags = { }), overwrite: flags.boolean({ description: 'Overwrite a zip file if it already exists', - allowNo: true, - default: false + allowNo: true }), }; diff --git a/test/commands/files.test.js b/test/commands/files.test.js index 06427c07..662da8ae 100644 --- a/test/commands/files.test.js +++ b/test/commands/files.test.js @@ -1925,6 +1925,32 @@ describe('Files', () => { /* eslint-enable no-sync */ assert.ok(downloadContent.equals(expectedContent)); }); + test + .nock(TEST_API_ROOT, api => api + .get(`/2.0/files/${fileId}`) + .reply(200, getFileFixture) + ) + .do(() => { + /* eslint-disable no-sync */ + fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName), 'foo', 'utf8'); + /* eslint-enable no-sync */ + }) + .stdout() + .stderr() + .command([ + 'files:download', + fileId, + `--save-as=${saveAsFileName}`, + '--no-overwrite', + '--token=test' + ]) + .it('should skip downloading when file exists and --no-overwrite flag is used', ctx => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName); + fs.unlinkSync(downloadedFilePath); + /* eslint-enable no-sync */ + assert.equal(ctx.stderr, `Downloading the file will not occur because the file ${downloadedFilePath} already exists, and the --no-overwrite flag is set.${os.EOL}`); + }); test .nock(TEST_API_ROOT, api => api .get(`/2.0/files/${fileId}`) @@ -2198,6 +2224,33 @@ describe('Files', () => { /* eslint-enable no-sync */ assert.ok(downloadContent.equals(expectedContent)); }); + test + .nock(TEST_API_ROOT, api => api + .get(`/2.0/files/${fileId}`) + .reply(200, getFileFixture) + ) + .do(() => { + /* eslint-disable no-sync */ + fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName), 'foo', 'utf8'); + /* eslint-enable no-sync */ + }) + .stdout() + .stderr() + .command([ + 'files:versions:download', + fileId, + fileVersionID, + `--save-as=${saveAsFileName}`, + '--no-overwrite', + '--token=test' + ]) + .it('should skip downloading when file exists and --no-overwrite flag is used', ctx => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName); + fs.unlinkSync(downloadedFilePath); + /* eslint-enable no-sync */ + assert.equal(ctx.stderr, `Downloading the file will not occur because the file ${downloadedFilePath} already exists, and the --no-overwrite flag is set.${os.EOL}`); + }); }); describe('files:zip', () => { let fileName = 'test.zip', @@ -2367,5 +2420,29 @@ describe('Files', () => { assert.ok(downloadContent.equals(expectedContent)); assert.equal(ctx.stdout, downloadStatusFixture); }); + test + .do(async() => { + /* eslint-disable no-sync */ + await fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, fileName), 'foo'); + /* eslint-enable no-sync */ + }) + .stdout() + .stderr() + .command([ + 'files:zip', + fileName, + `--item=${items[0].type}:${items[0].id}`, + `--item=${items[1].type}:${items[1].id}`, + '--no-overwrite', + '--json', + '--token=test' + ]) + .it('should skip downloading zip file when exists and --no-overwrite flag is used', ctx => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(DEFAULT_DOWNLOAD_PATH, fileName); + fs.unlinkSync(downloadedFilePath); + /* eslint-enable no-sync */ + assert.equal(ctx.stderr, `Downloading the file will not occur because the file ${downloadedFilePath} already exists, and the --no-overwrite flag is set.${os.EOL}`); + }); }); });