Skip to content

Commit 0804a13

Browse files
Merge pull request #1808 from buildtesters/specify_file_to_add_to_buildspec_cache
Add support for specifying a file path via -f/--file when loading buildspec into cache
2 parents 26ed347 + 1db80fa commit 0804a13

File tree

7 files changed

+95
-20
lines changed

7 files changed

+95
-20
lines changed

bash_completion.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ _buildtest ()
413413
COMPREPLY=( $( compgen -W "${opts}" -- "${cur}" ) );;
414414
# completion for rest of arguments
415415
*)
416-
local longopts="--buildspec --count --directory --executors --filter --filterfields --format --formatfields --group-by-executor --group-by-tags --help --helpfilter --helpformat --no-header --pager --paths --quiet --rebuild --row-count --tags --terse"
417-
local shortopts="-b -d -e -h -n -p -q -r -t"
416+
local longopts="--buildspec --count --directory --executors --file --filter --filterfields --format --formatfields --group-by-executor --group-by-tags --help --helpfilter --helpformat --no-header --pager --paths --quiet --rebuild --row-count --tags --terse"
417+
local shortopts="-b -d -e -f -h -n -p -q -r -t"
418418
local cmds="invalid"
419419

420420
COMPREPLY=( $( compgen -W "${cmds} ${longopts} ${shortopts}" -- "${cur}" ) )

buildtest/cli/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,14 @@ def buildspec_find_menu(self, buildspec_find_parser):
12941294
"help": "Specify root buildspecs (directory) path to load buildspecs into buildspec cache.",
12951295
},
12961296
),
1297+
(
1298+
["-f", "--file"],
1299+
{
1300+
"type": str,
1301+
"action": "append",
1302+
"help": "Specify buildspec file to load into buildspec cache.",
1303+
},
1304+
),
12971305
],
12981306
}
12991307

