Skip to content

Commit

Permalink
feat(tools): idf.py adding arguments from file via @filename.txt
Browse files Browse the repository at this point in the history
- moved test inputs to one directory
- removed `-` from test arguments

Closes espressif#11821
Closes espressif#11783
  • Loading branch information
Marek Fiala authored and nebkat committed May 8, 2024
1 parent c78bb14 commit 45c3421
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions tools/idf.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def parse_project_dir(project_dir: str) -> Any:
return CLI(help=cli_help, verbose_output=verbose_output, all_actions=all_actions)


def main(argv=None) -> None:
def main(argv: List[Any] = None) -> None:
# Check the environment only when idf.py is invoked regularly from command line.
checks_output = None if SHELL_COMPLETE_RUN else check_environment()

Expand All @@ -717,7 +717,7 @@ def main(argv=None) -> None:
cli(argv, prog_name=PROG, complete_var=SHELL_COMPLETE_VAR)


def expand_file_arguments(argv):
def expand_file_arguments(argv: List[Any]) -> List[Any]:
"""
Any argument starting with "@" gets replaced with all values read from a text file.
Text file arguments can be split by newline or by space.
Expand All @@ -727,10 +727,10 @@ def expand_file_arguments(argv):
visited = set()
expanded = False

def expand_args(args, parent_path, file_stack):
def expand_args(args: List[Any], parent_path: str, file_stack: List[str]) -> List[str]:
expanded_args = []
for arg in args:
if not arg.startswith("@"):
if not arg.startswith('@'):
expanded_args.append(arg)
else:
nonlocal expanded, visited
Expand All @@ -745,13 +745,13 @@ def expand_args(args, parent_path, file_stack):
visited.add(rel_path)

try:
with open(rel_path, "r") as f:
with open(rel_path, 'r') as f:
for line in f:
expanded_args.extend(expand_args(shlex.split(line), os.path.dirname(rel_path), file_stack + [file_name]))
except IOError:
file_stack_str = ' -> '.join(['@' + f for f in file_stack + [file_name]])
raise FatalError(f"File '{rel_path}' (expansion of {file_stack_str}) could not be opened. "
"Please ensure the file exists and you have the necessary permissions to read it.")
'Please ensure the file exists and you have the necessary permissions to read it.')
return expanded_args

argv = expand_args(argv, os.getcwd(), [])
Expand Down
1 change: 0 additions & 1 deletion tools/test_idf_py/args_a

This file was deleted.

1 change: 0 additions & 1 deletion tools/test_idf_py/args_b

This file was deleted.

1 change: 0 additions & 1 deletion tools/test_idf_py/args_circular_a

This file was deleted.

1 change: 0 additions & 1 deletion tools/test_idf_py/args_circular_b

This file was deleted.

1 change: 0 additions & 1 deletion tools/test_idf_py/args_recursive

This file was deleted.

1 change: 1 addition & 0 deletions tools/test_idf_py/file_args_expansion_inputs/args_a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DAAA DBBB
1 change: 1 addition & 0 deletions tools/test_idf_py/file_args_expansion_inputs/args_b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DCCC DDDD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DAAA @args_circular_b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DBBB @args_circular_a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@args_a DEEE DFFF
16 changes: 8 additions & 8 deletions tools/test_idf_py/test_idf_py.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

import json
Expand Down Expand Up @@ -296,40 +296,40 @@ def test_file_expansion(self):
"""Test @filename expansion functionality"""
try:
output = subprocess.check_output(
[sys.executable, idf_py_path, '--version', '@args_a'],
[sys.executable, idf_py_path, '--version', '@file_args_expansion_inputs/args_a'],
env=os.environ,
stderr=subprocess.STDOUT).decode('utf-8', 'ignore')
self.assertIn('Running: idf.py --version -DAAA -DBBB', output)
self.assertIn('Running: idf.py --version DAAA DBBB', output)
except subprocess.CalledProcessError as e:
self.fail(f'Process should have exited normally, but it exited with a return code of {e.returncode}')

def test_multiple_file_arguments(self):
"""Test multiple @filename arguments"""
try:
output = subprocess.check_output(
[sys.executable, idf_py_path, '--version', '@args_a', '@args_b'],
[sys.executable, idf_py_path, '--version', '@file_args_expansion_inputs/args_a', '@file_args_expansion_inputs/args_b'],
env=os.environ,
stderr=subprocess.STDOUT).decode('utf-8', 'ignore')
self.assertIn('Running: idf.py --version -DAAA -DBBB -DCCC -DDDD', output)
self.assertIn('Running: idf.py --version DAAA DBBB DCCC DDDD', output)
except subprocess.CalledProcessError as e:
self.fail(f'Process should have exited normally, but it exited with a return code of {e.returncode}')

def test_recursive_expansion(self):
"""Test recursive expansion of @filename arguments"""
try:
output = subprocess.check_output(
[sys.executable, idf_py_path, '--version', '@args_recursive'],
[sys.executable, idf_py_path, '--version', '@file_args_expansion_inputs/args_recursive'],
env=os.environ,
stderr=subprocess.STDOUT).decode('utf-8', 'ignore')
self.assertIn('Running: idf.py --version -DAAA -DBBB -DEEE -DFFF', output)
self.assertIn('Running: idf.py --version DAAA DBBB DEEE DFFF', output)
except subprocess.CalledProcessError as e:
self.fail(f'Process should have exited normally, but it exited with a return code of {e.returncode}')

def test_circular_dependency(self):
"""Test circular dependency detection in file argument expansion"""
with self.assertRaises(subprocess.CalledProcessError) as cm:
subprocess.check_output(
[sys.executable, idf_py_path, '--version', '@args_circular_a'],
[sys.executable, idf_py_path, '--version', '@file_args_expansion_inputs/args_circular_a'],
env=os.environ,
stderr=subprocess.STDOUT).decode('utf-8', 'ignore')
self.assertIn('Circular dependency in file argument expansion', cm.exception.output.decode('utf-8', 'ignore'))
Expand Down

0 comments on commit 45c3421

Please sign in to comment.