blob: 53cc1401f0e655b38213a905aa7891b9f02f8de5 [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Compiled build api proto sanity check."""
from __future__ import print_function
import os
import sys
from chromite.api import compile_build_api_proto
from chromite.lib import constants
from chromite.lib import cros_build_lib
from chromite.lib import cros_test_lib
pytestmark = cros_test_lib.pytestmark_network_test
assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
class ProtoGeneratedTest(cros_test_lib.TempDirTestCase):
"""Check if the generated proto is up to date."""
def _GetGenHashes(self, directory):
"""Get the hashes of the generated files in the directory."""
md5s = {}
pb2s = []
for dirpath, _dirnames, files in os.walk(directory):
curpb = [os.path.join(dirpath, f) for f in files if f.endswith('')]
if curpb:
cmd = ['md5sum'] + pb2s
output =, stdout=True, encoding='utf-8').stdout
for line in output.splitlines():
md5, filename = line.split()
md5s[filename.replace(directory, '')] = md5
return md5s
def testGeneratedUpToDate(self):
"""Test the generated files match the raw proto.
Fails when the generated proto files in the repo do not match a fresh
This is accomplished by creating `'file/path': 'md5sum'` mappings for each
file in each the repo and the fresh generation. This approach is somewhat
more expensive than other approaches, such as `diff -r`, but also means we
can include other changes in those folders, such as non-empty
and README files.
generated = os.path.join(constants.SOURCE_ROOT, 'chromite', 'api', 'gen')
current_hash = self._GetGenHashes(generated)
# Generate it into a tempdir so we don't change the files if they're not up
# to date.
# Get the hashes from the freshly generated directory.
new_hash = self._GetGenHashes(self.tempdir)
self.assertDictEqual(current_hash, new_hash,
'The freshly compiled proto has new changes. '
'Please look into that. '
'Run chromite/api/compile_build_api_proto to generate '
'the new proto.')