Skip to content

Commit ac64af7

Browse files
committed
fix(lftest): accept --test args with fewer than three elements
The `test()` helper previously required callers to pass a three-element CSV (`stdout,stderr,retc`). Plugins that only need to inject STDOUT had to trail `,,0` on every invocation, and typing `--test=path/to/fixture` from the command line tripped an `IndexError` on `args[1]`. Fall back to `''` for stderr and `0` for retc when those positions are absent, so both `--test=stdout/foo` and `--test=stdout/foo,,0` work interchangeably.
1 parent 930351a commit ac64af7

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Changed
1212

1313
* base.py: `get_worst()` now accepts any number of state arguments (`*states`). Existing two-argument callers keep working unchanged, but plugins that need to combine three or more states in one call no longer have to nest the call - e.g. `get_worst(state, used_state, committed_state)` instead of `get_worst(state, get_worst(used_state, committed_state))`
14+
* lftest.py: `test()` now accepts `args` with fewer than three elements. Plugins can be invoked as `--test=path/to/fixture` without the trailing `,,0`; stderr defaults to the empty string and the return code to `0`
1415

1516
### Fixed
1617

lftest.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from . import base, disk, shell
1717

1818
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
19-
__version__ = '2026041201'
19+
__version__ = '2026041301'
2020

2121

2222
def run(test_instance, plugin, testcase):
@@ -192,11 +192,16 @@ def test(args):
192192
Returns the content of two files and the provided return code. The first file represents STDOUT,
193193
and the second represents STDERR. This function is useful for enabling unit tests.
194194
195+
Only the STDOUT entry is required. STDERR and the return code default to
196+
the empty string and 0, so callers can pass `--test=path/to/stdout`
197+
without trailing commas.
198+
195199
### Parameters
196200
- **args** (`list`): A list containing:
197201
- The path to the file representing STDOUT or the string to be used as STDOUT.
198-
- The path to the file representing STDERR or the string to be used as STDERR.
199-
- The return code (integer or string). Defaults to 0 if not provided.
202+
- Optional: the path to the file representing STDERR or the string to be used
203+
as STDERR. Defaults to the empty string if not provided.
204+
- Optional: the return code (integer or string). Defaults to 0 if not provided.
200205
201206
### Returns
202207
- **tuple**:
@@ -205,11 +210,13 @@ def test(args):
205210
- **retc** (`int`): The return code, either from the provided value or defaulted to 0.
206211
207212
### Example
208-
>>> test('path/to/stdout.txt', 'path/to/stderr.txt', 128)
213+
>>> test(['path/to/stdout.txt', 'path/to/stderr.txt', 128])
209214
('This is stdout content', 'This is stderr content', 128)
215+
>>> test(['path/to/stdout.txt'])
216+
('This is stdout content', '', 0)
210217
"""
211-
stdout = args[0]
212-
stderr = args[1]
218+
stdout = args[0] if len(args) > 0 else ''
219+
stderr = args[1] if len(args) > 1 else ''
213220
retc = int(args[2]) if len(args) > 2 and args[2] != '' else 0
214221

215222
if stdout and os.path.isfile(stdout):

0 commit comments

Comments
 (0)