| #!/bin/bash |
| # Copyright 2021 Google LLC |
| # |
| # 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. |
| # |
| # Script to generate mocks for interfaces by using mockgen. |
| # Must be run from the src directory containing all the Policy Manager |
| # source code. |
| # Requires mockgen binary and gomock package to be installed and $GOPATH to be |
| # set correctly. |
| # |
| # Generated mocks are placed under policy_manager/mock/mock<package_name> |
| # and will have the package name mock<package_name>. |
| |
| # Directory to store all the mocks. |
| mock_dir="policy_manager/mock" |
| |
| # Packages with interface definitions to be mocked. |
| packages=( |
| "policyenforcer" |
| "dbus" |
| "devicepolicy" |
| "imgstatus" |
| "policymanagerutil" |
| "sysapi" |
| "systemd" |
| ) |
| |
| # Interfaces in each package to be mocked, presented as a comma-separated list |
| # for each package. |
| interfaces=( |
| "PolicyEnforcer" |
| "UpdateEngineClient" |
| "Manager" |
| "Reporter" |
| "MetadataRetriever" |
| "APIHandler" |
| "SystemdClient" |
| ) |
| |
| index=0 |
| for pkg in ${packages[@]}; do |
| echo -n "Generating mock for ${pkg}..." |
| |
| # Make sure the output directory for the mock exists. |
| output_dir=${mock_dir}/mock${pkg} |
| mkdir -p ${output_dir} |
| |
| # Use mockgen's reflect mode to generate mock interfaces. |
| # mockgen's source mode has a bug where a struct defined in the same file |
| # as the interface is not imported into the generated mock, causing the mock |
| # to fail to compile. |
| mockgen -package=mock${pkg} \ |
| -destination=${output_dir}/${pkg}.go \ |
| policy_manager/${pkg} \ |
| ${interfaces[${index}]} |
| |
| if [ $? -ne 0 ] |
| then |
| echo "failed" |
| exit 1 |
| fi |
| |
| let index+=1 |
| |
| echo "done" |
| done |