Skip to content

Commit b9bdcca

Browse files
committed
code refactoring to improve modularity
1 parent 4009875 commit b9bdcca

File tree

5 files changed

+916
-828
lines changed

5 files changed

+916
-828
lines changed

BUILD+TEST.MD

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,223 @@ Tips:
5151
- Add new tests by updating `tests/CMakeLists.txt` and adding `TEST(...)` cases.
5252
- Note: when writing code that directly uses the in-memory transport, include `mcp/InMemoryTransport.hpp`. For StdioTransport, include `mcp/StdioTransport.hpp`.
5353

54+
## Command variations and options (Linux/macOS and Windows via WSL)
55+
56+
Below are commonly used Docker Buildx/CTest variants with both native (Linux/macOS) and Windows (PowerShell via WSL2 Ubuntu) forms. Substitute your WSL distro name if not `Ubuntu`.
57+
58+
### Full test run (default)
59+
60+
- Linux/macOS:
61+
```bash
62+
cd /path/to/mcp-cpp
63+
docker buildx build -f Dockerfile.demo --target test --progress=plain --pull --load -t mcp-cpp-test .
64+
```
65+
66+
- Windows (PowerShell via WSL2 Ubuntu):
67+
```powershell
68+
wsl -d Ubuntu -- bash -lc "cd /mnt/c/Work/mcp-cpp && docker buildx build -f Dockerfile.demo --target test --progress=plain --pull --load -t mcp-cpp-test ."
69+
```
70+
71+
### Full rebuild (disable layer cache)
72+
73+
- Linux/macOS:
74+
```bash
75+
docker buildx build -f Dockerfile.demo --target test --no-cache --progress=plain --pull --load -t mcp-cpp-test .
76+
```
77+
78+
- Windows (WSL):
79+
```powershell
80+
wsl -d Ubuntu -- bash -lc "cd /mnt/c/Work/mcp-cpp && docker buildx build -f Dockerfile.demo --target test --no-cache --progress=plain --pull --load -t mcp-cpp-test ."
81+
```
82+
83+
### Build only (don’t run CTest yet)
84+
85+
- Linux/macOS:
86+
```bash
87+
docker buildx build -f Dockerfile.demo --target build --progress=plain --pull --load -t mcp-cpp-build .
88+
```
89+
90+
- Windows (WSL):
91+
```powershell
92+
wsl -d Ubuntu -- bash -lc "cd /mnt/c/Work/mcp-cpp && docker buildx build -f Dockerfile.demo --target build --progress=plain --pull --load -t mcp-cpp-build ."
93+
```
94+
95+
### Run CTest inside the built container
96+
97+
- Run entire suite with verbose failure output:
98+
```powershell
99+
wsl -d Ubuntu -- bash -lc "docker run --rm -t mcp-cpp-build bash -lc 'cd /src && ctest --test-dir build --output-on-failure'"
100+
```
101+
102+
- Run tests matching a regex (single or group):
103+
```powershell
104+
wsl -d Ubuntu -- bash -lc "docker run --rm -t mcp-cpp-build bash -lc 'cd /src && ctest --test-dir build -R ClientCache --output-on-failure'"
105+
```
106+
107+
- Re-run only failed tests from the last run:
108+
```powershell
109+
wsl -d Ubuntu -- bash -lc "docker run --rm -t mcp-cpp-build bash -lc 'cd /src && ctest --test-dir build --rerun-failed --output-on-failure'"
110+
```
111+
112+
- Increase verbosity (`-V` or `-VV`) and run in parallel (adjust `-j`):
113+
```powershell
114+
wsl -d Ubuntu -- bash -lc "docker run --rm -t mcp-cpp-build bash -lc 'cd /src && ctest --test-dir build -VV -j 4 --output-on-failure'"
115+
```
116+
117+
- List discovered tests without running:
118+
```powershell
119+
wsl -d Ubuntu -- bash -lc "docker run --rm -t mcp-cpp-build bash -lc 'cd /src && ctest --test-dir build -N'"
120+
```
121+
122+
- Inspect the last CTest log:
123+
```powershell
124+
wsl -d Ubuntu -- bash -lc "docker run --rm -t mcp-cpp-build bash -lc 'cd /src && sed -n \"1,200p\" build/Testing/Temporary/LastTest.log'"
125+
```
126+
127+
- Start an interactive shell inside the build image:
128+
```powershell
129+
wsl -d Ubuntu -- bash -lc "docker run --rm -it mcp-cpp-build bash"
130+
```
131+
132+
> Note: On Linux/macOS, replace the outer `wsl -d Ubuntu -- bash -lc "..."` with the inner command only.
133+
134+
### Specify architecture (e.g., Apple Silicon building x86_64)
135+
136+
- Linux/macOS:
137+
```bash
138+
docker buildx build --platform linux/amd64 -f Dockerfile.demo --target test --progress=plain --pull --load -t mcp-cpp-test .
139+
```
140+
141+
- Windows (WSL):
142+
```powershell
143+
wsl -d Ubuntu -- bash -lc "cd /mnt/c/Work/mcp-cpp && docker buildx build --platform linux/amd64 -f Dockerfile.demo --target test --progress=plain --pull --load -t mcp-cpp-test ."
144+
```
145+
146+
### Pass environment/options to tests
147+
148+
- Build-time transport timeout (already supported in Dockerfile):
149+
```bash
150+
docker buildx build -f Dockerfile.demo --target test --build-arg MCP_STDIOTRANSPORT_TIMEOUT_MS=200 --progress=plain --pull --load -t mcp-cpp-test .
151+
```
152+
153+
- Run-time environment inside tests (logger level, stdio mode, etc.):
154+
```powershell
155+
wsl -d Ubuntu -- bash -lc "docker run --rm -t -e MCP_LOG_LEVEL=INFO -e MCP_STDIO_MODE=1 mcp-cpp-build bash -lc 'cd /src && ctest --test-dir build --output-on-failure'"
156+
```
157+
158+
Common env vars:
159+
- `MCP_LOG_LEVEL` (e.g., `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`)
160+
- `MCP_STDIO_MODE=1` routes Logger to stderr (useful to avoid polluting stdio frames)
161+
- `MCP_STDIOTRANSPORT_TIMEOUT_MS` (build arg) configures request timeout
162+
163+
### Clean up Docker caches (optional)
164+
165+
- Remove dangling build cache:
166+
```bash
167+
docker builder prune -f
168+
```
169+
170+
- Remove dangling images:
171+
```bash
172+
docker image prune -f
173+
```
174+
175+
### Docker Desktop (Windows, no WSL wrapper)
176+
177+
If you use Docker Desktop with Linux containers, you can run commands natively in PowerShell without the WSL shim. Use Windows-style paths and prefer `--mount` for bind mounts.
178+
179+
- Full test run:
180+
```powershell
181+
cd C:\Work\mcp-cpp
182+
docker buildx build -f Dockerfile.demo --target test --progress=plain --pull --load -t mcp-cpp-test .
183+
```
184+
185+
- Build only:
186+
```powershell
187+
docker buildx build -f Dockerfile.demo --target build --progress=plain --pull --load -t mcp-cpp-build .
188+
```
189+
190+
- Run full test suite inside built image:
191+
```powershell
192+
docker run --rm -t mcp-cpp-build bash -lc "cd /src && ctest --test-dir build --output-on-failure"
193+
```
194+
195+
- Regex match a test group:
196+
```powershell
197+
docker run --rm -t mcp-cpp-build bash -lc "cd /src && ctest --test-dir build -R ClientCache --output-on-failure"
198+
```
199+
200+
- Run the demo with a bind mount (note Windows path):
201+
```powershell
202+
docker buildx build -f Dockerfile.demo --target demo --progress=plain --pull --load -t mcp-cpp-demo .
203+
docker run --rm --name mcp-cpp-demo \
204+
-e MCP_STDIOTRANSPORT_TIMEOUT_MS=2000 \
205+
--mount type=bind,src=C:\\Work\\mcp-cpp,dst=/work mcp-cpp-demo
206+
```
207+
208+
Notes:
209+
- If your Windows path contains spaces, wrap it in quotes: `src="C:\\Path With Spaces\\mcp-cpp"`.
210+
- On older Docker Desktop versions, you may use `-v C:\\Work\\mcp-cpp:/work` instead of `--mount`.
211+
- For multi-arch images on Docker Desktop, set `--platform` as shown below.
212+
213+
### Publishing images to a registry (`--push`)
214+
215+
Use `--push` to upload images to a registry (Docker Hub, GHCR, etc.). Do not combine `--push` with `--load`.
216+
217+
1) Log in to your registry (example: Docker Hub or GHCR):
218+
```powershell
219+
docker login # Docker Hub
220+
# or
221+
docker login ghcr.io # GitHub Container Registry
222+
```
223+
224+
2) Choose a tag with your registry/namespace (examples):
225+
- Docker Hub: `yourname/mcp-cpp-test:latest`
226+
- GHCR: `ghcr.io/yourorg/mcp-cpp-test:latest`
227+
228+
3) Build and push a single-arch image:
229+
- Linux/macOS:
230+
```bash
231+
docker buildx build -f Dockerfile.demo --target test --progress=plain --pull \
232+
-t yourname/mcp-cpp-test:latest --push .
233+
```
234+
- Windows (PowerShell / Docker Desktop):
235+
```powershell
236+
docker buildx build -f Dockerfile.demo --target test --progress=plain --pull \
237+
-t yourname/mcp-cpp-test:latest --push .
238+
```
239+
240+
4) Build and push a multi-arch image (recommended for public images):
241+
- Linux/macOS:
242+
```bash
243+
docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile.demo --target test \
244+
--progress=plain --pull -t yourname/mcp-cpp-test:latest --push .
245+
```
246+
- Windows (PowerShell / Docker Desktop):
247+
```powershell
248+
docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile.demo --target test \
249+
--progress=plain --pull -t yourname/mcp-cpp-test:latest --push .
250+
```
251+
252+
5) Optional: ensure a Buildx builder instance is available (only if needed):
253+
```powershell
254+
docker buildx ls
255+
# If no usable builder is available:
256+
docker buildx create --name mcp-builder --use
257+
docker buildx inspect --bootstrap
258+
```
259+
260+
6) Push other targets (build/demo) with your registry tag:
261+
```powershell
262+
# Build stage as a published image
263+
docker buildx build -f Dockerfile.demo --target build --progress=plain --pull \
264+
-t yourname/mcp-cpp-build:latest --push .
265+
266+
# Demo stage as a published image
267+
docker buildx build -f Dockerfile.demo --target demo --progress=plain --pull \
268+
-t yourname/mcp-cpp-demo:latest --push .
269+
```
270+
54271
### Terminal output and colors
55272

56273
- The Docker test stage runs `ctest -VV`, so GoogleTest logs and any shell script output are printed to your terminal.

0 commit comments

Comments
 (0)