Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --all flag for search to return all results #336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ function parseMetadataValue(value) {
class SearchCommand extends BoxCommand {
async run() {
const { flags, args } = this.parse(SearchCommand);

if (flags.all && flags.limit) {
throw new BoxCLIError('--all and --limit flags cannot be used together.');
}

let options = {
limit: RESULTS_LIMIT,
};

if (flags.scope) {
options.scope = flags.scope;
}
Expand Down Expand Up @@ -153,7 +159,7 @@ class SearchCommand extends BoxCommand {
let limitedResults = [];
for await (let result of { [Symbol.asyncIterator]: () => results }) {
let numResults = limitedResults.push(result);
if (numResults >= itemsLimit) {
if (!flags.all && numResults >= itemsLimit) {
break;
}
}
Expand Down Expand Up @@ -288,6 +294,9 @@ SearchCommand.flags = {
limit: flags.integer({
description: 'Defines the maximum number of items to return. Default value is 100.',
}),
all: flags.boolean({
description: 'Returns all search results.',
}),
'include-recent-shared-links': flags.boolean({
description: 'Returns shared links that the user recently accessed'
}),
Expand Down
71 changes: 68 additions & 3 deletions test/commands/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const leche = require('leche');

describe('Search', () => {

describe('search', () => {
let query = 'Test',
let query = 'Test',
fixture = getFixture('search/get_search_query_page_1'),
fixture2 = getFixture('search/get_search_query_page_2'),
jsonOutput = getFixture('output/search_json.txt');
jsonOutput = getFixture('output/search_json.txt'),
jsonOutputLimitedTo5 = getFixture('output/search_json_limit_5.txt');

describe('search', () => {

test
.nock(TEST_API_ROOT, api => api
Expand Down Expand Up @@ -314,5 +316,68 @@ describe('Search', () => {
assert.equal(ctx.stdout, jsonOutput);
});
});

test
.nock(TEST_API_ROOT, api => api
.get('/2.0/search')
.query({
query,
limit: 100,
})
.reply(200, fixture)
.get('/2.0/search')
.query({
query,
limit: 100,
offset: 5
})
.reply(200, fixture2)
)
.stdout()
.command([
'search',
query,
'--json',
'--all',
'--token=test'
])
.it('should return all results when --all flag provided', ctx => {
assert.equal(ctx.stdout, jsonOutput);
});

test
.nock(TEST_API_ROOT, api => api
.get('/2.0/search')
.query({
query,
limit: 100,
})
.reply(200, fixture)
)
.stdout()
.command([
'search',
query,
'--json',
'--limit=5',
'--token=test'
])
.it('should return limited results when --limit flag provided', ctx => {
assert.equal(ctx.stdout, jsonOutputLimitedTo5);
});
});
describe('fails', () => {
test
.stderr()
.command([
'search',
query,
'--limit=80',
'--all',
'--token=test'
])
.it('when both --all and --limit flag provided', ctx => {
assert.include(ctx.stderr, '--all and --limit flags cannot be used together.');
});
});
});
Loading