Coverage for mlos_bench/mlos_bench/tests/config/__init__.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Helper functions for config example loading tests.""" 

6 

7import os 

8from collections.abc import Callable 

9from importlib.resources import files 

10 

11from mlos_bench.util import path_join 

12 

13BUILTIN_TEST_CONFIG_PATH = str(files("mlos_bench.tests.config").joinpath("")).replace("\\", "/") 

14 

15 

16def locate_config_examples( 

17 root_dir: str, 

18 config_examples_dir: str, 

19 examples_filter: Callable[[list[str]], list[str]] | None = None, 

20) -> list[str]: 

21 """ 

22 Locates all config examples in the given directory. 

23 

24 Parameters 

25 ---------- 

26 root_dir : str 

27 Root dir of the config_examples_dir. 

28 config_examples_dir: str 

29 Name to the directory containing config examples. 

30 examples_filter : callable 

31 Optional filter to provide on the returned results. 

32 

33 Returns 

34 ------- 

35 config_examples: list[str] 

36 List of paths to config examples. 

37 """ 

38 if examples_filter is None: 

39 examples_filter = list 

40 config_examples_path = path_join(root_dir, config_examples_dir) 

41 assert os.path.isdir(config_examples_path) 

42 config_examples = [] 

43 for root, _, dir_files in os.walk(config_examples_path): 

44 for file in dir_files: 

45 if file.endswith(".json") or file.endswith(".jsonc"): 

46 config_examples.append(path_join(root, file)) 

47 return examples_filter(config_examples)