Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
37 changes: 30 additions & 7 deletions src/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export async function readConfig(configPath) {
const presetsFile = await findPresetsFile(entries)
const translationsFile = await findTranslationsFile(entries)
const metadataFile = await findMetadataFile(entries)
const iconEntries = getIconEntries(entries)
assert(
isValidConfigFile(metadataFile),
`invalid or missing config file version ${metadataFile.fileVersion}. We support version ${SUPPORTED_CONFIG_VERSION}}`
Expand All @@ -75,13 +76,6 @@ export async function readConfig(configPath) {
/** @type {IconData | undefined} */
let icon

// we sort the icons by filename so we can group variants together
const iconEntries = entries
.filter((entry) => entry.filename.match(/^icons\/([^/]+)$/))
.sort((icon, nextIcon) =>
icon.filename.localeCompare(nextIcon.filename)
)

for (const entry of iconEntries) {
if (entry.uncompressedSize > MAX_ICON_SIZE) {
warnings.push(
Expand Down Expand Up @@ -193,6 +187,26 @@ export async function readConfig(configPath) {
presetValue[key] = preset[key]
}
}
// check that preset references existing icon
const iconFilenames = new Set(
iconEntries.map((icon) => {
const matches = path
.basename(icon.filename)
.match(/([a-zA-Z--0-]+)-/)
if (matches) {
const [_, name] = matches
return name
}
})
)
if ('icon' in preset && typeof preset.icon === 'string') {
if (!iconFilenames.has(preset.icon)) {
throw new Error(
`preset references icon with name ${preset.icon} but file doesn't exist`
)
}
}

if (!validate('preset', presetValue)) {
warnings.push(new Error(`Invalid preset ${preset.name}`))
continue
Expand Down Expand Up @@ -472,6 +486,15 @@ function translateMessageObject(warnings) {
}
}

/**
* @param {ReadonlyArray<Entry>} entries
*/
function getIconEntries(entries) {
return entries
.filter((entry) => entry.filename.match(/^icons\/([^/]+)$/))
.sort((icon, nextIcon) => icon.filename.localeCompare(nextIcon.filename))
}

/**
* @param {string} filename
* @param {Buffer} buf
Expand Down
9 changes: 9 additions & 0 deletions tests/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ test('config import - presets', async () => {
'the second error is because the preset is null'
)

config = await readConfig(
'./tests/fixtures/config/invalidIconNameOnPreset.zip'
)

assert.throws(
() => arrayFrom(config.presets()),
/preset references icon with name /
)

config = await readConfig('./tests/fixtures/config/validPreset.zip')
for (const preset of config.presets()) {
assert.equal(preset.value.schemaName, 'preset', `schemaName is 'preset'`)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/fixtures/config/invalidIconNameOnPreset/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test-config",
"buildDate": "2024-07-29T17:37:23.382Z",
"fileVersion": "1.0"
}
47 changes: 47 additions & 0 deletions tests/fixtures/config/invalidIconNameOnPreset/presets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"fields":{
"nombre-monitor": {
"tagKey": "nombre-monitor",
"type": "text",
"label": "Nombre del Monitor o Mapeador",
"universal": true,
"placeholder": "¿Quién está haciendo esta observación?"
}
},
"presets":{
"access-point":{
"icon": "access-pont",
"fields": [
"nombre-monitor"
],
"geometry": [
"point",
"area",
"line"
],
"sort": 1,
"tags": {
"type": "threat",
"threat": "access-point"
},
"name": "Punto de entrada"
},
"plant":{
"icon": "plant",
"fields": [
"nombre-monitor"
],
"sort": 3,
"geometry": [
"point",
"area"
],
"tags": {
"type": "natural",
"natural": "plant"
},
"terms": [],
"name": "Planta"
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading