Skip to content

Commit

Permalink
fix: Fix unit tests (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 authored Feb 2, 2023
1 parent 34aff89 commit f89d9ef
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/commands/files/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ class FilesDownloadCommand extends BoxCommand {
}

let stream = await this.client.files.getReadStream(args.id, options);
let output;
try {
let output = fs.createWriteStream(filePath);
output = fs.createWriteStream(filePath);
stream.pipe(output);
} catch (ex) {
throw new BoxCLIError(`Could not download to destination file ${filePath}`, ex);
Expand All @@ -69,7 +70,7 @@ class FilesDownloadCommand extends BoxCommand {
/* eslint-disable promise/avoid-new */
// We need to await the end of the stream to avoid a race condition here
await new Promise((resolve, reject) => {
stream.on('end', resolve);
output.on('close', resolve);
stream.on('error', reject);
});
clearInterval(intervalUpdate);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/files/versions/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class FilesVersionsDownloadCommand extends BoxCommand {
options.version = args.fileVersionID;

let stream = await this.client.files.getReadStream(args.fileID, options);
let output;
try {
let output = fs.createWriteStream(filePath);
output = fs.createWriteStream(filePath);
stream.pipe(output);
} catch (ex) {
throw new BoxCLIError(`Could not download to destination file ${filePath}`, ex);
Expand All @@ -52,7 +53,7 @@ class FilesVersionsDownloadCommand extends BoxCommand {
/* eslint-disable promise/avoid-new */
// We need to await the end of the stream to avoid a race condition here
await new Promise((resolve, reject) => {
stream.on('end', resolve);
output.on('close', resolve);
stream.on('error', reject);
});
this.info(`Downloaded file ${fileName}`);
Expand Down
23 changes: 22 additions & 1 deletion src/commands/files/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,28 @@ class FilesZipCommand extends BoxCommand {
}

let output = fs.createWriteStream(filePath);
let downloadStatus = await this.client.files.downloadZip(fileName, flags.item, output);

let downloadStatus;
let outputFinished = false;
/* eslint-disable promise/avoid-new */
await new Promise((resolve, reject) => {
/* eslint-disable promise/always-return */
this.client.files.downloadZip(fileName, flags.item, output).then((status) => {
downloadStatus = status;
if (outputFinished) {
resolve();
}
})
.catch(reject);
output.on('close', () => {
output.close();
outputFinished = true;
if (downloadStatus) {
resolve();
}
});
output.on('error', reject);
});
await this.output(downloadStatus);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/folders/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ const utils = require('../../util');
*/
function saveFileToDisk(folderPath, file, stream) {

let output;
try {
let output = fs.createWriteStream(path.join(folderPath, file.path));
output = fs.createWriteStream(path.join(folderPath, file.path));
stream.pipe(output);
} catch (ex) {
throw new BoxCLIError(`Error downloading file ${file.id} to ${file.path}`, ex);
Expand All @@ -33,7 +34,7 @@ function saveFileToDisk(folderPath, file, stream) {
/* eslint-disable promise/avoid-new */
// We need to await the end of the stream to avoid a race condition here
return new Promise((resolve, reject) => {
stream.on('end', resolve);
output.on('close', resolve);
stream.on('error', reject);
});
/* eslint-enable promise/avoid-new */
Expand Down Expand Up @@ -184,7 +185,6 @@ class FoldersDownloadCommand extends BoxCommand {
/* eslint-disable promise/avoid-new */
// We need to await the end of the stream to avoid a race condition here
return new Promise((resolve, reject) => {
output.on('end', resolve);
output.on('close', resolve);
output.on('error', reject);
});
Expand Down
6 changes: 3 additions & 3 deletions test/commands/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2349,17 +2349,17 @@ describe('Files', () => {
)
.nock(TEST_DOWNLOAD_ROOT, api => api
.get(downloadUrl)
.reply(200, function () {
.reply(200, function() {
return fs.createReadStream(testFilePath, 'utf8');
})
)
.nock(TEST_API_ROOT, api => api
.get(statusUrl)
.reply(200, downloadStatusFixture)
)
.do(() => {
.do(async() => {
/* eslint-disable no-sync */
fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, fileName), 'foo', 'utf8');
await fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, fileName), 'foo');
/* eslint-enable no-sync */
})
.stdout()
Expand Down
26 changes: 13 additions & 13 deletions test/commands/folders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,20 +1486,20 @@ describe('Folders', () => {
}
};

async function getDirectoryContents(folderPath) {

function getDirectoryContents(folderPath) {
/* eslint-disable no-sync */
let obj = {};
let folderContents = await fs.readdir(folderPath);
folderContents.forEach(async item => {
let folderContents = fs.readdirSync(folderPath);
folderContents.forEach(item => {
let itemPath = path.join(folderPath, item);
let stat = await fs.stat(itemPath);
let stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
obj[item] = await getDirectoryContents(itemPath);
obj[item] = getDirectoryContents(itemPath);
} else {
obj[item] = await fs.readFile(itemPath, 'utf8');
obj[item] = fs.readFileSync(itemPath, 'utf8');
}
});

/* eslint-enable no-sync */
return obj;
}

Expand Down Expand Up @@ -1534,7 +1534,7 @@ describe('Folders', () => {
])
.it('should download folder to specified path on disk when called with destination flag', async ctx => {
let folderPath = path.join(downloadPath, folderName);
let actualContents = await getDirectoryContents(folderPath);
let actualContents = getDirectoryContents(folderPath);
await fs.remove(downloadPath);

assert.deepEqual(actualContents, expectedContents);
Expand Down Expand Up @@ -1588,7 +1588,7 @@ describe('Folders', () => {
manyFilesExpectedContents[`file ${i}.txt`] = `File ${i} contents`;
}
let folderPath = path.join(downloadPath, folderName);
let actualContents = await getDirectoryContents(folderPath);
let actualContents = getDirectoryContents(folderPath);
await fs.remove(downloadPath);

assert.deepEqual(actualContents, manyFilesExpectedContents);
Expand Down Expand Up @@ -1621,7 +1621,7 @@ describe('Folders', () => {
])
.it('should only download files in top-level folder when --depth=0 flag is passed', async ctx => {
let folderPath = path.join(downloadPath, folderName);
let actualContents = await getDirectoryContents(folderPath);
let actualContents = getDirectoryContents(folderPath);
await fs.remove(downloadPath);

assert.deepEqual(actualContents, _.omit(expectedContents, 'subfolder'));
Expand Down Expand Up @@ -1725,7 +1725,7 @@ describe('Folders', () => {
])
.it('should download a folder a non-existent path', async(ctx) => {
let folderPath = path.join(destination, folderName);
let actualContents = await getDirectoryContents(folderPath);
let actualContents = getDirectoryContents(folderPath);
await fs.remove(destination);

assert.deepEqual(actualContents, expectedContents);
Expand Down Expand Up @@ -1762,7 +1762,7 @@ describe('Folders', () => {
])
.it('should download a folder to a default destination', async(ctx) => {
let folderPath = path.join(DEFAULT_DOWNLOAD_PATH, folderName);
let actualContents = await getDirectoryContents(folderPath);
let actualContents = getDirectoryContents(folderPath);
await fs.remove(folderPath);

assert.deepEqual(actualContents, expectedContents);
Expand Down

0 comments on commit f89d9ef

Please sign in to comment.