Skip to content

Commit edf73db

Browse files
committed
Add file tester
Should be useful for onboarding to package managers. Does non-trivial testing on the files, but doesn't actually launch fzf.
1 parent 1b8f9df commit edf73db

File tree

10 files changed

+257
-59
lines changed

10 files changed

+257
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
g3upt/
22
g3doc/
33
out/
4+
obj/

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Support for Windows may be provided in future, but is not a current priority.
4343
* [perl](https://www.perl.org/)
4444

4545
### Guide
46-
`purr` includes a simple tool to help select the device serial from `adb devices`, or can read from the `$ANDROID_SERIAL` environment variable if set. Otherwise, `purr` has six command-line parameters:
46+
`purr` includes a simple tool to help select the device serial from `adb devices`, or can read from the `$ANDROID_SERIAL` environment variable if set. Otherwise, `purr` has the following command-line parameters:
4747

4848
* -a: Sets custom parameters for `adb` that will be used as well as the defaults whenever an input stream is selected.
4949
* -f: Sets custom parameters for `fzf`. Used on top of default parameters.
@@ -52,6 +52,12 @@ Support for Windows may be provided in future, but is not a current priority.
5252
* -v: Shows the `purr` version.
5353
* -V: Shows a composite version of `purr` and dependencies.
5454

55+
There are also the following special command line parameters that should only be used for testing and debugging:
56+
57+
* -A: Replace all calls to `adb` with the given binary. This can be used in conjunction with the bundled `adb_mock` binary to perform basic testing on purr.
58+
* -D: Use the given directory to store all generated files instead of the standard /tmp dir.
59+
* -X: Do not execute `fzf` when reached in the execution loop; return as if `fzf` executed correctly.
60+
5561
Any other command-line parameters will print the help dialog.
5662

5763
Note that both `-a` and `-f` are read without validation; there is no guarantee that setting either parameter will not break `purr`.

makefile

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,80 @@
11
OUTDIR?=$(CURDIR)/out
2+
OBJDIR?=$(CURDIR)/obj
23
SRCDIR?=$(CURDIR)/src
34
RESDIR?=$(CURDIR)/res
45
TESTDIR?=$(CURDIR)/tests
56

67
PURRFILE ?=$(OUTDIR)/purr
7-
PURRFILE_TEMP ?=$(OUTDIR)/purr_temp
8+
PURRFILE_TEMP ?=$(OBJDIR)/purr
89

910
ADBMOCKFILE ?=$(OUTDIR)/adb_mock
11+
ADBMOCKFILE_TEMP ?=$(OBJDIR)/adb_mock
1012

11-
all: purr adb_mock
13+
FILETESTERFILE ?=$(OUTDIR)/file_tester
14+
FILETESTERFILE_TEMP ?=$(OBJDIR)/file_tester
15+
16+
all: purr adb_mock file_tester
1217

1318
.PHONY: purr
1419

1520
purr:
1621
mkdir -p $(OUTDIR)
22+
mkdir -p $(OBJDIR)
23+
1724
echo "" > $(PURRFILE)
25+
echo "" > $(PURRFILE_TEMP)
1826

1927
# We first need the threading functions, since we use them as part of shell
2028
# env setup, specifically to create the cache files.
21-
cat $(SRCDIR)/threads/thread_background.sh >> "$(PURRFILE)"
22-
cat $(SRCDIR)/threads/thread_controller.sh >> "$(PURRFILE)"
23-
cat $(SRCDIR)/threads/thread_interface.sh >> "$(PURRFILE)"
24-
cat $(SRCDIR)/threads/thread_setup.sh >> "$(PURRFILE)"
29+
cat $(SRCDIR)/threads/thread_background.sh >> "$(PURRFILE_TEMP)"
30+
cat $(SRCDIR)/threads/thread_controller.sh >> "$(PURRFILE_TEMP)"
31+
cat $(SRCDIR)/threads/thread_interface.sh >> "$(PURRFILE_TEMP)"
32+
cat $(SRCDIR)/threads/thread_setup.sh >> "$(PURRFILE_TEMP)"
2533

2634
# We can then cleanly do shell setup.
27-
cat $(SRCDIR)/shell/shell_utils.sh >> "$(PURRFILE)"
28-
cat $(SRCDIR)/shell/argument_parsing.sh >> "$(PURRFILE)"
29-
cat $(SRCDIR)/shell/shell_env_check.sh >> "$(PURRFILE)"
30-
cat $(SRCDIR)/shell/shell_env_init.sh >> "$(PURRFILE)"
35+
cat $(SRCDIR)/shell/shell_utils.sh >> "$(PURRFILE_TEMP)"
36+
cat $(SRCDIR)/shell/argument_parsing.sh >> "$(PURRFILE_TEMP)"
37+
cat $(SRCDIR)/shell/shell_env_check.sh >> "$(PURRFILE_TEMP)"
38+
cat $(SRCDIR)/shell/shell_env_init.sh >> "$(PURRFILE_TEMP)"
3139

3240
# After we've validated the shell, we want to query for a serial number.
33-
cat $(SRCDIR)/serial/serial_picker.sh >> "$(PURRFILE)"
34-
cat $(SRCDIR)/serial/serial_interface.sh >> "$(PURRFILE)"
35-
cat $(SRCDIR)/serial/serial_init.sh >> "$(PURRFILE)"
41+
cat $(SRCDIR)/serial/serial_picker.sh >> "$(PURRFILE_TEMP)"
42+
cat $(SRCDIR)/serial/serial_interface.sh >> "$(PURRFILE_TEMP)"
43+
cat $(SRCDIR)/serial/serial_init.sh >> "$(PURRFILE_TEMP)"
3644

3745
# Load in all the UI utilities. We'll need these for setting up the fzf
3846
# environment in the next step.
39-
cat $(SRCDIR)/ui/ui_strings.sh >> "$(PURRFILE)"
40-
cat $(SRCDIR)/ui/ui_utils.sh >> "$(PURRFILE)"
47+
cat $(SRCDIR)/ui/ui_strings.sh >> "$(PURRFILE_TEMP)"
48+
cat $(SRCDIR)/ui/ui_utils.sh >> "$(PURRFILE_TEMP)"
4149

4250
# Finalizes the fzf environment; program is in a ready state at this point.
4351
# We just need to load in all the UI/UX elements from bindings.
44-
cat $(SRCDIR)/fzf_env/load_copy_program.sh >> "$(PURRFILE)"
45-
cat $(SRCDIR)/fzf_env/fzf_default_state.sh >> "$(PURRFILE)"
52+
cat $(SRCDIR)/fzf_env/load_copy_program.sh >> "$(PURRFILE_TEMP)"
53+
cat $(SRCDIR)/fzf_env/fzf_default_state.sh >> "$(PURRFILE_TEMP)"
4654

4755
# Load the binding libraries; this is the main block of code for purr.
4856
# Order here isn't super important, but the stream bindings need to be
4957
# at the very bottom after the command suites are fully initialized.
50-
cat $(SRCDIR)/bindings/command_suites.sh >> "$(PURRFILE)"
51-
cat $(SRCDIR)/bindings/copy_bindings.sh >> "$(PURRFILE)"
52-
cat $(SRCDIR)/bindings/history_bindings.sh >> "$(PURRFILE)"
53-
cat $(SRCDIR)/bindings/input_trim_bindings.sh >> "$(PURRFILE)"
54-
cat $(SRCDIR)/bindings/misc_bindings.sh >> "$(PURRFILE)"
55-
cat $(SRCDIR)/bindings/mode_bindings.sh >> "$(PURRFILE)"
56-
cat $(SRCDIR)/bindings/navigation_bindings.sh >> "$(PURRFILE)"
57-
cat $(SRCDIR)/bindings/preview_bindings.sh >> "$(PURRFILE)"
58-
cat $(SRCDIR)/bindings/query_bindings.sh >> "$(PURRFILE)"
59-
cat $(SRCDIR)/bindings/stream_bindings.sh >> "$(PURRFILE)"
58+
cat $(SRCDIR)/bindings/command_suites.sh >> "$(PURRFILE_TEMP)"
59+
cat $(SRCDIR)/bindings/copy_bindings.sh >> "$(PURRFILE_TEMP)"
60+
cat $(SRCDIR)/bindings/history_bindings.sh >> "$(PURRFILE_TEMP)"
61+
cat $(SRCDIR)/bindings/input_trim_bindings.sh >> "$(PURRFILE_TEMP)"
62+
cat $(SRCDIR)/bindings/misc_bindings.sh >> "$(PURRFILE_TEMP)"
63+
cat $(SRCDIR)/bindings/mode_bindings.sh >> "$(PURRFILE_TEMP)"
64+
cat $(SRCDIR)/bindings/navigation_bindings.sh >> "$(PURRFILE_TEMP)"
65+
cat $(SRCDIR)/bindings/preview_bindings.sh >> "$(PURRFILE_TEMP)"
66+
cat $(SRCDIR)/bindings/query_bindings.sh >> "$(PURRFILE_TEMP)"
67+
cat $(SRCDIR)/bindings/stream_bindings.sh >> "$(PURRFILE_TEMP)"
6068

6169
# Load the execution loop that actually makes things happen.
62-
cat $(SRCDIR)/execution_loop.sh >> "$(PURRFILE)"
70+
cat $(SRCDIR)/execution_loop.sh >> "$(PURRFILE_TEMP)"
6371

6472
# Remove comments/shebangs.
65-
sed -i -e '/^[ \t]*#/d' "$(PURRFILE)"
73+
sed -i -e '/^[ \t]*#/d' "$(PURRFILE_TEMP)"
6674

6775
# Add one shebang and the copyright notice.
6876
# We need a temp file since cat can't do it in-place.
69-
cat $(RESDIR)/header/purr_header.txt $(PURRFILE) > $(PURRFILE_TEMP)
70-
71-
# Undo the temp file swap
72-
mv -f $(PURRFILE_TEMP) $(PURRFILE)
77+
cat $(RESDIR)/header/purr_header.txt $(PURRFILE_TEMP) > $(PURRFILE)
7378

7479
# Grant execution permission.
7580
chmod +rwx $(PURRFILE)
@@ -78,13 +83,48 @@ purr:
7883

7984
adb_mock:
8085
mkdir -p $(OUTDIR)
86+
mkdir -p $(OBJDIR)
87+
8188
echo "" > $(ADBMOCKFILE)
89+
echo "" > $(ADBMOCKFILE_TEMP)
90+
91+
cat $(TESTDIR)/res/adb_log_example.sh >> "$(ADBMOCKFILE_TEMP)"
92+
cat $(TESTDIR)/mocks/adb_mock.sh >> "$(ADBMOCKFILE_TEMP)"
8293

83-
cat $(TESTDIR)/mocks/adb_mock.sh >> "$(ADBMOCKFILE)"
94+
# Remove comments/shebangs.
95+
sed -i -e '/^[ \t]*#/d' "$(ADBMOCKFILE_TEMP)"
8496

97+
# Add one shebang and the copyright notice.
98+
# We need a temp file since cat can't do it in-place.
99+
cat $(RESDIR)/header/purr_header.txt $(ADBMOCKFILE_TEMP) > $(ADBMOCKFILE)
100+
101+
# Grant execution permission.
85102
chmod +rwx $(ADBMOCKFILE)
86103

104+
.PHONY: file_tester
105+
106+
file_tester:
107+
mkdir -p $(OUTDIR)
108+
mkdir -p $(OBJDIR)
109+
110+
echo "" > $(FILETESTERFILE)
111+
echo "" > $(FILETESTERFILE_TEMP)
112+
113+
cat $(TESTDIR)/res/adb_log_example.sh >> "$(FILETESTERFILE_TEMP)"
114+
cat $(TESTDIR)/validators/file_validator.sh >> "$(FILETESTERFILE_TEMP)"
115+
116+
# Remove comments/shebangs.
117+
sed -i -e '/^[ \t]*#/d' "$(FILETESTERFILE_TEMP)"
118+
119+
# Add one shebang and the copyright notice.
120+
# We need a temp file since cat can't do it in-place.
121+
cat $(RESDIR)/header/purr_header.txt $(FILETESTERFILE_TEMP) > $(FILETESTERFILE)
122+
123+
# Grant execution permission.
124+
chmod +rwx $(FILETESTERFILE)
125+
87126
.PHONY: clean
88127

89128
clean:
90129
[ -e $(OUTDIR) ] && rm -r $(OUTDIR) || true
130+
[ -e $(OBJDIR) ] && rm -r $(OBJDIR) || true

src/execution_loop.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ while true; do
3131

3232
__purr_set_start_preview
3333

34-
# Starts and runs the actual fzf process.
35-
if [ ! -z $cached_query ]; then
34+
if [[ "$fzf_exec_flag" = "false" ]]; then # Don't exec fzf if in a testing session.
35+
sleep 5
36+
accepted=""
37+
ret=$?
38+
elif [ ! -z $cached_query ]; then # Starts and runs the actual fzf process.
3639
accepted=$(FZF_DEFAULT_COMMAND="$load_input_stream" fzf $starter_preview_command $fzfp $fzf_prompt $bind_commands $start_command --query=$cached_query)
3740
ret=$?
3841
else

src/shell/argument_parsing.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
REQUIRED_FZF_VERSION="0.40.0"
1818

19-
VERSION="2.0.1"
19+
VERSION="2.0.2"
2020

2121
USAGE=("purr"
2222
"\n[-q: Sets the default query]"
@@ -47,13 +47,19 @@ __purr_get_composite_version() {
4747
# Parse argument flags.
4848
instruction_flag=true
4949
adb_cmd_loc="adb"
50-
while getopts ':A:a:f:ivVq:' flags; do
50+
delete_dir_flag=true
51+
fzf_exec_flag=true
52+
while getopts ':XA:D:a:f:ivVq:' flags; do
5153
case $flags in
5254
q) query_string="--query=${OPTARG}" ;;
5355
a) custom_adb_params=${OPTARG} ;;
5456
A) adb_cmd_loc=${OPTARG} ;;
57+
D)
58+
delete_dir_flag=false
59+
dir_name=${OPTARG} ;;
5560
f) custom_fzf_params=${OPTARG} ;;
5661
i) instruction_flag=false ;;
62+
X) fzf_exec_flag=false ;;
5763
v)
5864
echo $VERSION
5965
exit 0

