Skip to content

Commit

Permalink
tests: add + support runtime tests (#309)
Browse files Browse the repository at this point in the history
* ci: pass `-wipe-data` to emulator

* ci: use newer build-tools, and matching emulator API

* ci: use system provided `vab` in `vab_error_test.v`

* ci: pass `-d vab_no_notices` via `VFLAGS`
  • Loading branch information
larpon authored Sep 20, 2024
1 parent cc06c7b commit 42f106b
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -v 3
steps:
- name: Checkout V
Expand Down Expand Up @@ -125,6 +126,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 15
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -v 3
steps:
- name: Checkout V
Expand Down Expand Up @@ -187,6 +189,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -v 3
steps:
- name: Checkout V
Expand Down Expand Up @@ -266,6 +269,7 @@ jobs:
runs-on: windows-latest
timeout-minutes: 10
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -cg -v 3 --api 31 --build-tools 30.0.3
steps:

Expand Down
36 changes: 8 additions & 28 deletions .github/workflows/ci_emulator_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: macos-12
timeout-minutes: 45
env:
VAB_FLAGS: -cg -f '-d no_load_styles' -v 3 --api 30 --build-tools 29.0.0 --device auto --log-clear --archs x86_64
VAB_FLAGS: -cg -f '-d no_load_styles' -v 3 --api 30 --build-tools 33.0.2 --device auto --log-clear --archs x86_64
VFLAGS: -no-parallel
steps:
- uses: actions/setup-java@v4
Expand Down Expand Up @@ -50,9 +50,13 @@ jobs:
- name: Run vab --help
run: vab --help

- name: Install build-tools 29
- name: Install build-tools 33.0.2
run: |
vab install "build-tools;29.0.0"
vab install "build-tools;33.0.2"
- name: Install platform android-30
run: |
vab install "platforms;android-30"
- name: Install AAB dependencies
run: |
Expand All @@ -62,9 +66,6 @@ jobs:
- name: Run vab doctor
run: vab doctor

- name: Run tests
run: v test ~/.vmodules/vab

- name: Cache emulator
id: cache-emulator
uses: actions/cache@v4
Expand All @@ -88,11 +89,9 @@ jobs:
# Don't set Java > 8 here (emulator can't be started otherwise, lol) #export JAVA_HOME=$JAVA_HOME_11_X64
#pgrep emulator >/dev/null 2>&1 ||
$ANDROID_SDK_ROOT/emulator/emulator -avd test -no-metrics -no-snapshot -no-window -no-boot-anim -camera-back emulated -camera-front emulated -gpu swiftshader_indirect &
$ANDROID_SDK_ROOT/emulator/emulator -avd test -wipe-data -no-metrics -no-snapshot -no-window -no-boot-anim -camera-back emulated -camera-front emulated -gpu swiftshader_indirect &
adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'
# Remove packages here if cache is run, and emulator failed
# Debugging
#export ADB_TAGS="SOKOL_APP:D"
#export ADB_TAGS="$ADB_TAGS V_ANDROID:D v_test_app:D"
Expand All @@ -104,9 +103,6 @@ jobs:
echo "Testing vab deployment *after* build"
vab --package-id "io.v.ci.vab.apk.deploytest" --name "V DEPLOY TEST APK" v/examples/gg/bezier.v && vab v_deploy_test_apk.apk
vab --package-id "io.v.ci.vab.aab.deploytest" --name "V DEPLOY TEST AAB" --package aab v/examples/gg/bezier.v && vab v_deploy_test_aab.aab
# Remove app in case cache is run
# adb uninstall --user 0 "io.v.ci.vab.apk.deploytest"
# adb uninstall --user 0 "io.v.ci.vab.aab.deploytest"
# 'flappylearning' can build but running is currently broken on Android
# Skip fireworks for now
Expand All @@ -125,10 +121,6 @@ jobs:
# AAB
echo "Compiling aab from examples/$example ($package_id)"
vab --package aab --package-id "io.v.aab.$package_id" run v/examples/$example
# Remove app in case cache is run
adb uninstall "io.v.apk.$package_id"
adb uninstall "io.v.aab.$package_id"
done
# Output test
Expand All @@ -139,10 +131,6 @@ jobs:
echo "Looking for traces of BDWGC"
cat /tmp/logcat.dump.txt | grep -q 'BDWGC : Grow'; if [ ! $? -eq 0 ]; then cat /tmp/logcat.dump.txt; fi
# Remove app in case cache is run
echo "Uninstalling v/examples/tetris"
adb uninstall io.v.ci.vab.apk.examples.tetris
# V UI
echo "Installing V UI"
git clone --depth 1 https://github.com/vlang/ui
Expand All @@ -162,10 +150,6 @@ jobs:
# AAB
echo "Compiling aab from ui/examples/$example ($package_id)"
vab --package aab --package-id "io.v.aab.ui.$package_id" run ui/examples/$example
# Remove app in case cache is run
adb uninstall "io.v.apk.ui.$package_id"
adb uninstall "io.v.aab.ui.$package_id"
done
# Output test
Expand All @@ -176,9 +160,5 @@ jobs:
echo "Looking for traces of BDWGC"
cat /tmp/logcat.dump.txt | grep -q 'BDWGC : Grow'; if [ ! $? -eq 0 ]; then cat /tmp/logcat.dump.txt; fi
# Remove app in case cache is run
echo "Uninstalling ui/examples/calculator"
adb uninstall io.v.ui.ci.examples.calculator
echo "Killing emulator"
adb -s emulator-5554 emu kill
3 changes: 3 additions & 0 deletions .github/workflows/matrix_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
fail-fast: false
timeout-minutes: 20
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -v 3 --build-tools 29.0.0
steps:
- uses: actions/setup-java@v4
Expand Down Expand Up @@ -101,6 +102,7 @@ jobs:
fail-fast: false
timeout-minutes: 20
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -v 3 -gc none --build-tools 29.0.0
steps:
- uses: actions/setup-java@v4
Expand Down Expand Up @@ -166,6 +168,7 @@ jobs:
fail-fast: false
timeout-minutes: 20
env:
VFLAGS: -d vab_no_notices
VAB_FLAGS: -v 3 --build-tools 29.0.0
steps:
- uses: actions/setup-java@v4
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ here](https://github.com/vlang/vab/blob/3091ade4c9792c6a37596ccfa9299fb269d3160e
In either case the following dependencies is required before `vab` will work
as intented.

Dependencies:
## Runtime dependencies
* V
* Java (JDK) >= 8 (>= 9 on Windows)
* Android SDK
Expand Down Expand Up @@ -127,7 +127,8 @@ EMULATOR # Absolute path to the emulator to use
```

```bash
VAB_FLAGS # Use to pass flags to vab. Command-line flags overwrites any flags/values set via VAB_FLAGS.
VAB_EXE # Absolute path to a vab executable (Used in tests and sub-cmd execution)
VAB_FLAGS # Used to pass flags to vab. Command-line flags overwrites any flags/values set via VAB_FLAGS.
VAB_KILL_ADB # Set to let vab kill adb after use. This is useful on some hosts.
```

Expand Down Expand Up @@ -203,6 +204,12 @@ The accompaning script used in the video can be found here:
See [*"Where is the `examples` folder?"*](docs/FAQ.md#where-is-the-examples-folder)
in the [FAQ](docs/FAQ.md).

# Tests

`vab`, like many other V modules, can be tested with `v test .`.
Note that `vab` has *runtime* tests that requires all [runtime dependencies](#runtime-dependencies)
to be installed in order for the tests to run correctly.

# Notes

`vab` targets as low an API level as possible by default for maximum
Expand Down
1 change: 1 addition & 0 deletions cli/cli.v
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub const vab_env_vars = [
'AAPT2',
'JAVA_HOME',
'VEXE',
'VAB_EXE',
'VMODULES',
]

Expand Down
85 changes: 85 additions & 0 deletions tests/at-runtime/vab_build_apk_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import vab.vabxt
import vab.vxt
import vab.android.util

const test_dir_base = os.join_path(os.vtmp_dir(), 'vab', 'tests', 'runtime')
const apk_arch_dirs = ['arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64', 'armeabi']

fn setup_apk_build(id string) (string, string) {
test_dir := os.join_path(test_dir_base, id)
os.rm(test_dir) or {}
os.mkdir_all(test_dir) or { panic('mkdir_all failed making "${test_dir}": ${err}') }

// vab (per design) implicitly deploys to any devices sat via `--device-id`.
// Make sure no deployment is done after build if CI/other sets `ANDROID_SERIAL`
os.unsetenv('ANDROID_SERIAL')
vab := vabxt.vabexe()
assert vab != '', 'vab needs to be installed to run this test'
return vab, test_dir
}

fn v_example(path string) string {
v_root := vxt.home()
examples_root := os.join_path(v_root, 'examples')
example := os.join_path(examples_root, ...path.split('/'))
assert os.is_file(example) || os.is_dir(example) == true, 'example not found. Ensure a full V source install (with examples) is present'
return example
}

fn run(cmd string) {
eprintln('running: ${cmd}')
res := os.execute(cmd)
if res.exit_code != 0 {
dump(res.output)
}
assert res.exit_code == 0
}

fn extract_and_check_apk(libname string, path string) {
expected_lib_name := libname
expected_apk := os.join_path(path, '${expected_lib_name}.apk')
assert os.is_file(expected_apk)

extract_dir := os.join_path(path, 'extracted')
extracted_apk_path := os.join_path(extract_dir, expected_lib_name)
util.unzip(expected_apk, extracted_apk_path) or {
panic('unzip failed extracting "${expected_apk}": ${err}')
}

dump(os.ls(extracted_apk_path) or { panic('ls failed on "${extracted_apk_path}": ${err}') })
// test that expected libs are actually present in the apk
for arch in apk_arch_dirs {
lib_dir := os.join_path(extracted_apk_path, 'lib', arch)
dump(os.ls(lib_dir) or { panic('ls failed on "${lib_dir}": ${err}') })
assert os.is_file(os.join_path(lib_dir, 'lib${expected_lib_name}.so'))
}
}

fn test_build_apk_way_1() {
vab, test_dir := setup_apk_build(@FN)

vab_cmd := [vab, '-o', test_dir, v_example('gg/worker_thread.v')].join(' ')
run(vab_cmd)

extract_and_check_apk('v_test_app', test_dir)
}

fn test_build_apk_way_2() {
vab, test_dir := setup_apk_build(@FN)

vab_cmd := [vab, v_example('sokol/particles'), '-o', test_dir].join(' ')
run(vab_cmd)

extract_and_check_apk('v_test_app', test_dir)
}

fn test_build_apk_way_3() {
vab, test_dir := setup_apk_build(@FN)

vab_cmd := [vab, '-f "-d trace_moves_spool_to_sbin"', v_example('sokol/particles'),
'-o', test_dir].join(' ')
run(vab_cmd)

extract_and_check_apk('v_test_app', test_dir)
}
14 changes: 4 additions & 10 deletions tests/vab_error_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import toml
import sync.pool
import v.util.diff
import vab.vxt
import vab.vabxt
import runtime

const vab_home = os.real_path(os.dir(os.dir(@FILE)))
const vab_test_dirs = [
os.join_path(vab_home, 'tests'),
]
const vexe = vxt.vexe()
const vab_exe = compile_vab()
const vab_exe = vabxt.vabexe()

const should_autofix = os.getenv('VAUTOFIX') != ''
const empty_toml_map = map[string]toml.Any{}

Expand Down Expand Up @@ -108,6 +110,7 @@ fn sync_run(job TOMLTestJob) &TOMLTestJobResult {
|| execute.contains('|') {
panic('Only single vab commands allowed')
}
os.unsetenv('ANDROID_SERIAL')
res := os.execute(execute.replace_once('vab', vab_exe))

mut expected := ''
Expand Down Expand Up @@ -146,15 +149,6 @@ fn sync_run(job TOMLTestJob) &TOMLTestJobResult {
}
}

fn compile_vab() string {
// TODO vtest vflags: ??? or env VFLAGS???
res := os.execute([vexe, '-d vab_no_notices', '"${vab_home}"'].join(' '))
if res.exit_code != 0 {
panic('command failed building vab in "${vab_home}":\n${res.output}')
}
return os.join_path(vab_home, 'vab')
}

fn clean_line_endings(s string) string {
mut res := s.trim_space()
res = res.replace(' \n', '\n')
Expand Down
Loading

0 comments on commit 42f106b

Please sign in to comment.