Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comprehensive test suite #76

Merged
merged 22 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c2d903f
tests: Add `tests.sh` (simple testing framework)
obiwac Nov 24, 2024
f55f11c
ci: Update to running `tests.sh` for the tests
obiwac Nov 24, 2024
8bf89a9
readme: Document testing
obiwac Nov 24, 2024
599b55e
logging: Fix success and info logs erroneously outputting to `stderr`
obiwac Nov 24, 2024
fac3379
tests: Move setting `$SUDO` to `tests.sh`
obiwac Nov 24, 2024
59aedce
tests: Add chown regression test for #69
obiwac Nov 24, 2024
b7edd2a
ci(cirrus): Fix bootstrap and test scripts
obiwac Nov 24, 2024
320ff51
tests: Export `$SUDO`
obiwac Nov 24, 2024
6c450c3
tests: Fix for sudo on Linux
obiwac Nov 24, 2024
bc4b7e1
tests: Install bob first
obiwac Nov 24, 2024
eec37c5
tests/chown: Fix operators
obiwac Nov 24, 2024
69ca37b
tests: Export nothing for `$SUDO` if already root
obiwac Nov 24, 2024
da9a14a
tests/install: Install using bootstrapped bob, otherwise we're copyin…
obiwac Nov 24, 2024
1431d06
install: Move prefix assertion to after null-check, so it doesn't tri…
obiwac Nov 24, 2024
9dfc2db
tests: Pipe errors to `stderr`
obiwac Nov 24, 2024
e057c8d
tests: Run path traversal order regression test, for #71
obiwac Nov 24, 2024
a515187
ci: Don't bootstrap, `tests.sh` does this for us
obiwac Nov 24, 2024
2b57720
tests: Keep environment variables when running `sudo`
obiwac Nov 24, 2024
c506351
tests: Bring `SUDO` out as an alias
obiwac Nov 24, 2024
f1cecb8
tests: Couple minor fixes
obiwac Nov 24, 2024
d900b1b
tests/chown: Fix user creation on FreeBSD
obiwac Nov 24, 2024
8848d61
tests: Add frugality regression test, for #70
obiwac Nov 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 6 additions & 48 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,8 @@ task:
timeout_in: 5m
container:
image: gcc:latest
env:
ASAN_OPTIONS: detect_leaks=0
bootstrap_script:
- sh bootstrap.sh
test_build_bob_with_bob_script:
- $(realpath .bootstrap/bob) build
test_run_bob_with_bob_script:
- rm -rf .bob
- $(realpath .bootstrap/bob) run build
test_install_bob_script:
- rm -rf .bob
- $(realpath .bootstrap/bob) install
test_run_installed_bob_script:
- rm -rf .bob
- bob build
tests_script:
- sh tests.sh
amd64_artifacts:
path: ".bob/prefix/bin/bob"

Expand All @@ -26,21 +13,8 @@ task:
timeout_in: 5m
arm_container:
image: gcc:latest
env:
ASAN_OPTIONS: detect_leaks=0
bootstrap_script:
- sh bootstrap.sh
test_build_bob_with_bob_script:
- $(realpath .bootstrap/bob) build
test_run_bob_with_bob_script:
- rm -rf .bob
- $(realpath .bootstrap/bob) run build
test_install_bob_script:
- rm -rf .bob
- $(realpath .bootstrap/bob) install
test_run_installed_bob_script:
- rm -rf .bob
- bob build
tests_script:
- sh tests.sh
arm64_artifacts:
path: ".bob/prefix/bin/bob"