src/shell/shell_env_init.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
dir_name=$(mktemp -d /tmp/purr.XXXXXXXXXX)
17+
if [ -z $dir_name ]; then
18+
dir_name=$(mktemp -d /tmp/purr.XXXXXXXXXX)
19+
else
20+
mkdir -p $dir_name
21+
fi
1822

1923
# Run cleanup if we exit abnormally.
2024
trap "__purr_cleanup $dir_name" INT TERM

src/threads/thread_interface.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ __purr_cleanup() {
3131
fi
3232

3333
# Delete all of the cached state files.
34-
if [ -d $dir_name ]; then
34+
if [ -d $dir_name ] && [[ $delete_dir_flag = "true" ]]; then
3535
rm -r $dir_name &>/dev/null
3636
fi
3737
}

tests/mocks/adb_mock.sh

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
#!/usr/bin/env zsh
22

3-
if grep -q "devices" <<< $@; then
3+
if grep -q "devices" <<<$@; then
44
echo "List of devices attached"
55
echo "emulator-5554 device"
6-
elif grep -q "wait-for-device" <<< $@; then
7-
sleep 0.01;
8-
elif grep -q "logcat" <<< $@; then
9-
cat <<-END
10-
--------- beginning of system
11-
11-30 11:51:17.111 520 565 V DisplayPowerController2[0]: Brightness [0.39763778] reason changing to: 'manual', previous reason: 'manual [ dim ]'.
12-
11-30 11:51:17.111 520 565 I DisplayPowerController2[0]: BrightnessEvent: disp=0, physDisp=local:4619827259835644672, brt=0.39763778, initBrt=0.05, rcmdBrt=NaN, preBrt=NaN, lux=0.0, preLux=0.0, hbmMax=1.0, hbmMode=off, rbcStrength=0, thrmMax=1.0, powerFactor=1.0, wasShortTermModelActive=false, flags=, reason=manual, autoBrightness=false, strategy=InvalidBrightnessStrategy
13-
--------- beginning of main
14-
11-30 11:51:17.159 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff101010
15-
11-30 11:51:17.186 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff111111
16-
11-30 11:51:17.197 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff121212
17-
11-30 11:51:17.214 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff131313
18-
11-30 11:51:17.231 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff141414
19-
11-30 11:51:17.247 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff151515
20-
11-30 11:51:17.264 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff161616
21-
11-30 11:51:17.281 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff171717
22-
END
6+
elif grep -q "wait-for-device" <<<$@; then
7+
sleep 0.01
8+
elif grep -q "logcat" <<<$@; then
9+
echo $mocked_adb_output
2310
sleep 9999999
2411
fi

tests/res/adb_log_example.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env zsh
2+
3+
# Copyright 2023 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
mocked_adb_output=$(cat <<-END
18+
--------- beginning of system
19+
11-30 11:51:17.111 520 565 V DisplayPowerController2[0]: Brightness [0.39763778] reason changing to: 'manual', previous reason: 'manual [ dim ]'.
20+
11-30 11:51:17.111 520 565 I DisplayPowerController2[0]: BrightnessEvent: disp=0, physDisp=local:4619827259835644672, brt=0.39763778, initBrt=0.05, rcmdBrt=NaN, preBrt=NaN, lux=0.0, preLux=0.0, hbmMax=1.0, hbmMode=off, rbcStrength=0, thrmMax=1.0, powerFactor=1.0, wasShortTermModelActive=false, flags=, reason=manual, autoBrightness=false, strategy=InvalidBrightnessStrategy
21+
--------- beginning of main
22+
11-30 11:51:17.159 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff101010
23+
11-30 11:51:17.186 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff111111
24+
11-30 11:51:17.197 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff121212
25+
11-30 11:51:17.214 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff131313
26+
11-30 11:51:17.231 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff141414
27+
11-30 11:51:17.247 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff151515
28+
11-30 11:51:17.264 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff161616
29+
11-30 11:51:17.281 397 397 I android.hardware.lights-service.example: Lights setting state for id=1 to color ff171717
30+
END
31+
)

0 commit comments

Comments
 (0)