-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add recommended test 6.2.40 #432
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
Open
christopher-exx
wants to merge
2
commits into
main
Choose a base branch
from
feat/198-csaf-2.1-recommended-test-6.2.40
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,132 @@ | ||||||
import Ajv from 'ajv/dist/jtd.js' | ||||||
import translations from '../../lib/language_specific_translation/translations.js' | ||||||
import bcp47 from 'bcp47' | ||||||
|
||||||
const ajv = new Ajv() | ||||||
|
||||||
const inputSchema = /** @type {const} */ ({ | ||||||
additionalProperties: true, | ||||||
properties: { | ||||||
document: { | ||||||
additionalProperties: true, | ||||||
optionalProperties: { | ||||||
lang: { type: 'string' }, | ||||||
notes: { | ||||||
elements: { | ||||||
additionalProperties: true, | ||||||
optionalProperties: { | ||||||
category: { type: 'string' }, | ||||||
title: { type: 'string' }, | ||||||
group_ids: { elements: { type: 'string' } }, | ||||||
product_ids: { elements: { type: 'string' } }, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
|
||||||
const validate = ajv.compile(inputSchema) | ||||||
|
||||||
/** | ||||||
* Checks if the document language is English or unspecified | ||||||
* | ||||||
* @param {string | undefined} language The language expression to check | ||||||
* @returns {boolean} True if the language is English or unspecified, false otherwise | ||||||
*/ | ||||||
export function isLangEnglishOrUnspecified(language) { | ||||||
return !language || bcp47.parse(language)?.langtag.language.language === 'en' | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get the language specific translation of the given i18nKey | ||||||
* @param {string } lang | ||||||
* @param {string} i18nKey | ||||||
* @returns {string | undefined} - The language specific translation of the `i18nKey` | ||||||
* - or undefined if the provided language could not be parsed as a BCP 47 tag | ||||||
* - or undefined if no translation of the `i18nKey` could be found | ||||||
*/ | ||||||
export function getTranslationInDocumentLang(lang, i18nKey) { | ||||||
const language = bcp47.parse(lang)?.langtag.language.language | ||||||
|
||||||
/** @type {Record<string, Record <string,string>>}*/ | ||||||
const translationByLang = translations.translation | ||||||
|
||||||
if (!language || !translationByLang[language]) { | ||||||
return undefined | ||||||
} else { | ||||||
return translationByLang[language][i18nKey] | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Check if the given note item contains at least one of `group_ids` or `product_ids` | ||||||
* @param {{ group_ids?: string[]; product_ids?: string[]}} note | ||||||
* @return {boolean} | ||||||
*/ | ||||||
export function containsNoteGroupIdOrProductId(note) { | ||||||
return !!(note.group_ids || note.product_ids) | ||||||
} | ||||||
|
||||||
/** | ||||||
* This implements the recommended test 6.2.40 of the CSAF 2.1 standard. | ||||||
* | ||||||
/** | ||||||
* @param {any} doc | ||||||
*/ | ||||||
export function recommendedTest_6_2_40(doc) { | ||||||
/** @type { {warnings: Array<{ message: string; instancePath: string }>; | ||||||
* infos: Array<{ message: string; instancePath: string }>}} */ | ||||||
const context = { | ||||||
warnings: [], | ||||||
infos: [], | ||||||
} | ||||||
|
||||||
if (!validate(doc)) { | ||||||
return context | ||||||
} | ||||||
const documentLanguage = doc.document.lang | ||||||
doc.document.notes?.forEach((note, noteIndex) => { | ||||||
if (note.category === 'description') { | ||||||
if (isLangEnglishOrUnspecified(documentLanguage)) { | ||||||
if (note.title?.startsWith('Product Description')) { | ||||||
if (!containsNoteGroupIdOrProductId(note)) { | ||||||
context.warnings.push({ | ||||||
instancePath: `/document/notes/${noteIndex}`, | ||||||
message: | ||||||
'the given note item must include one of the elements "group_id" or "product_id"', | ||||||
}) | ||||||
} | ||||||
} | ||||||
} else { | ||||||
const translation = getTranslationInDocumentLang( | ||||||
/** @type {string} */ ( | ||||||
documentLanguage | ||||||
) /* This cast is allowed since the else statement is just called | ||||||
id documentLanguage is not undefined. Without the cast one must check here too | ||||||
if documentLanguage is not undefined, which would be a code fragment that is newer used*/, | ||||||
'product_description' | ||||||
) | ||||||
if (!translation) { | ||||||
context.infos.push({ | ||||||
instancePath: `/document/notes/${noteIndex}`, | ||||||
message: | ||||||
'no language specific translation for the "title" of this note has been recorded', | ||||||
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.
Suggested change
|
||||||
}) | ||||||
return context | ||||||
} | ||||||
if (note.title?.startsWith(translation)) { | ||||||
if (!containsNoteGroupIdOrProductId(note)) { | ||||||
context.warnings.push({ | ||||||
instancePath: `/document/notes/${noteIndex}`, | ||||||
message: | ||||||
'the given note item must include one of the elements "group_id" or "product_id"', | ||||||
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. Same as above.
Suggested change
|
||||||
}) | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
}) | ||||||
return context | ||||||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default { | ||
$schema: | ||
'https://raw.githubusercontent.com/oasis-tcs/csaf/master/csaf_2.1/test/language_specific_translation/translations_json_schema.json', | ||
translation_version: '2.1', | ||
translation: { | ||
de: { | ||
license: 'Lizenz', | ||
product_description: 'Produktbeschreibung', | ||
reasoning_for_supersession: 'Begründung für die Ersetzung', | ||
reasoning_for_withdrawal: 'Begründung für die Zurückziehung', | ||
superseding_document: 'Ersetzendes Dokument', | ||
}, | ||
}, | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -48,7 +48,6 @@ const excluded = [ | |
'6.2.39.2', | ||
'6.2.39.3', | ||
'6.2.39.4', | ||
'6.2.40', | ||
'6.2.41', | ||
'6.2.42', | ||
'6.2.43', | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import assert from 'node:assert/strict' | ||
import { recommendedTest_6_2_40 } from '../../csaf_2_1/recommendedTests/recommendedTest_6_2_40.js' | ||
|
||
describe('recommendedTest_6_2_40', function () { | ||
it('only runs on relevant documents', function () { | ||
assert.equal(recommendedTest_6_2_40({}).warnings.length, 0) | ||
}) | ||
it('skips empty objects', function () { | ||
assert.equal( | ||
recommendedTest_6_2_40({ | ||
document: { | ||
notes: [ | ||
{ | ||
category: 'description', | ||
text: 'Product A is a local time tracking tool. It is mainly used by software developers and can be connected with most modern time-tracking systems.', | ||
title: 'Product Description', | ||
}, | ||
{}, // skip this empty object | ||
], | ||
}, | ||
}).warnings.length, | ||
1 | ||
) | ||
}) | ||
it('no language specific translation', function () { | ||
assert.equal( | ||
recommendedTest_6_2_40({ | ||
document: { | ||
lang: '123456789', | ||
notes: [ | ||
{ | ||
category: 'description', | ||
product_ids: ['CSAFPID-9080700'], | ||
text: 'Produkt A is ein lokales Zeiterfassungstool. Es wird hauptsächlich von Softwareentwicklern verwendet und kann an die meisten modernen Zeiterfasssungssysteme angebunden werden.', | ||
title: 'Produkt A wird hier beschrieben', | ||
}, | ||
], | ||
}, | ||
}).infos.length, | ||
1 | ||
) | ||
}) | ||
}) |
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.
If you come up with a better message, feel free to suggest it.