Expand All @@ -54,23 +28,7 @@ compute_engine_instance:
task:
name: Test Bob with Clang on FreeBSD (x86_64)
timeout_in: 30m
env:
ASAN_OPTIONS: detect_leaks=0
setup_script:
- uname -a
- df -h
bootstrap_script:
- sh bootstrap.sh
test_build_bob_with_bob_script:
- $(realpath .bootstrap/bob) build
test_run_bob_with_bob_script:
- rm -rf .bob
- $(realpath .bootstrap/bob) run build
test_install_bob_script:
- rm -rf .bob
- $(realpath .bootstrap/bob) install
test_run_installed_bob_script:
- rm -rf .bob
- bob build
tests_script:
- sh tests.sh
amd64_artifacts:
path: ".bob/prefix/bin/bob"
40 changes: 4 additions & 36 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,15 @@ on: [push, workflow_dispatch]
jobs:
run_tests:
runs-on: ubuntu-latest
env:
ASAN_OPTIONS: detect_leaks=0
steps:
- uses: actions/checkout@v4
- name: Set up compiler
uses: egor-tensin/setup-clang@v1
with:
version: latest
platform: x64
- name: Bootstrap
run: sh bootstrap.sh
- name: Test building Bob using Bob
run: .bootstrap/bob build
- name: Test running the version of Bob built with Bob
run: |
rm -rf .bob
.bootstrap/bob run build
- name: Test installing Bob
run: |
rm -rf .bob
sudo -E .bootstrap/bob install
- name: Test running the installed version of Bob
run: |
rm -rf .bob
bob build
- name: Run tests
run: sh tests.sh
- name: Set up tmate session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
Expand All @@ -43,26 +27,10 @@ jobs:
matrix:
os: [macos-13, macos-latest] # 'macos-13' is Intel, 'macos-latest' is M1.
runs-on: ${{ matrix.os }}
env:
ASAN_OPTIONS: detect_leaks=0
steps:
- uses: actions/checkout@v4
- name: Bootstrap
run: sh bootstrap.sh
- name: Test building Bob using Bob
run: .bootstrap/bob build
- name: Test running the version of Bob built with Bob
run: |
rm -rf .bob
.bootstrap/bob run build
- name: Test installing Bob
run: |
rm -rf .bob
sudo -E .bootstrap/bob install
- name: Test running the installed version of Bob
run: |
rm -rf .bob
bob build
- name: Run tests
run: sh tests.sh
- name: Set up tmate session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ compile_commands.json
.cache
.bob
.bootstrap
.test-out
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ To install a project with Bob, run:
```console
bob install
```

## Testing

To run the Bob tests, simply run:

