Skip to content

Add craft test command modeled on cargo test#34

Open
DaanyaalSobani wants to merge 1 commit into
randerson112:mainfrom
DaanyaalSobani:feat/craft-test
Open

Add craft test command modeled on cargo test#34
DaanyaalSobani wants to merge 1 commit into
randerson112:mainfrom
DaanyaalSobani:feat/craft-test

Conversation

@DaanyaalSobani

Copy link
Copy Markdown

Implements the proposal in #33.

What

Adds a craft test command that discovers .c / .cpp files in a project's tests/ directory and treats each one as an independent test binary — the cargo integration test model (one binary per file). craft test:

  • Builds via the same flow as craft build (test targets are emitted in the generated CMakeLists.txt when tests/ exists, so craft build also picks them up).
  • Runs each test binary, suppressing per-test output.
  • Reports per-binary pass/fail and an aggregate summary.

Example

// tests/arithmetic.c
#include <assert.h>
#include "math.h"

int main(void) {
    assert(math_add(2, 3) == 5);
    return 0;
}
$ craft test
Building...
Project built successfully

running 3 tests
test adds_test ... ok
test multiplies_test ... ok
test subtracts_test ... ok

test result: ok. 3 passed; 0 failed

Options

  • craft test <name> — substring filter (cargo-style)
  • craft test --no-run — build test binaries without running them

Implementation

File Change
include/test.h, src/test.c New — test discovery, build via handle_build(), find each test binary across known multi-config layouts (build/, build/Debug/, build/Release/, build/MinSizeRel/, build/RelWithDebInfo/), report results
src/cmake.c New write_tests() — emits an if(EXISTS tests/) block with file(GLOB ... CONFIGURE_DEPENDS). Library project types auto-link against the project library; executable projects do not
src/parser.c + include/parser.h Registered test command + --no-run option
src/dispatcher.c Routes test to handle_test
src/help.c craft help test + entry in main help

Library vs. executable projects

  • Library targets (static-library, shared-library, header-only): test files are auto-linked against the project library — write tests in terms of the project's public headers.
  • Executable targets: test files are not auto-linked; cargo's equivalent for binary crates also requires explicit setup.

Trying it out

If you'd like to poke at this end-to-end without setting anything up yourself, I built a small public playground:

DaanyaalSobani/craft-playground — has a math/ static-library project with three passing tests and a repeatable bash harness at scripts/test_craft_test.sh that exercises six scenarios (22 checks total: all-passing, filter, --no-run, known-failing test, missing tests/, no-match filter). Points at this branch's craft.exe by default; override via the CRAFT env var.

Scope

Deliberately matches cargo's integration test model only. Per-function #[test]-style granularity (one file, many independently-reported tests) is called out as a future direction in #33 — happy to layer that on later via either a small bundled framework or a [test] section in craft.toml if you'd prefer.

Testing

Tested on Windows 11 / MSVC 19.42 / CMake 4.3.2 against the playground harness — 22/22 checks pass.

Discovers .c/.cpp files in a project's tests/ directory and treats
each one as an independent test binary, matching cargo's integration
test model (one binary per file). `craft test` builds via the same
flow as `craft build`, runs each test binary, and reports per-binary
pass/fail with an aggregate summary.

Test targets are emitted in the generated CMakeLists.txt inside an
`if(EXISTS tests)` block, so `craft build` also picks them up when
tests are present. For library project types each test target is
linked against the project library automatically; executable
projects skip the link step.

Options:
  craft test <name>   filter tests by substring
  craft test --no-run build the test binaries but do not run them

Implements the proposal in randerson112#33.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant