-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ec regex to extract voilation reports from tkn logs
- Loading branch information
Showing
5 changed files
with
110 additions
and
35 deletions.
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
86 changes: 64 additions & 22 deletions
86
src/components/EnterpriseContract/__tests__/utils.spec.ts
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,27 +1,69 @@ | ||
import { extractEcResultsFromTaskRunLogs } from '../utils'; | ||
|
||
describe('extractEcResultsFromTaskRunLogs', () => { | ||
it('should extract logs from string ', () => { | ||
expect( | ||
extractEcResultsFromTaskRunLogs(`asdfcdfadsf | ||
[report-json] { "components": [] } | ||
`), | ||
).toEqual({ components: [] }); | ||
expect( | ||
extractEcResultsFromTaskRunLogs(`asdfcdfadsf | ||
[report-json] { "components": | ||
[report-json] [] } | ||
safkjasdfj 9034823 0dju@#$@# | ||
`), | ||
).toEqual({ components: [] }); | ||
|
||
expect( | ||
extractEcResultsFromTaskRunLogs(`asdfcdfadsf | ||
[report] { "components": [] } | ||
[report-json] { "components": | ||
[report-json] [] } | ||
safkjasdfj 9034823 0dju@#$@# | ||
`), | ||
).toEqual({ components: [] }); | ||
test('should extract and parse JSON from logs correctly', () => { | ||
const logs = ` | ||
step-vulnerabilities :- | ||
Lorem Ipsum some logs | ||
step-report-json :- | ||
{"success":true,"components":[{"name":"component-name","details":"example"}]} | ||
step-something-else :- | ||
Some other logs | ||
`; | ||
|
||
const expectedResult = { | ||
success: true, | ||
components: [ | ||
{ | ||
name: 'component-name', | ||
details: 'example', | ||
}, | ||
], | ||
}; | ||
expect(extractEcResultsFromTaskRunLogs(logs)).toEqual(expectedResult); | ||
}); | ||
|
||
test('should return null if JSON parsing fails', () => { | ||
const logs = ` | ||
step-report-json :- | ||
{invalid JSON} | ||
`; | ||
expect(extractEcResultsFromTaskRunLogs(logs)).toBeNull(); | ||
}); | ||
|
||
test('should return null if step-report-json is missing', () => { | ||
const logs = ` | ||
step-vulnerabilities :- | ||
Lorem Ipsum some logs | ||
`; | ||
|
||
expect(extractEcResultsFromTaskRunLogs(logs)).toBeNull(); | ||
}); | ||
|
||
test('should handle multiple step-report-json blocks and extract the first one', () => { | ||
const logs = ` | ||
step-report-json :- | ||
{"success":true,"components":[{"name":"first-component","details":"example"}]} | ||
step-report-json :- | ||
{"success":false,"components":[{"name":"second-component","details":"example"}]} | ||
`; | ||
|
||
const expectedResult = { | ||
success: true, | ||
components: [ | ||
{ | ||
name: 'first-component', | ||
details: 'example', | ||
}, | ||
], | ||
}; | ||
expect(extractEcResultsFromTaskRunLogs(logs)).toEqual(expectedResult); | ||
}); | ||
|
||
test('should handle empty logs and return null', () => { | ||
expect(extractEcResultsFromTaskRunLogs(``)).toBeNull(); | ||
}); | ||
}); |
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