Skip to content

Commit

Permalink
[YAML] Update scripts/tools/zap_regen_all.py to be able to generate o…
Browse files Browse the repository at this point in the history
…nly the test targets if needed (#15246)

* [YAML] Update chip-tool templates in such a way that tests can be generated alone

* [YAML] Update src/darwin templates in such a way that tests can be generated alone

* [YAML] Update examples/placeholder dedicated tests templates with the new paths

* Update scripts/tools/zap_regen_all.py to be able to generate only the test targets if needed
  • Loading branch information
vivien-apple authored and pull[bot] committed Mar 4, 2022
1 parent d751300 commit 2188514
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 85 deletions.
6 changes: 0 additions & 6 deletions examples/chip-tool/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ function asTypeMaxValue(type)
return templateUtil.templatePromise(this.global, promise);
}

function utf8StringLength(str)
{
return new TextEncoder().encode(str).length
}

async function structs_with_cluster_name(options)
{
const packageId = await templateUtil.ensureZclPackageId(this);
Expand Down Expand Up @@ -140,5 +135,4 @@ async function structs_with_cluster_name(options)
exports.asDelimitedCommand = asDelimitedCommand;
exports.asTypeMinValue = asTypeMinValue;
exports.asTypeMaxValue = asTypeMaxValue;
exports.utf8StringLength = utf8StringLength;
exports.structs_with_cluster_name = structs_with_cluster_name;
21 changes: 1 addition & 20 deletions examples/chip-tool/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"../../../src/app/zap-templates/common/StringHelper.js",
"../../../src/app/zap-templates/templates/app/helper.js",
"../../../src/app/zap-templates/templates/chip/helper.js",
"../../../src/app/zap-templates/common/ClusterTestGeneration.js",
"helper.js",
"tests.js"
"helper.js"
],
"override": "../../../src/app/zap-templates/common/override.js",
"partials": [
Expand All @@ -23,18 +21,6 @@
{
"name": "cluster_header",
"path": "../../../src/app/zap-templates/partials/cluster_header.zapt"
},
{
"name": "test_cluster",
"path": "partials/test_cluster.zapt"
},
{
"name": "commandValue",
"path": "partials/test_cluster_command_value.zapt"
},
{
"name": "valueEquals",
"path": "partials/test_cluster_value_equals.zapt"
}
],
"templates": [
Expand Down Expand Up @@ -62,11 +48,6 @@
"path": "ComplexArgumentParser-src.zapt",
"name": "Complex Argument Parser",
"output": "cluster/ComplexArgumentParser.cpp"
},
{
"path": "tests-commands.zapt",
"name": "Tests Commands header",
"output": "test/Commands.h"
}
]
}
26 changes: 26 additions & 0 deletions examples/chip-tool/templates/tests/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* 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.
*/

function utf8StringLength(str)
{
return new TextEncoder().encode(str).length
}

