Skip to content

Commit

Permalink
tests: add more tests, test stage 1 & 2 (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Jan 5, 2025
1 parent b59bbc5 commit 8fc441e
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 58 deletions.
44 changes: 21 additions & 23 deletions .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,42 +117,40 @@ jobs:
if: runner.os == 'macOS'
uses: ./.github/actions/setup-macos-deps

- name: Print versions
run: make versions

- name: First Generation Build
run: make -j4

- name: Test the first generation
run: make test -j4

- name: Integration Test the first generation
- name: Setup Sharness
run: |
wget https://raw.githubusercontent.com/felipec/sharness/refs/tags/v1.2.1/sharness.sh
wget https://raw.githubusercontent.com/felipec/sharness/refs/tags/v1.2.1/lib-sharness/functions.sh
mv sharness.sh tests/
mkdir tests/lib-sharness
mv functions.sh tests/lib-sharness/
prove -v -j4 --shuffle tests/[0-9]*.sh
- name: Second Generation Build
run: ./build/cabin build --verbose
- name: Print versions
run: make versions

- name: Stage 1 - Build
run: make -j4

- name: Test the second generation
run: ./cabin-out/debug/cabin test --verbose
- name: Stage 1 - Print version
run: ./build/cabin version --verbose

- name: Third Generation Build & Test
run: ./cabin-out/debug/cabin --verbose run --release test --verbose
- name: Stage 1 - Test
run: make test -j4

- name: Print Cabin version
- name: Stage 1 - Integration Test
run: prove -v -j4 --shuffle tests/[0-9]*.sh

- name: Stage 2 - Build & Test
run: ./build/cabin --verbose run test --verbose

- name: Stage 2 - Print version
run: ./cabin-out/debug/cabin version --verbose

- name: Create a new project with the third generation
run: ./cabin-out/debug/cabin new hello_world
- name: Stage 2 - Integration Test
run: prove -v -j4 --shuffle tests/[0-9]*.sh
env:
CABIN_BIN: ${{ github.workspace }}/cabin-out/debug/cabin

- name: Run the new project
run: ../cabin-out/debug/cabin --verbose run
working-directory: hello_world

# - name: Print coverage
# if: success() && matrix.coverage == 'on'
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CABIN_TIDY ?= clang-tidy
PREFIX ?= /usr/local
INSTALL ?= install
COMMIT_HASH ?= $(shell git rev-parse HEAD)
COMMIT_SHORT_HASH ?= $(shell git rev-parse --short HEAD)
COMMIT_SHORT_HASH ?= $(shell git rev-parse --short=8 HEAD)
COMMIT_DATE ?= $(shell git show -s --date=format-local:'%Y-%m-%d' --format=%cd)

DEBUG_FLAGS := -g -O0 -DDEBUG
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lib-sharness/
test-results/
sharness.sh
trash*
8 changes: 3 additions & 5 deletions tests/01-cabin-exists.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/bin/sh

WHEREAMI=$(dirname "$(realpath "$0")")
export CABIN_TERM_COLOR='never'

test_description='Check if the cabin binary exists'

. $WHEREAMI/sharness.sh
WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

test_expect_success 'The cabin binary exists' '
test -x "$WHEREAMI"/../build/cabin
test -x "$CABIN_BIN"
'

test_done
14 changes: 6 additions & 8 deletions tests/02-new.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/bin/sh

WHEREAMI=$(dirname "$(realpath "$0")")
export CABIN_TERM_COLOR='never'

test_description='Test the new command'

. $WHEREAMI/sharness.sh
WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

test_expect_success 'cabin new bin hello_world' '
test_when_finished "rm -rf hello_world" &&
"$WHEREAMI"/../build/cabin new hello_world 2>actual &&
"$CABIN_BIN" new hello_world 2>actual &&
(
test -d hello_world &&
cd hello_world &&
Expand All @@ -27,7 +25,7 @@ EOF

test_expect_success 'cabin new lib hello_world' '
test_when_finished "rm -rf hello_world" &&
"$WHEREAMI"/../build/cabin new --lib hello_world 2>actual &&
"$CABIN_BIN" new --lib hello_world 2>actual &&
(
test -d hello_world &&
cd hello_world &&
Expand All @@ -43,7 +41,7 @@ EOF
'

test_expect_success 'cabin new empty' '
test_must_fail "$WHEREAMI"/../build/cabin new 2>actual &&
test_must_fail "$CABIN_BIN" new 2>actual &&
cat >expected <<-EOF &&
Error: package name must not be empty: \`\`
Error: '\''cabin new'\'' failed with exit code \`1\`
Expand All @@ -54,7 +52,7 @@ EOF
test_expect_success 'cabin new existing' '
test_when_finished "rm -rf existing" &&
mkdir -p existing &&
test_must_fail "$WHEREAMI"/../build/cabin new existing 2>actual &&
test_must_fail "$CABIN_BIN" new existing 2>actual &&
cat >expected <<-EOF &&
Error: directory \`existing\` already exists
Error: '\''cabin new'\'' failed with exit code \`1\`
Expand Down
26 changes: 12 additions & 14 deletions tests/03-fmt.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#!/bin/sh

WHEREAMI=$(dirname "$(realpath "$0")")
export CABIN_TERM_COLOR='never'

test_description='Test the fmt command'

. $WHEREAMI/sharness.sh
WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

command -v clang-format >/dev/null && test_set_prereq CLANG_FORMAT

if ! test_have_prereq CLANG_FORMAT; then
test_expect_success 'cabin fmt without clang-format' '
test_when_finished "rm -rf pkg" &&
"$WHEREAMI"/../build/cabin new pkg &&
"$CABIN_BIN" new pkg &&
cd pkg &&
(
test_must_fail "$WHEREAMI"/../build/cabin fmt 2>actual &&
test_must_fail "$CABIN_BIN" fmt 2>actual &&
cat >expected <<-EOF &&
Error: fmt command requires clang-format; try installing it by:
apt/brew install clang-format
Expand All @@ -33,12 +31,12 @@ test_expect_success 'cabin fmt' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$WHEREAMI"/../build/cabin new pkg &&
"$CABIN_BIN" new pkg &&
cd pkg &&
(
echo "int main(){}" >src/main.cc &&
md5sum src/main.cc >before &&
"$WHEREAMI"/../build/cabin fmt 2>actual &&
"$CABIN_BIN" fmt 2>actual &&
md5sum src/main.cc >after &&
test_must_fail test_cmp before after &&
cat >expected <<-EOF &&
Expand All @@ -52,11 +50,11 @@ test_expect_success 'cabin fmt no targets' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$WHEREAMI"/../build/cabin new pkg &&
"$CABIN_BIN" new pkg &&
cd pkg &&
(
rm src/main.cc &&
"$WHEREAMI"/../build/cabin fmt 2>actual &&
"$CABIN_BIN" fmt 2>actual &&
cat >expected <<-EOF &&
Warning: no files to format
EOF
Expand All @@ -68,11 +66,11 @@ test_expect_success 'cabin fmt without manifest' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$WHEREAMI"/../build/cabin new pkg &&
"$CABIN_BIN" new pkg &&
cd pkg &&
(
rm cabin.toml &&
test_must_fail "$WHEREAMI"/../build/cabin fmt 2>actual &&
test_must_fail "$CABIN_BIN" fmt 2>actual &&
cat >expected <<-EOF &&
Error: could not find \`cabin.toml\` here and in its parents
EOF
Expand All @@ -85,11 +83,11 @@ test_expect_success 'cabin fmt without name in manifest' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$WHEREAMI"/../build/cabin new pkg &&
"$CABIN_BIN" new pkg &&
cd pkg &&
(
echo "[package]" >cabin.toml &&
test_must_fail "$WHEREAMI"/../build/cabin fmt 2>actual &&
test_must_fail "$CABIN_BIN" fmt 2>actual &&
cat >expected <<-EOF &&
Error: [error] toml::value::at: key "name" not found
--> $(realpath $OUT)/pkg/cabin.toml
Expand Down
12 changes: 5 additions & 7 deletions tests/04-init.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#!/bin/sh

WHEREAMI=$(dirname "$(realpath "$0")")
export CABIN_TERM_COLOR='never'

test_description='Test the init command'

. $WHEREAMI/sharness.sh
WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

test_expect_success 'cabin init' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
mkdir $OUT/pkg &&
cd $OUT/pkg &&
"$WHEREAMI"/../build/cabin init 2>actual &&
"$CABIN_BIN" init 2>actual &&
cat >expected <<-EOF &&
Created binary (application) \`pkg\` package
EOF
Expand All @@ -25,13 +23,13 @@ test_expect_success 'cabin init existing' '
test_when_finished "rm -rf $OUT" &&
mkdir $OUT/pkg &&
cd $OUT/pkg &&
"$WHEREAMI"/../build/cabin init 2>actual &&
"$CABIN_BIN" init 2>actual &&
cat >expected <<-EOF &&
Created binary (application) \`pkg\` package
EOF
test_cmp expected actual &&
test -f cabin.toml
test_must_fail "$WHEREAMI"/../build/cabin init 2>actual &&
test_must_fail "$CABIN_BIN" init 2>actual &&
cat >expected <<-EOF &&
Error: cannot initialize an existing cabin package
Error: '\''cabin init'\'' failed with exit code \`1\`
Expand Down
19 changes: 19 additions & 0 deletions tests/05-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

test_description='Test the version command'

WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

test_expect_success 'cabin version' '
VERSION=$(grep -m1 version "$WHEREAMI"/../cabin.toml | cut -f 2 -d'\''"'\'') &&
COMMIT_SHORT_HASH=$(git rev-parse --short=8 HEAD) &&
COMMIT_DATE=$(git show -s --date=format-local:'\''%Y-%m-%d'\'' --format=%cd) &&
"$CABIN_BIN" version 1>actual &&
cat >expected <<-EOF &&
cabin $VERSION ($COMMIT_SHORT_HASH $COMMIT_DATE)
EOF
test_cmp expected actual
'

test_done
36 changes: 36 additions & 0 deletions tests/06-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

test_description='Test the run command'

WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

test_expect_success 'cabin run hello_world' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$CABIN_BIN" new hello_world &&
cd hello_world &&
"$CABIN_BIN" run 1>stdout 2>stderr &&
(
test -d cabin-out &&
test -d cabin-out/debug &&
test -x cabin-out/debug/hello_world
) &&
(
TIME=$(cat stderr | grep Finished | grep -o '\''[0-9]\+\.[0-9]\+'\'') &&
cat >stderr_exp <<-EOF &&
Compiling hello_world v0.1.0 ($(realpath $OUT)/hello_world)
Finished \`dev\` profile [unoptimized + debuginfo] target(s) in ${TIME}s
EOF
test_cmp stderr_exp stderr
) &&
(
cat >stdout_exp <<-EOF &&
Hello, world!
EOF
test_cmp stdout_exp stdout
)
'

test_done
7 changes: 7 additions & 0 deletions tests/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

export WHEREAMI=$(dirname "$(realpath "$0")")
export CABIN_BIN="${CABIN_BIN:-"$WHEREAMI/../build/cabin"}"
export CABIN_TERM_COLOR='never'

. $WHEREAMI/sharness.sh

0 comments on commit 8fc441e

Please sign in to comment.