```console
sh tests.sh
```
2 changes: 1 addition & 1 deletion src/class/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void get_include_deps(compile_task_t* task, char* cc) {
// -MT "": Exclude the source file from the output.
// TODO Can we do this in parallel easily with 'cmd_exec_async'?

cmd_t __attribute__((cleanup(cmd_free))) cmd;
cmd_t __attribute__((cleanup(cmd_free))) cmd = {0};
cmd_create(&cmd, cc, "-fdiagnostics-color=always", "-MM", "-MT", "", task->src, NULL);
add_flags(&cmd, task);

Expand Down
1 change: 1 addition & 0 deletions src/class/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <errno.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static flamingo_val_t* list_val = NULL;
Expand Down
3 changes: 3 additions & 0 deletions src/frugal.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ bool frugal_flags(flamingo_val_t* flags, char* out) {

fclose(f);

// Note that we want to compare the order of these flags too, so just compare the strings.
// There could be some cases where flipping the order of flags could change the build, which is perhaps rare but let's not try to be too smart here.

if (strcmp(prev_flag_str, flag_str) != 0) {
goto write_out;
}
Expand Down
6 changes: 3 additions & 3 deletions src/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <libgen.h>

static flamingo_val_t* install_map = NULL;
static char const* prefix;
static char const* prefix = NULL;

int setup_install_map(flamingo_t* flamingo, char const* _prefix) {
// Find the install map.
Expand Down Expand Up @@ -183,12 +183,12 @@ static int install_single(flamingo_val_t* key_val, char* val, bool installing_co
}

int install_all(char const* _prefix) {
assert(_prefix == prefix);

if (install_map == NULL || prefix == NULL) {
return 0;
}

assert(_prefix == prefix);

for (size_t i = 0; i < install_map->map.count; i++) {
flamingo_val_t* const val_val = install_map->map.vals[i];
char* const val = strndup(val_val->str.str, val_val->str.size);
Expand Down
1 change: 0 additions & 1 deletion src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
5 changes: 2 additions & 3 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

// external variables

Expand Down Expand Up @@ -56,5 +55,5 @@ void log_already_done(char const* cookie, char const* prefix, char const* past);
#define LOG_FATAL(...) vlog(stderr, "💀 " BOLD PURPLE, __VA_ARGS__)
#define LOG_ERROR(...) vlog(stderr, "🛑 " BOLD RED, __VA_ARGS__)
#define LOG_WARN(...) vlog(stderr, "🚸 " REGULAR YELLOW, __VA_ARGS__)
#define LOG_INFO(...) vlog(stderr, "👷 " REGULAR BLUE, __VA_ARGS__)
#define LOG_SUCCESS(...) vlog(stderr, "✅ " REGULAR GREEN, __VA_ARGS__)
#define LOG_INFO(...) vlog(stdout, "👷 " REGULAR BLUE, __VA_ARGS__)
#define LOG_SUCCESS(...) vlog(stdout, "✅ " REGULAR GREEN, __VA_ARGS__)
43 changes: 43 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh

. tests/common.sh

echo "Bootstrapping Bob..."
sh bootstrap.sh

echo "Installing Bob..."
SUDO .bootstrap/bob install > /dev/null

# Actually run the tests.

all_passed=1

for test in $(ls -p tests | grep -v /); do
if [ $test = "common.sh" ]; then
continue
fi

if [ -d $test ]; then
continue
fi

rm -rf .bob $TEST_OUT
mkdir -p $TEST_OUT

echo -n "Running test $test... "
sh tests/$test > /dev/null

if [ $? = 0 ]; then
echo "PASSED"
else
echo "FAILED"
all_passed=0
fi
done

if [ $all_passed = 0 ]; then
echo "TESTS FAILED!" >&2
exit 1
else
echo "ALL TESTS PASSED!"
fi
9 changes: 9 additions & 0 deletions tests/bob.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
set -e

. tests/common.sh

# Test building Bob itself.

bob run -o $TEST_OUT/test-bob build
bob run -o $TEST_OUT/test-bob run -o $TEST_OUT/test-bob2 build
55 changes: 55 additions & 0 deletions tests/chown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/sh

. tests/common.sh

# Regression test for the following PR:
# https://github.com/inobulles/bob/pull/69
# Basically, there's this feature which chown's generated files to the owner of the project directory when running Bob as root.
# The issue though is that, before #69, even installed files would be chown'd to the owner of the project directory.
# The result was that on FreeBSD, the ldconfig service would refuse to cache the libraries in /usr/local/lib because they weren't owned by root, which is obviously very bad as nothing that linked against those would work.

# Make sure everything is clean.

INSTALL_PATH=/usr/local/share/bob_chown_test.fl
SUDO rm -rf tests/chown/.bob $INSTALL_PATH

# Create a user.

USER=bobtestuser

if [ $(uname) = "FreeBSD" ]; then
SUDO pw adduser -n $USER
elif [ $(uname) = "Linux" ]; then
SUDO useradd -m $USER
elif [ $(uname) = "Darwin" ]; then
out=$(SUDO sysadminctl -addUser $USER 2>&1)

if [ $? != 0 ]; then
echo "Failed to create user: $out" >&2
exit 1
fi
else
echo "Unsupported OS (don't know how to create a user)." >&2
exit 1
fi

# Chown the project directory to that user.

SUDO chown -R bobtestuser tests/chown
SUDO bob -C tests/chown build

if [ ! -z "$(find tests/chown ! -user bobtestuser)" ]; then
echo "Some files are not owned by bobtestuser in tests/chown." >&2
exit 1
fi

SUDO bob -C tests/chown install

# XXX Annoyingly, 'stat -f %Su' doesn't work on Linux (you have to use 'stat -c %U').

owner=$(ls -ld /usr/local/share/bob_chown_test.fl | awk '{print $3}')

if [ $owner != "root" ]; then
echo "Installed files are not owned by root." >&2
exit 1
fi
8 changes: 8 additions & 0 deletions tests/chown/build.fl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 Aymeric Wibo

import bob

install = {
"build.fl": "share/bob_chown_test.fl",
}
20 changes: 20 additions & 0 deletions tests/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

export ASAN_OPTIONS=detect_leaks=0 # XXX For now, let's not worry about leaks.
export TEST_OUT=.test-out

# Find doas or sudo.

if [ $(id -u) = 0 ]; then
alias SUDO=""
elif [ $(uname) = "Linux" ]; then
# XXX Linux is annoying, 'which -s' doesn't exist.
alias SUDO="sudo -E"
elif which -s doas; then
alias SUDO="doas"
elif which -s sudo; then
alias SUDO="sudo -E"
else
echo "No sudo or doas found." >&2
exit 1
fi
Loading