blob: 48b104f135fcbc32c2bedc07a440a51c84f4f766 [file] [log] [blame]
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07001# Copyright 2021 The IREE Authors
Geoffrey Martin-Noble93a1ff12021-03-11 10:39:25 -08002#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# Licensed under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Geoffrey Martin-Noble93a1ff12021-03-11 10:39:25 -08006
7"""A utility to enforce that a list matches a glob expression.
8
9We use this primarily to enable the error-checking capabilities of globs of test
10files in IREE while still allowing our Bazel to CMake conversion to not create
11CMake globs (which are discouraged for collecting source files, see
12https://cmake.org/cmake/help/latest/command/file.html#glob) and not be dependent
13on any information outside of the BUILD file.
14"""
15
16def enforce_glob(files, **kwargs):
17 """A utility to enforce that a list matches a glob expression.
18
19 Note that the comparison is done in an order-independent fashion.
20
21 Args:
22 files: a list that is expected to contain the same files as the
23 specified glob expression.
24 **kwargs: keyword arguments forwarded to the glob.
25
26 Returns:
27 files. The input argument unchanged
28 """
29 glob_result = native.glob(**kwargs)
Stella Laurenzo8ef62de2023-11-03 16:04:57 -070030 for skip_file in ["CMakeLists.txt"]:
31 if skip_file in glob_result:
32 glob_result.remove(skip_file)
Geoffrey Martin-Noble93a1ff12021-03-11 10:39:25 -080033
34 # glob returns a sorted list.
35 if sorted(files) != glob_result:
36 glob_result_dict = {k: None for k in glob_result}
37 result_dict = {k: None for k in files}
38 missing = [k for k in glob_result if k not in files]
39 extra = [k for k in files if k not in glob_result]
Geoffrey Martin-Noble9467a902021-03-11 11:57:11 -080040 expected_formatted = "\n".join(['"{}",'.format(file) for file in glob_result])
Geoffrey Martin-Noble93a1ff12021-03-11 10:39:25 -080041 fail(("Error in enforce_glob." +
42 "\nExpected {}." +
43 "\nGot {}." +
44 "\nMissing {}." +
Geoffrey Martin-Noble9467a902021-03-11 11:57:11 -080045 "\nExtra {}" +
46 "\nPaste this into the first enforce_glob argument:" +
47 "\n{}").format(
48 glob_result,
49 files,
50 missing,
51 extra,
52 expected_formatted,
53 ))
Geoffrey Martin-Noble93a1ff12021-03-11 10:39:25 -080054 return files