Skip to content

Commit

Permalink
fix: Fix the functionality of the overwrite flag (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjankowski authored Nov 22, 2023
1 parent 2b40963 commit f4bf7af
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/commands/files/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions src/commands/files/versions/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/files/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -89,8 +94,7 @@ FilesZipCommand.flags = {
}),
overwrite: flags.boolean({
description: 'Overwrite a zip file if it already exists',
allowNo: true,
default: false
allowNo: true
}),
};

Expand Down
77 changes: 77 additions & 0 deletions test/commands/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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}`);
});
});
});

0 comments on commit f4bf7af

Please sign in to comment.