diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml index cdde28313..3de23c409 100644 --- a/.github/workflows/cpp.yml +++ b/.github/workflows/cpp.yml @@ -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' diff --git a/Makefile b/Makefile index f455fa932..5da73ed45 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/tests/.gitignore b/tests/.gitignore index ff1cc7b4f..92c474764 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,3 +1,4 @@ lib-sharness/ test-results/ sharness.sh +trash* diff --git a/tests/01-cabin-exists.sh b/tests/01-cabin-exists.sh index 2b5d5fc8d..cdbc425c6 100644 --- a/tests/01-cabin-exists.sh +++ b/tests/01-cabin-exists.sh @@ -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 diff --git a/tests/02-new.sh b/tests/02-new.sh index 09459ff00..9700abae4 100644 --- a/tests/02-new.sh +++ b/tests/02-new.sh @@ -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 && @@ -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 && @@ -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\` @@ -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\` diff --git a/tests/03-fmt.sh b/tests/03-fmt.sh index 44a7939f6..66cd0c637 100644 --- a/tests/03-fmt.sh +++ b/tests/03-fmt.sh @@ -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 @@ -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 && @@ -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 @@ -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 @@ -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 diff --git a/tests/04-init.sh b/tests/04-init.sh index c0835ccbf..8633df67b 100644 --- a/tests/04-init.sh +++ b/tests/04-init.sh @@ -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 @@ -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\` diff --git a/tests/05-version.sh b/tests/05-version.sh new file mode 100644 index 000000000..c2b8dd012 --- /dev/null +++ b/tests/05-version.sh @@ -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 diff --git a/tests/06-run.sh b/tests/06-run.sh new file mode 100644 index 000000000..7bc8a4d72 --- /dev/null +++ b/tests/06-run.sh @@ -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 diff --git a/tests/setup.sh b/tests/setup.sh new file mode 100644 index 000000000..66f876bbe --- /dev/null +++ b/tests/setup.sh @@ -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