Skip to content

Commit f622651

Browse files
committed
test: add JSON and JSON Lines output test cases
1 parent 5eba279 commit f622651

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \
132132
tests/case-20-offset-field.bash \
133133
tests/case-20-repeat-count.bash \
134134
tests/case-21-exit-Q-status.bash \
135-
tests/case-22-empty-process-name.bash
135+
tests/case-22-empty-process-name.bash \
136+
tests/case-30-json-output.bash \
137+
tests/case-31-jsonl-output.bash
136138
TESTS = $(DIALECT_NEUTRAL_TESTS)
137139
EXTRA_DIST += $(DIALECT_NEUTRAL_TESTS) \
138140
tests/case-13-classic.bash \

tests/case-30-json-output.bash

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
source tests/common.bash
3+
4+
# Skip if python3 not available (needed for JSON validation)
5+
if ! command -v python3 &>/dev/null; then
6+
echo "SKIP: python3 not available" >> $report
7+
exit 77
8+
fi
9+
10+
{
11+
# Test 1: -J produces valid JSON
12+
output=$($lsof -J -p $$ 2>/dev/null)
13+
if ! echo "$output" | python3 -m json.tool > /dev/null 2>&1; then
14+
echo "FAIL: -J output is not valid JSON" >> $report
15+
exit 1
16+
fi
17+
echo "PASS: -J produces valid JSON" >> $report
18+
19+
# Test 2: -J output has lsof_version and processes keys
20+
echo "$output" | python3 -c "
21+
import sys, json
22+
d = json.load(sys.stdin)
23+
assert 'lsof_version' in d, 'missing lsof_version'
24+
assert 'processes' in d, 'missing processes'
25+
assert isinstance(d['processes'], list), 'processes is not a list'
26+
assert len(d['processes']) > 0, 'no processes found'
27+
" 2>&1 || { echo "FAIL: -J output structure invalid" >> $report; exit 1; }
28+
echo "PASS: -J output structure valid" >> $report
29+
30+
# Test 3: -J -Fpcfn only includes selected fields
31+
output_sel=$($lsof -J -Fpcfn -p $$ 2>/dev/null)
32+
echo "$output_sel" | python3 -c "
33+
import sys, json
34+
d = json.load(sys.stdin)
35+
p = d['processes'][0]
36+
assert 'pid' in p, 'missing pid'
37+
assert 'command' in p, 'missing command'
38+
f = p['files'][0]
39+
allowed_file_keys = {'fd', 'name'}
40+
actual_file_keys = set(f.keys())
41+
extra_keys = actual_file_keys - allowed_file_keys
42+
assert not extra_keys, 'unexpected file keys with -Fpcfn: %s' % extra_keys
43+
assert 'uid' not in p, 'uid should not be present with -Fpcfn'
44+
" 2>&1 || { echo "FAIL: -J field selection not working" >> $report; exit 1; }
45+
echo "PASS: -J field selection works" >> $report
46+
47+
# Test 4: -J empty result produces valid JSON
48+
output_empty=$($lsof -J -p 999999 2>/dev/null)
49+
echo "$output_empty" | python3 -c "
50+
import sys, json
51+
d = json.load(sys.stdin)
52+
assert d['processes'] == [], 'empty result should have empty processes array'
53+
" 2>&1 || { echo "FAIL: -J empty result is not valid JSON" >> $report; exit 1; }
54+
echo "PASS: -J empty result is valid JSON" >> $report
55+
56+
# Test 5: -J and -j are mutually exclusive
57+
if $lsof -J -j 2>/dev/null; then
58+
echo "FAIL: -J -j should error" >> $report
59+
exit 1
60+
fi
61+
echo "PASS: -J -j mutual exclusivity" >> $report
62+
63+
# Test 6: -J and -t are mutually exclusive
64+
if $lsof -J -t 2>/dev/null; then
65+
echo "FAIL: -J -t should error" >> $report
66+
exit 1
67+
fi
68+
echo "PASS: -J -t mutual exclusivity" >> $report
69+
70+
exit 0
71+
} > $report 2>&1

tests/case-31-jsonl-output.bash

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
source tests/common.bash
3+
4+
# Skip if python3 not available
5+
if ! command -v python3 &>/dev/null; then
6+
echo "SKIP: python3 not available" >> $report
7+
exit 77
8+
fi
9+
10+
{
11+
# Test 1: -j produces valid JSON Lines (each line is valid JSON)
12+
tmpout=$(mktemp)
13+
$lsof -j -p $$ 2>/dev/null > "$tmpout"
14+
fail=0
15+
while IFS= read -r line; do
16+
if [ -z "$line" ]; then
17+
continue
18+
fi
19+
if ! echo "$line" | python3 -m json.tool > /dev/null 2>&1; then
20+
echo "FAIL: -j line is not valid JSON: $line" >> $report
21+
fail=1
22+
break
23+
fi
24+
done < "$tmpout"
25+
rm -f "$tmpout"
26+
if [ $fail -ne 0 ]; then
27+
exit 1
28+
fi
29+
echo "PASS: -j produces valid JSON Lines" >> $report
30+
31+
# Test 2: -j lines contain both process and file fields (denormalized)
32+
line=$($lsof -j -p $$ 2>/dev/null | head -1)
33+
echo "$line" | python3 -c "
34+
import sys, json
35+
d = json.load(sys.stdin)
36+
assert 'pid' in d, 'missing pid'
37+
assert 'fd' in d or 'command' in d, 'missing file or process fields'
38+
" 2>&1 || { echo "FAIL: -j line missing expected fields" >> $report; exit 1; }
39+
echo "PASS: -j lines have denormalized fields" >> $report
40+
41+
# Test 3: -j with no matching process produces empty output
42+
output=$($lsof -j -p 999999 2>/dev/null)
43+
if [ -n "$output" ]; then
44+
echo "FAIL: -j with no matches should produce empty output" >> $report
45+
exit 1
46+
fi
47+
echo "PASS: -j empty result is empty" >> $report
48+
49+
exit 0
50+
} > $report 2>&1

0 commit comments

Comments
 (0)