-
Notifications
You must be signed in to change notification settings - Fork 533
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
Don't create JSON if we don't need it #1516
Conversation
a248640
to
e2deb1c
Compare
e2deb1c
to
54e48a1
Compare
54e48a1
to
b6db27a
Compare
1ab9d0a
to
5b3a1d2
Compare
test/format-test-results.spec.ts
Outdated
it('should not create any JSON unless it is needed per options', () => { | ||
const options = {} as Options; | ||
const jsonStringifySpy = jest.spyOn(JSON, 'stringify'); | ||
extractDataToSendFromResults({}, {}, options); |
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.
Would it make sense to also check that the un/expected return values are unset - ''
? Or that's covered by another test?
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.
I updated the tests... had to add a couple fixtures to do this right. LMK what you think @JackuB I'll wait before merging.
test/json.spec.ts
Outdated
describe('jsonStringifyLargeObject', () => { | ||
it('works normally with a small object', () => { | ||
const smallObject = { | ||
name: 'Brian', |
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.
name: 'Brian', | |
name: 'Mozart', |
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.
Is there a small dog named Mozart @orsagie ?
@@ -0,0 +1,20 @@ | |||
const debug = require('debug')('snyk-json'); |
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.
Considering this utility is purely for formatting output, I think it should sit next to the rest of the formatters src/cli/commands/test/formatters
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.
We were thinking we'd probably use it for other parts of the code outside of formatters eventually, so I think it would be good to keep in its own file.
5b3a1d2
to
8d6ad37
Compare
: stringifiedJsonData; | ||
|
||
let stringifiedData = ''; | ||
if (options.sarif || options.json) { |
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.
@SarahU you were last in here, so just wanted to make sure you were aware of this change. I'm pretty sure we don't need any value in stringifiedData unless --json
or --sarif
are set. The other two outputs - stringifiedJsonData
and stringifiedSarifData
are for when we want to save the JSON directly to a file using either --json-file-output
or --sarif-file-output
, respectively. @orkamara you may be interested in this as well.
This doesn't change the output for any variants of the flags I mentioned, just makes the code a bit more logical and consistent between the sarif and json options.
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.
Hey Jeff. I'm not 100% sure. Hope it's covered by a test TBH but I assume the intention was to have it set as json as default rather that leaving it empty. I don't think that's a problem in particular unless you need it to be empty in any explicit scenario, you can probably leave it as defaulting to the Json data if you want? But if it's only used in the 2 scenarios you mention then probably fine as is. For me, I didn't want to create any changes I didn't need to as I'm not sure of all the combos of flag that can be used / are supported
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.
Ok, sounds good @SarahU. This is quite complex and in the end I found some very weird scenario where a related test failed in CI only (could not reproduce on my mac) and Node v10. So I went with the original logic here just to be safe. I did quite a bit of manual testing with a bunch of options variants (--json, --json-file-output, --sarif, --sarif-file-output and combinations thereof) and I added a bunch of unit tests to that function.
test/format-test-results.spec.ts
Outdated
|
||
import { extractDataToSendFromResults } from '../src/cli/commands/test/formatters/format-test-results'; | ||
import { Options } from '../src/lib/types'; | ||
const fs = require('fs'); |
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.
Why not use import like the other files?
const fs = require('fs'); | |
import * as fs from 'fs'; |
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.
done
256193f
to
3a4bd57
Compare
3a4bd57
to
f6d7217
Compare
Expected release notes (by @maxjeffos) fixes:
|
🎉 This PR is included in version 1.425.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What does this PR do?
Two important things wrt generating the Snyk-format JSON or SARIF-format JSON output from the test command:
JSON.stringify
at all if we don't need it--json
and--sarif-file-output
), then produce both.We're also try/catching the
JSON.stringify
for the test output and, if it throws, we'll automatically try again without pretty-print. This may help in some scenarios where JSON.stringify is failing withRangeError: Invalid string length
. OtherJSON.stringify
calls are likely to be tiny by comparison so we don't really need this for those, but we could consider it in the future.Any background context you want to provide?
What are the relevant tickets?
HAMMER-182