xref: /aosp_15_r20/system/core/cli-test/README.md (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1*00c7fec1SAndroid Build Coastguard Worker# cli-test
2*00c7fec1SAndroid Build Coastguard Worker
3*00c7fec1SAndroid Build Coastguard Worker## What?
4*00c7fec1SAndroid Build Coastguard Worker
5*00c7fec1SAndroid Build Coastguard Worker`cli-test` makes integration testing of command-line tools easier.
6*00c7fec1SAndroid Build Coastguard Worker
7*00c7fec1SAndroid Build Coastguard Worker## Goals
8*00c7fec1SAndroid Build Coastguard Worker
9*00c7fec1SAndroid Build Coastguard Worker* Readable syntax. Common cases should be concise, and pretty much anyone
10*00c7fec1SAndroid Build Coastguard Worker  should be able to read tests even if they've never seen this tool before.
11*00c7fec1SAndroid Build Coastguard Worker
12*00c7fec1SAndroid Build Coastguard Worker* Minimal issues with quoting. The toybox tests -- being shell scripts --
13*00c7fec1SAndroid Build Coastguard Worker  quickly become a nightmare of quoting. Using a non ad hoc format (such as
14*00c7fec1SAndroid Build Coastguard Worker  JSON) would have introduced similar but different quoting issues. A custom
15*00c7fec1SAndroid Build Coastguard Worker  format, while annoying, side-steps this.
16*00c7fec1SAndroid Build Coastguard Worker
17*00c7fec1SAndroid Build Coastguard Worker* Sensible defaults. We expect your exit status to be 0 unless you say
18*00c7fec1SAndroid Build Coastguard Worker  otherwise. We expect nothing on stderr unless you say otherwise. And so on.
19*00c7fec1SAndroid Build Coastguard Worker
20*00c7fec1SAndroid Build Coastguard Worker* Convention over configuration. Related to sensible defaults, we don't let you
21*00c7fec1SAndroid Build Coastguard Worker  configure things that aren't absolutely necessary. So you can't keep your test
22*00c7fec1SAndroid Build Coastguard Worker  data anywhere except in the `files/` subdirectory of the directory containing
23*00c7fec1SAndroid Build Coastguard Worker  your test, for example.
24*00c7fec1SAndroid Build Coastguard Worker
25*00c7fec1SAndroid Build Coastguard Worker## Non Goals
26*00c7fec1SAndroid Build Coastguard Worker
27*00c7fec1SAndroid Build Coastguard Worker* Portability. Just being able to run on Linux (host and device) is sufficient
28*00c7fec1SAndroid Build Coastguard Worker  for our needs. macOS is probably easy enough if we ever need it, but Windows
29*00c7fec1SAndroid Build Coastguard Worker  probably doesn't make sense.
30*00c7fec1SAndroid Build Coastguard Worker
31*00c7fec1SAndroid Build Coastguard Worker## Syntax
32*00c7fec1SAndroid Build Coastguard Worker
33*00c7fec1SAndroid Build Coastguard WorkerAny all-whitespace line, or line starting with `#` is ignored.
34*00c7fec1SAndroid Build Coastguard Worker
35*00c7fec1SAndroid Build Coastguard WorkerA test looks like this:
36*00c7fec1SAndroid Build Coastguard Worker```
37*00c7fec1SAndroid Build Coastguard Workername: unzip -l
38*00c7fec1SAndroid Build Coastguard Workercommand: unzip -l $FILES/example.zip d1/d2/x.txt
39*00c7fec1SAndroid Build Coastguard Workerafter: [ ! -f d1/d2/x.txt ]
40*00c7fec1SAndroid Build Coastguard Workerexpected-stdout:
41*00c7fec1SAndroid Build Coastguard Worker	Archive:  $FILES/example.zip
42*00c7fec1SAndroid Build Coastguard Worker	  Length      Date    Time    Name
43*00c7fec1SAndroid Build Coastguard Worker	---------  ---------- -----   ----
44*00c7fec1SAndroid Build Coastguard Worker	     1024  2017-06-04 08:45   d1/d2/x.txt
45*00c7fec1SAndroid Build Coastguard Worker	---------                     -------
46*00c7fec1SAndroid Build Coastguard Worker	     1024                     1 file
47*00c7fec1SAndroid Build Coastguard Worker---
48*00c7fec1SAndroid Build Coastguard Worker```
49*00c7fec1SAndroid Build Coastguard Worker
50*00c7fec1SAndroid Build Coastguard WorkerThe `name:` line names the test, and is only for human consumption.
51*00c7fec1SAndroid Build Coastguard Worker
52*00c7fec1SAndroid Build Coastguard WorkerThe `command:` line is the command to be run. Additional commands can be
53*00c7fec1SAndroid Build Coastguard Workersupplied as zero or more `before:` lines (run before `command:`) and zero or
54*00c7fec1SAndroid Build Coastguard Workermore `after:` lines (run after `command:`). These are useful for both
55*00c7fec1SAndroid Build Coastguard Workersetup/teardown but also for testing post conditions (as in the example above).
56*00c7fec1SAndroid Build Coastguard Worker
57*00c7fec1SAndroid Build Coastguard WorkerAny `command:`, `before:`, or `after:` line is expected to exit with status 0.
58*00c7fec1SAndroid Build Coastguard WorkerAnything else is considered a test failure.
59*00c7fec1SAndroid Build Coastguard Worker
60*00c7fec1SAndroid Build Coastguard WorkerThe `expected-stdout:` line is followed by zero or more tab-prefixed lines that
61*00c7fec1SAndroid Build Coastguard Workerare otherwise the exact output expected from the command. (There's magic behind
62*00c7fec1SAndroid Build Coastguard Workerthe scenes to rewrite the test files directory to `$FILES` because otherwise any
63*00c7fec1SAndroid Build Coastguard Workerpath in the output would depend on the temporary directory used to run the test.)
64*00c7fec1SAndroid Build Coastguard Worker
65*00c7fec1SAndroid Build Coastguard WorkerThere is currently no `expected-stderr:` line. Standard error is implicitly
66*00c7fec1SAndroid Build Coastguard Workerexpected to be empty, and any output will cause a test failure. (The support is
67*00c7fec1SAndroid Build Coastguard Workerthere, but not wired up because we haven't needed it yet.)
68*00c7fec1SAndroid Build Coastguard Worker
69*00c7fec1SAndroid Build Coastguard WorkerThe fields can appear in any order, but every test must contain at least a
70*00c7fec1SAndroid Build Coastguard Worker`name:` line and a `command:` line.
71*00c7fec1SAndroid Build Coastguard Worker
72*00c7fec1SAndroid Build Coastguard Worker## Output
73*00c7fec1SAndroid Build Coastguard Worker
74*00c7fec1SAndroid Build Coastguard WorkerThe output is intended to resemble gtest.
75*00c7fec1SAndroid Build Coastguard Worker
76*00c7fec1SAndroid Build Coastguard Worker## Future Directions
77*00c7fec1SAndroid Build Coastguard Worker
78*00c7fec1SAndroid Build Coastguard Worker* It's often useful to be able to *match* against stdout/stderr/a file rather
79*00c7fec1SAndroid Build Coastguard Worker  than give exact expected output. We might want to add explicit support for
80*00c7fec1SAndroid Build Coastguard Worker  this. In the meantime, it's possible to use an `after:` with `grep -q` if
81*00c7fec1SAndroid Build Coastguard Worker  you redirect in your `command:`.
82*00c7fec1SAndroid Build Coastguard Worker
83*00c7fec1SAndroid Build Coastguard Worker* In addition to using a `before:` (which will fail a test), it can be useful
84*00c7fec1SAndroid Build Coastguard Worker  to be able to specify tests that would cause us to *skip* a test. An example
85*00c7fec1SAndroid Build Coastguard Worker  would be "am I running as root?".
86*00c7fec1SAndroid Build Coastguard Worker
87*00c7fec1SAndroid Build Coastguard Worker* It might be useful to be able to make exit status assertions other than 0?
88*00c7fec1SAndroid Build Coastguard Worker
89*00c7fec1SAndroid Build Coastguard Worker* There's currently no way (other than the `files/` directory) to share repeated
90*00c7fec1SAndroid Build Coastguard Worker  setup between tests.
91