buildtest/cli/buildspec.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __init__(
5050
filterfields=None,
5151
formatfields=None,
5252
directory=None,
53+
buildspec_files=None,
5354
header=None,
5455
terse=None,
5556
pager=None,
@@ -71,6 +72,7 @@ def __init__(
7172
filterfields (str, optional): The filter options specified via ``buildtest buildspec find --filter`` that contains list of key value pairs for filtering buildspecs
7273
formatfields (str, optional): The format options used for formating table. The format option is a comma separated list of format fields specified via ``buildtest buildspec find --format``
7374
directory (list, optional): List of directories to search for buildspecs. This argument contains value of ``buildtest buildspec find --directory``
75+
buildspec_files (list, optional): List of buildspec files to add to cache. This argument contains value of ``buildtest buildspec find --file``
7476
headers (bool, optional): Option to control whether header are printed in terse output. This argument contains value of ``buildtest buildspec find --no-header``
7577
terse (bool, optional): Enable terse mode when printing output. In this mode we don't print output in table format instead output is printed in parseable format. This option can be specified via ``buildtest buildspec find --terse``
7678
color (str, optional): An instance of a string class that selects the color to use when printing table output
@@ -107,6 +109,8 @@ def __init__(
107109
# list of buildspec directories to search for .yml files
108110
self.paths = []
109111

112+
self.buildspec_files = buildspec_files
113+
110114
# stores invalid buildspecs and the error messages
111115
self.invalid_buildspecs = {}
112116

@@ -149,12 +153,12 @@ def load_paths(self):
149153
if not self.directory:
150154
self.paths += BUILDSPEC_DEFAULT_PATH
151155

152-
# for every root buildspec defined in configuration or via --root option,
156+
# for every root buildspec defined in configuration or via --directory option,
153157
# we resolve path and if path exist add to self.paths. The path must be a
154158
# directory. If its file, we ignore it
155159
if self.directory:
156-
for root in self.directory:
157-
path = resolve_path(root, exist=False)
160+
for dirname in self.directory:
161+
path = resolve_path(dirname, exist=False)
158162
if not os.path.exists(path):
159163
console.print(f"[red]Path: {path} does not exist!")
160164

@@ -169,6 +173,31 @@ def build(self):
169173
rebuild cache we remove the file and recreate cache. If cache file
170174
exists, we simply load from cache
171175
"""
176+
buildspecs = []
177+
# this method will check if buildspec_files are valid files and end with .yml.
178+
# If it's not a file or does not end with .yml we skip the file and report a message
179+
180+
if self.buildspec_files:
181+
for buildspec in self.buildspec_files:
182+
path = resolve_path(buildspec, exist=False)
183+
if not os.path.exists(path):
184+
console.print(f"[red]Path: {path} does not exist!")
185+
continue
186+
if not is_file(path):
187+
console.print(
188+
f"[red]{path} is not a file, please specify a file when adding buildspec to cache"
189+
)
190+
continue
191+
if not path.endswith(".yml"):
192+
console.print(
193+
f"[red]{path} does not end in .yml extension, please specify a valid buildspec file"
194+
)
195+
continue
196+
197+
buildspecs.append(path)
198+
199+
# set self.buildspec_files to list of valid buildspec files which will be used to build cache
200+
self.buildspec_files = buildspecs
172201

173202
# implements buildtest buildspec find --rebuild which removes cache file
174203
# before finding all buildspecs. We only remove file if file exists
@@ -192,7 +221,7 @@ def build(self):
192221

193222
def _discover_buildspecs(self):
194223
"""This method retrieves buildspecs based on ``self.paths`` which is a
195-
list of directory paths to search. If ``--root`` is specified
224+
list of directory paths to search. If ``--directory`` is specified
196225
we process each argument and recursively find all .yml files
197226
"""
198227

@@ -210,6 +239,9 @@ def _discover_buildspecs(self):
210239
buildspec = walk_tree(path, ".yml")
211240
buildspecs += buildspec
212241

242+
if self.buildspec_files:
243+
buildspecs += self.buildspec_files
244+
213245
if not self.terse:
214246
print(f"Buildspec Paths: {self.paths}")
215247

@@ -1405,6 +1437,7 @@ def buildspec_find(args, configuration):
14051437
filterfields=args.filter,
14061438
formatfields=args.format,
14071439
directory=args.directory,
1440+
buildspec_files=args.file,
14081441
configuration=configuration,
14091442
header=args.no_header,
14101443
terse=args.terse,

buildtest/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,17 @@ def setup(args):
252252
# build buildspec cache file automatically if it doesn't exist
253253
if not is_file(BUILDSPEC_CACHE_FILE):
254254
root_buildspecs = []
255+
buildspec_files = []
255256
if hasattr(args, "directory"):
256257
root_buildspecs = args.directory
258+
if hasattr(args, "file"):
259+
buildspec_files = args.file
257260

258-
BuildspecCache(directory=root_buildspecs, configuration=configuration)
261+
BuildspecCache(
262+
directory=root_buildspecs,
263+
buildspec_files=buildspec_files,
264+
configuration=configuration,
265+
)
259266

260267
return system, configuration, buildtest_editor, report_file
261268

docs/configuring_buildtest/overview.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ If you want buildtest to always rebuild cache you can set the following in your
220220
The configuration options such as ``count``, ``format``, ``terse`` can be tweaked to your preference. These configuration values
221221
can be overridden by command line option.
222222

223-
.. _buildspec_roots:
223+
.. _search_buildspecs_when_building_cache:
224224

225-
Specify Root Directories for searching buildspecs
226-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225+
Searching for buildspecs when building Buildspec Cache
226+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227+
228+
When building the buildspec cache, buildtest will search for buildspecs in a list of directories specified in the configuration file.
227229

228230
Buildtest will search for buildspecs by recursively searching for files with **.yml** extension. The ``directory`` property in configuration file
229231
is a list of directories to search for buildspecs. The ``directory`` property is not **required** in configuration file, but it can be a good

docs/gettingstarted/buildspecs_interface.rst

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Shown below is an example output.
7878
.. command-output:: buildtest buildspec find --buildspec
7979
:ellipsis: 11
8080

81-
Find root paths where buildspecs are searched
82-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81+
Adding buildspecs to cache
82+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
8383

8484
The ``buildtest buildspec find --paths`` will display a list of root directories buildtest will search for
8585
buildspecs when running ``buildtest buildspec find``. One can define these directories in the configuration file
@@ -89,10 +89,8 @@ or pass them via command line.
8989

9090
.. command-output:: buildtest buildspec find --paths
9191

92-
buildtest will search buildspecs in :ref:`buildspecs root <buildspec_roots>` defined in your configuration,
93-
which is a list of directory paths to search for buildspecs.
94-
If you want to load buildspecs from a directory path, you can specify a directory
95-
via ``--directory`` option in the format: ``buildtest buildspec find --directory <path>``.
92+
buildtest will :ref:`search buildspecs when building cache <search_buildspecs_when_building_cache>` that can be configured via
93+
configuration or command line. If you want to load buildspecs from a directory, you can use the ``--directory`` option.
9694
buildtest will rebuild cache when `--directory` option is specified. Note that to rebuild cache you typically
9795
need to pass **--rebuild** option but that is not required when using **--directory** option because we want
9896
buildtest to load buildspecs into cache.
@@ -112,6 +110,23 @@ Let's rebuild the cache again by running ``buildtest buildspec find`` which will
112110

113111
.. command-output:: buildtest buildspec find --rebuild --quiet
114112

113+
In addition to ``--directory`` option, one can specify a list of files to load into cache using the ``--file`` option. This can be useful
114+
if you want to load specific buildspecs into cache without having to specify ``--directory``. You can use ``--file`` option with ``--directory``
115+
and buildtest will recursively search directories and load files specified in ``--file`` option.
116+
117+
If you specify an invalid file path, a directory or file without ``.yml`` extension, buildtest will report a message and skip to next file.
118+
Shown below, we specify a list of files to load into cache using ``--file`` option.
119+
120+
.. dropdown:: ``buildtest buildspec find --file $BUILDTEST_ROOT/tutorials/vars.yml``
121+
122+
.. command-output:: buildtest buildspec find --file $BUILDTEST_ROOT/tutorials/vars.yml
123+
124+
We can confirm the file is loaded into cache using the `-b` option which list all buildspecs in cache and pipe via `grep` to search for `vars.yml`. Note that
125+
we specify ``--count=-1`` to show all buildspecs in cache.
126+
127+
.. command-output:: buildtest buildspec find -b --terse --count=-1 | grep vars.yml
128+
:shell:
129+
115130
Filtering buildspec
116131
~~~~~~~~~~~~~~~~~~~~
117132

tests/cli/test_buildspec.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,28 @@ def test_edit_file():
348348

349349

350350
@pytest.mark.cli
351-
def test_buildspec_find_roots():
351+
def test_buildspec_find_by_directory_and_files():
352352
root_buildspecs = [
353353
os.path.join(BUILDTEST_ROOT, "tests", "buildsystem"),
354354
os.path.join(BUILDTEST_ROOT, "tutorials"),
355355
]
356-
# buildtest buildspec find --root $BUILDTEST_ROOT/tests/buildsystem --root $BUILDTEST_ROOT/tutorials
356+
# list of buildspec files to add to cache, we have one valid file that exists, one with invalid extension and one file that doesn't exist
357+
bp_files = [
358+
os.path.join(BUILDTEST_ROOT, "tutorials", "vars.yml"),
359+
os.path.join(BUILDTEST_ROOT, "README.rst"), # invalid extension
360+
os.path.join(BUILDTEST_ROOT, "badfile.yml"), # file doesn't exist
361+
os.path.join(BUILDTEST_ROOT), # directory path
362+
]
363+
# buildtest buildspec find --directory $BUILDTEST_ROOT/tests/buildsystem --directory $BUILDTEST_ROOT/tutorials
357364
BuildspecCache(
358-
directory=root_buildspecs, configuration=configuration, rebuild=False
365+
directory=root_buildspecs,
366+
buildspec_files=bp_files,
367+
configuration=configuration,
368+
rebuild=False,
359369
)
360370

361371
with pytest.raises(BuildTestError):
362-
# buildtest buildspec find --root $BUILDTEST_ROOT/README.rst --root $BUILDTEST_ROOT/environment.yml
372+
# buildtest buildspec find --directory $BUILDTEST_ROOT/README.rst --directory $BUILDTEST_ROOT/environment.yml
363373
BuildspecCache(
364374
directory=[
365375
os.path.join(BUILDTEST_ROOT, "README.rst"),

0 commit comments

Comments
 (0)