-
Notifications
You must be signed in to change notification settings - Fork 189
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
Fix quoting of string columns in csv #1379
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c8390c
Fix quoting of string columns in csv
aeisenberg c10da7f
Update Changelog
aeisenberg fe7eb07
Don't choose a non-existent result set for csv viewing
aeisenberg aa270e5
Refactor exportCsvResults and create test
aeisenberg a3a0513
Handle quote escaping in csv export
aeisenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { expect } from 'chai'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import * as sinon from 'sinon'; | ||
import { Uri } from 'vscode'; | ||
|
||
import { QueryEvaluationInfo } from '../../run-queries'; | ||
import { Severity, compileQuery } from '../../pure/messages'; | ||
import * as config from '../../config'; | ||
import { tmpDir } from '../../helpers'; | ||
import { QueryServerClient } from '../../queryserver-client'; | ||
import { CodeQLCliServer } from '../../cli'; | ||
import { SELECT_QUERY_NAME } from '../../contextual/locationFinder'; | ||
|
||
describe('run-queries', () => { | ||
let sandbox: sinon.SinonSandbox; | ||
|
@@ -53,6 +58,85 @@ describe('run-queries', () => { | |
expect(info.canHaveInterpretedResults()).to.eq(true); | ||
}); | ||
|
||
[SELECT_QUERY_NAME, 'other'].forEach(resultSetName => { | ||
it(`should export csv results for result set ${resultSetName}`, async () => { | ||
const csvLocation = path.join(tmpDir.name, 'test.csv'); | ||
const qs = createMockQueryServerClient( | ||
createMockCliServer({ | ||
bqrsInfo: [{ 'result-sets': [{ name: resultSetName }, { name: 'hucairz' }] }], | ||
bqrsDecode: [{ | ||
columns: [{ kind: 'NotString' }, { kind: 'String' }], | ||
tuples: [['a', 'b'], ['c', 'd']], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's expand the tuples here to include some quote characters and make sure those get escaped correctly. |
||
next: 1 | ||
}, { | ||
// just for fun, give a different set of columns here | ||
// this won't happen with the real CLI, but it's a good test | ||
columns: [{ kind: 'String' }, { kind: 'NotString' }, { kind: 'StillNotString' }], | ||
tuples: [['a', 'b', 'c']] | ||
}] | ||
}) | ||
); | ||
const info = createMockQueryInfo(); | ||
const promise = info.exportCsvResults(qs, csvLocation); | ||
|
||
const result = await promise; | ||
expect(result).to.eq(true); | ||
|
||
const csv = fs.readFileSync(csvLocation, 'utf8'); | ||
expect(csv).to.eq('a,"b"\nc,"d"\n"a",b,c\n'); | ||
|
||
// now verify that we are using the expected result set | ||
expect((qs.cliServer.bqrsDecode as sinon.SinonStub).callCount).to.eq(2); | ||
expect((qs.cliServer.bqrsDecode as sinon.SinonStub).getCall(0).args[1]).to.eq(resultSetName); | ||
}); | ||
}); | ||
|
||
it('should export csv results with characters that need to be escaped', async () => { | ||
const csvLocation = path.join(tmpDir.name, 'test.csv'); | ||
const qs = createMockQueryServerClient( | ||
createMockCliServer({ | ||
bqrsInfo: [{ 'result-sets': [{ name: SELECT_QUERY_NAME }, { name: 'hucairz' }] }], | ||
bqrsDecode: [{ | ||
columns: [{ kind: 'NotString' }, { kind: 'String' }], | ||
// We only escape string columns. In practice, we will only see quotes in strings, but | ||
// it is a good test anyway. | ||
tuples: [ | ||
['"a"', '"b"'], | ||
['c,xxx', 'd,yyy'], | ||
['aaa " bbb', 'ccc " ddd'], | ||
[true, false], | ||
[123, 456], | ||
[123.98, 456.99], | ||
], | ||
}] | ||
}) | ||
); | ||
const info = createMockQueryInfo(); | ||
const promise = info.exportCsvResults(qs, csvLocation); | ||
|
||
const result = await promise; | ||
expect(result).to.eq(true); | ||
|
||
const csv = fs.readFileSync(csvLocation, 'utf8'); | ||
expect(csv).to.eq('"a","""b"""\nc,xxx,"d,yyy"\naaa " bbb,"ccc "" ddd"\ntrue,"false"\n123,"456"\n123.98,"456.99"\n'); | ||
|
||
// now verify that we are using the expected result set | ||
expect((qs.cliServer.bqrsDecode as sinon.SinonStub).callCount).to.eq(1); | ||
expect((qs.cliServer.bqrsDecode as sinon.SinonStub).getCall(0).args[1]).to.eq(SELECT_QUERY_NAME); | ||
}); | ||
|
||
it('should handle csv exports for a query with no result sets', async () => { | ||
const csvLocation = path.join(tmpDir.name, 'test.csv'); | ||
const qs = createMockQueryServerClient( | ||
createMockCliServer({ | ||
bqrsInfo: [{ 'result-sets': [] }] | ||
}) | ||
); | ||
const info = createMockQueryInfo(); | ||
const result = await info.exportCsvResults(qs, csvLocation); | ||
expect(result).to.eq(false); | ||
}); | ||
|
||
describe('compile', () => { | ||
it('should compile', async () => { | ||
const info = createMockQueryInfo(); | ||
|
@@ -116,7 +200,7 @@ describe('run-queries', () => { | |
); | ||
} | ||
|
||
function createMockQueryServerClient() { | ||
function createMockQueryServerClient(cliServer?: CodeQLCliServer): QueryServerClient { | ||
return { | ||
config: { | ||
timeoutSecs: 5 | ||
|
@@ -131,7 +215,20 @@ describe('run-queries', () => { | |
})), | ||
logger: { | ||
log: sandbox.spy() | ||
} | ||
}; | ||
}, | ||
cliServer | ||
} as unknown as QueryServerClient; | ||
} | ||
|
||
function createMockCliServer(mockOperations: Record<string, any[]>): CodeQLCliServer { | ||
const mockServer: Record<string, any> = {}; | ||
for (const [operation, returns] of Object.entries(mockOperations)) { | ||
mockServer[operation] = sandbox.stub(); | ||
returns.forEach((returnValue, i) => { | ||
mockServer[operation].onCall(i).resolves(returnValue); | ||
}); | ||
} | ||
|
||
return mockServer as unknown as CodeQLCliServer; | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be displaying an error in the
else
case here? I see wereject()
where we were previously usingshowAndLogErrorMessage()
but as far as I can tell we are then swallowing that error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aeisenberg I don't think you've addressed this comment. It's less blocking the other others, so I'm happy to approve without any changes here if you don't think any changes are needed, but just checking it wasn't missed accidentally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh...sorry. I forgot to comment here. The error is not being swallowed. Take a look at
commandRunnerWithProgress
, you can see that there is an error barrier there that catches and logs all thrown and rejected errors. So, you don't ever need to explicitly catch and log an error when you're inside a command as long as that command is wrapped incommandRunnerWithProgress
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And, indeed, when I try this out, forcing an error inside of
exportCsvResults
, the error is logged appropriately.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, thanks for the explanation 🙂