//
// Module exports
//
exports.utf8StringLength = utf8StringLength;
40 changes: 40 additions & 0 deletions examples/chip-tool/templates/tests/templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "CHIP Tool Tests templates",
"version": "chip-v1",
"helpers": [
"../../../../src/app/zap-templates/partials/helper.js",
"../../../../src/app/zap-templates/common/StringHelper.js",
"../../../../src/app/zap-templates/templates/app/helper.js",
"../../../../src/app/zap-templates/templates/chip/helper.js",
"../../../../src/app/zap-templates/common/ClusterTestGeneration.js",
"../helper.js",
"helper.js",
"tests.js"
],
"override": "../../../../src/app/zap-templates/common/override.js",
"partials": [
{
"name": "header",
"path": "../../../../src/app/zap-templates/partials/header.zapt"
},
{
"name": "test_cluster",
"path": "partials/test_cluster.zapt"
},
{
"name": "commandValue",
"path": "partials/command_value.zapt"
},
{
"name": "valueEquals",
"path": "partials/value_equals.zapt"
}
],
"templates": [
{
"path": "commands.zapt",
"name": "Tests Commands header",
"output": "test/Commands.h"
}
]
}
File renamed without changes.
14 changes: 3 additions & 11 deletions examples/placeholder/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@
"name": "header",
"path": "../../../src/app/zap-templates/partials/header.zapt"
},
{
"name": "clusters_header",
"path": "../../../src/app/zap-templates/partials/clusters_header.zapt"
},
{
"name": "cluster_header",
"path": "../../../src/app/zap-templates/partials/cluster_header.zapt"
},
{
"name": "test_cluster",
"path": "../../../examples/chip-tool/templates/partials/test_cluster.zapt"
"path": "../../../examples/chip-tool/templates/tests/partials/test_cluster.zapt"
},
{
"name": "commandValue",
"path": "../../../examples/chip-tool/templates/partials/test_cluster_command_value.zapt"
"path": "../../../examples/chip-tool/templates/tests/partials/command_value.zapt"
},
{
"name": "valueEquals",
"path": "../../../examples/chip-tool/templates/partials/test_cluster_value_equals.zapt"
"path": "../../../examples/chip-tool/templates/tests/partials/value_equals.zapt"
}
],
"templates": [
Expand Down
87 changes: 68 additions & 19 deletions scripts/tools/zap_regen_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.
#

import argparse
import os
from pathlib import Path
import sys
Expand Down Expand Up @@ -47,6 +48,8 @@ def generate(self):
cmd.append(self.template)

if self.output_dir:
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
cmd.append('-o')
cmd.append(self.output_dir)

Expand All @@ -61,6 +64,16 @@ def checkPythonVersion():
exit(1)


def setupArgumentsParser():
parser = argparse.ArgumentParser(
description='Generate content from ZAP files')
parser.add_argument('--type', default='all', choices=['all', 'tests'],
help='Choose which content type to generate (default: all)')
parser.add_argument('--tests', default='all', choices=['all', 'chip-tool', 'darwin', 'app1', 'app2'],
help='When generating tests only target, Choose which tests to generate (default: all)')
return parser.parse_args()


def getGlobalTemplatesTargets():
targets = []

Expand All @@ -72,7 +85,8 @@ def getGlobalTemplatesTargets():
# Place holder has apps within each build
if example_name == "placeholder":
example_name = filepath.as_posix()
example_name = example_name[example_name.index('apps/') + 5:]
example_name = example_name[example_name.index(
'apps/') + len('apps/'):]
example_name = example_name[:example_name.index('/')]
logging.info("Found example %s (via %s)" %
(example_name, str(filepath)))
Expand All @@ -81,8 +95,6 @@ def getGlobalTemplatesTargets():
# a name like <zap-generated/foo.h>
output_dir = os.path.join(
'zzz_generated', 'placeholder', example_name, 'zap-generated')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
template = 'examples/placeholder/templates/templates.json'

targets.append(ZAPGenerateTarget(filepath, output_dir=output_dir))
Expand All @@ -97,9 +109,6 @@ def getGlobalTemplatesTargets():
# a name like <zap-generated/foo.h>
output_dir = os.path.join(
'zzz_generated', example_name, 'zap-generated')
if not os.path.exists(output_dir):
os.makedirs(output_dir)

targets.append(ZAPGenerateTarget(filepath, output_dir=output_dir))

targets.append(ZAPGenerateTarget(
Expand All @@ -109,8 +118,46 @@ def getGlobalTemplatesTargets():
return targets


def getSpecificTemplatesTargets():
def getTestsTemplatesTargets(test_target):
templates = {
'chip-tool': {
'zap': 'src/controller/data_model/controller-clusters.zap',
'template': 'examples/chip-tool/templates/tests/templates.json',
'output_dir': 'zzz_generated/chip-tool/zap-generated'
},
'darwin': {
'zap': 'src/controller/data_model/controller-clusters.zap',
'template': 'src/darwin/Framework/CHIP/templates/tests/templates.json',
'output_dir': None
}
}

# Place holder has apps within each build
for filepath in Path('./examples/placeholder').rglob('*.zap'):
example_name = filepath.as_posix()
example_name = example_name[example_name.index(
'apps/') + len('apps/'):]
example_name = example_name[:example_name.index('/')]

templates[example_name] = {
'zap': filepath,
'template': 'examples/placeholder/templates/templates.json',
'output_dir': os.path.join('zzz_generated', 'placeholder', example_name, 'zap-generated')
}

targets = []
for key, target in templates.items():
if test_target == 'all' or test_target == key:
logging.info("Found test target %s (via %s)" %
(key, target['template']))
targets.append(ZAPGenerateTarget(
target['zap'], template=target['template'], output_dir=target['output_dir']))

return targets


def getSpecificTemplatesTargets():
zap_filepath = 'src/controller/data_model/controller-clusters.zap'

# Mapping of required template and output directory
templates = {
Expand All @@ -122,23 +169,24 @@ def getSpecificTemplatesTargets():
'src/app/tests/suites/templates/templates.json': 'zzz_generated/controller-clusters/zap-generated',
}

targets = []
for template, output_dir in templates.items():
target = ZAPGenerateTarget(
'src/controller/data_model/controller-clusters.zap', template=template)
if output_dir is not None:
if not os.path.exists(output_dir):
os.makedirs(output_dir)
target.output_dir = output_dir

targets.append(target)
targets.append(ZAPGenerateTarget(
zap_filepath, template=template, output_dir=output_dir))

return targets


def getTargets():
def getTargets(type, test_target):
targets = []
targets.extend(getGlobalTemplatesTargets())
targets.extend(getSpecificTemplatesTargets())

if type == 'all':
targets.extend(getGlobalTemplatesTargets())
targets.extend(getTestsTemplatesTargets('all'))
targets.extend(getSpecificTemplatesTargets())
elif type == 'tests':
targets.extend(getTestsTemplatesTargets(test_target))

return targets


Expand All @@ -149,8 +197,9 @@ def main():
)
checkPythonVersion()
os.chdir(CHIP_ROOT_DIR)
args = setupArgumentsParser()

targets = getTargets()
targets = getTargets(args.type, args.tests)
for target in targets:
target.generate()

Expand Down
6 changes: 0 additions & 6 deletions src/darwin/Framework/CHIP/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ function asObjectiveCNumberType(label, type, asLowerCased)
return templateUtil.templatePromise(this.global, promise)
}

function asTestIndex(index)
{
return index.toString().padStart(6, 0);
}

async function asObjectiveCClass(type, cluster, options)
{
let pkgId = await templateUtil.ensureZclPackageId(this);
Expand Down Expand Up @@ -155,7 +150,6 @@ function commandHasRequiredField(command)
//
exports.asObjectiveCBasicType = asObjectiveCBasicType;
exports.asObjectiveCNumberType = asObjectiveCNumberType;
exports.asTestIndex = asTestIndex;
exports.asObjectiveCClass = asObjectiveCClass;
exports.asObjectiveCType = asObjectiveCType;
exports.asStructPropertyName = asStructPropertyName;
Expand Down
24 changes: 1 addition & 23 deletions src/darwin/Framework/CHIP/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@
"../../../../../src/app/zap-templates/templates/app/helper.js",
"../../../../../src/app/zap-templates/templates/chip/helper.js",
"../../../../../src/app/zap-templates/common/ClusterTestGeneration.js",
"helper.js",
"tests.js"
"helper.js"
],
"override": "../../../../../src/app/zap-templates/common/override.js",
"partials": [
{
"name": "header",
"path": "../../../../../src/app/zap-templates/partials/header.zapt"
},
{
"name": "test_cluster",
"path": "partials/test_cluster.zapt"
},
{
"name": "CHIPCallbackBridge",
"path": "partials/CHIPCallbackBridge.zapt"
Expand All @@ -33,14 +28,6 @@
"name": "decode_value",
"path": "partials/decode_value.zapt"
},
{
"name": "test_value",
"path": "partials/test_value.zapt"
},
{
"name": "check_test_value",
"path": "partials/check_test_value.zapt"
},
{
"name": "init_struct_member",
"path": "partials/init_struct_member.zapt"
Expand All @@ -52,10 +39,6 @@
{
"name": "attribute_data_callback_name",
"path": "partials/attribute_data_callback_name.zapt"
},
{
"name": "defined_value",
"path": "partials/defined_value.zapt"
}
],
"templates": [
Expand Down Expand Up @@ -118,11 +101,6 @@
"path": "CHIPAttributeTLVValueDecoder-src.zapt",
"name": "Decode TLV attribute values into Objc objects",
"output": "src/darwin/Framework/CHIP/zap-generated/CHIPAttributeTLVValueDecoder.mm"
},
{
"path": "clusters-tests.zapt",
"name": "Cluster Tests",
"output": "src/darwin/Framework/CHIPTests/CHIPClustersTests.m"
}
]
}
Loading

0 comments on commit 2188514

Please sign in to comment.