Skip to content
代码片段 群组 项目
未验证 提交 c43ceac6 编辑于 作者: Timotej Ecimovic's avatar Timotej Ecimovic 提交者: GitHub
浏览文件

Clean up some aspects of tests processing for Matter (#676)

* Generalize the JSON file inclusion.
* Add simple test for data gathering.
上级 bbe83854
No related branches found
No related tags found
无相关合并请求
...@@ -646,7 +646,7 @@ async function chip_tests(listOrJson, options) { ...@@ -646,7 +646,7 @@ async function chip_tests(listOrJson, options) {
if (listOrJson.endsWith('.json')) { if (listOrJson.endsWith('.json')) {
// It's a JSON file. Read it. // It's a JSON file. Read it.
let f = path.join(path.dirname(global.templatePath), listOrJson); let f = path.join(path.dirname(global.templatePath), listOrJson);
items = util.collectTests(f); items = await util.collectJsonData(f);
} else { } else {
items = listOrJson.split(','); items = listOrJson.split(',');
} }
......
...@@ -599,36 +599,40 @@ function disable(testName) { ...@@ -599,36 +599,40 @@ function disable(testName) {
} }
/** /**
* Utility method that collects tests from a JSON file. * Utility method that collects data from a JSON file.
* JSON file supports following special keys: *
* JSON file is formatted as a bunch of keyed strings:
* "someKey": [ "a", "b", "c"]
* Then it supports following special keys:
* "include": "path/to/json/file" - includes the said JSON file * "include": "path/to/json/file" - includes the said JSON file
* "disable": [ "test", "test1" ...] - disables the specified tests * "disable": [ "x", "y" ...] - disables the specified data points
* "collection": ["key", "key2", ...] - collects final list of tests * "collection": ["key", "key2", ...] - collects final list of data points
* *
* @param {*} jsonFile * @param {*} jsonFile
*/ */
function collectTests(jsonFile, recursiveLevel = 0) { async function collectJsonData(jsonFile, recursiveLevel = 0) {
if (recursiveLevel > 20) { if (recursiveLevel > 20) {
// Prevent infinite recursion // Prevent infinite recursion
throw new Error(`Recursion too deep in tests inclusion.`) throw new Error(`Recursion too deep in JSON file inclusion.`)
} }
let data = fs.readFileSync(jsonFile) let rawData = await fsp.readFile(jsonFile)
let tests = JSON.parse(data) let jsonData = JSON.parse(rawData)
let collectedTests = [] let collectedData = []
if ('include' in tests) { if ('include' in jsonData) {
let f = path.join(path.dirname(jsonFile), tests.include) let f = path.join(path.dirname(jsonFile), jsonData.include)
collectedTests.push(...collectTests(f, recursiveLevel++)) let includedData = await collectJsonData(f, recursiveLevel++)
collectedData.push(...includedData)
} }
if ('collection' in tests) { if ('collection' in jsonData) {
collectedTests.push(...tests.collection.map((c) => tests[c]).flat(1)) collectedData.push(...jsonData.collection.map((c) => jsonData[c]).flat(1))
} }
if ('disable' in tests) { if ('disable' in jsonData) {
collectedTests = collectedTests.filter( collectedData = collectedData.filter(
(test) => !tests.disable.includes(test) (test) => !jsonData.disable.includes(test)
) )
} }
collectedTests.disable = disable.bind(collectedTests) collectedData.disable = disable.bind(collectedData)
return collectedTests return collectedData
} }
exports.createBackupFile = createBackupFile exports.createBackupFile = createBackupFile
...@@ -649,4 +653,4 @@ exports.parseXml = parseXml ...@@ -649,4 +653,4 @@ exports.parseXml = parseXml
exports.readFileContentAndCrc = readFileContentAndCrc exports.readFileContentAndCrc = readFileContentAndCrc
exports.duration = duration exports.duration = duration
exports.mainOrSecondaryInstance = mainOrSecondaryInstance exports.mainOrSecondaryInstance = mainOrSecondaryInstance
exports.collectTests = collectTests exports.collectJsonData = collectJsonData
/**
*
* Copyright (c) 2022 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @jest-environment node
*/
const path = require('path')
const util = require('../src-electron/util/util')
const jsonDir = path.join(__dirname, 'resource/json/')
test('Test json data', async () => {
let jsonFile = path.join(jsonDir, 'data1.json')
let data = await util.collectJsonData(jsonFile)
expect(data[0]).toEqual('3')
expect(data[6]).toEqual('9')
})
{
"a": ["1", "2", "3"],
"b": ["4", "5", "6"],
"c": ["7", "8", "9"],
"collection": ["a", "b", "c"],
"disable": ["1", "2"]
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册