Testing the CLI
Relevant source files
The following files were used as context for generating this wiki page:
Testing the vscli tool involves a combination of unit tests, manual dry-run execution, and fixture-based validation to ensure reliable launching of Visual Studio Code projects and dev containers. The project utilizes Rust's built-in testing framework and specific CLI flags to verify core logic without side effects on the user's system. Sources: src/main.rs:1-10, Cargo.toml:1-15
The testing strategy focuses on three primary areas: workspace detection logic, configuration parsing using JSON5, and path manipulation across different environments (Unix, Windows, and WSL). Sources: src/workspace.rs:250-280, src/workspace.rs:160-200
Unit Testing Framework
The project uses the standard Rust test runner. The rust-toolchain.toml specifies a stable channel with clippy and rustfmt components, ensuring that code style and common linting errors are caught during the development and testing cycle. Sources: rust-toolchain.toml:1-5
Workspace and Dev Container Logic
Unit tests are primarily located within the source files they test, specifically in src/workspace.rs. These tests validate:
- Deserialization: Ensuring that
devcontainer.jsonfiles are correctly mapped to internalDevContainerstructs. - Variable Substitution: Verifying that placeholders like
${localWorkspaceFolderBasename}are correctly replaced with actual workspace names. Sources: src/workspace.rs:254-287
The diagram above illustrates the flow of a typical unit test for container configuration. Sources: src/workspace.rs:254-266
Integration Fixtures
The project maintains a tests/fixtures directory containing sample configuration files. These fixtures are used to test the robustness of the json5 parser, which must handle features not present in standard JSON, such as comments and trailing commas. Sources: tests/fixtures/devcontainer.json:1-11, Cargo.toml:23
| Fixture File | Purpose | Key Features Tested |
|---|---|---|
devcontainer.json | Deserialization testing | Comments, trailing commas, and "name" field mapping |
Sources: tests/fixtures/devcontainer.json:1-11, src/workspace.rs:255-256
Dry-Run Mode
For testing the end-to-end command-line interface without actually spawning editor instances, vscli provides a --dry-run (or -d) flag. This flag is propagated through the Setup and Workspace structures to prevent the final execution of system commands. Sources: src/opts.rs:23-25, src/launch.rs:104-106
Dry-Run Execution Flow
When --dry-run is active, the application performs all logic—including history tracking, workspace detection, and URI generation—but intercepts the final call to the system shell. Sources: src/workspace.rs:237-248, src/main.rs:70-75
This sequence shows how the dry-run flag prevents the run function from executing the final command while allowing the rest of the logic to be exercised. Sources: src/workspace.rs:242-248, src/launch.rs:114-140
Environment-Specific Testing
A significant portion of the logic involves path conversion for WSL (Windows Subsystem for Linux). This is tested by simulating the detection of the WSL environment through the presence of WSLENV and uname output. Sources: src/workspace.rs:170-195
- WSL Path Conversion: Uses the
wslpath2crate to convert Linux-style paths to Windows-style paths for the VS Code URI. - Path Canonicalization: The
Workspace::from_pathfunction usesstd::fs::canonicalizeto ensure paths are absolute and valid before testing or usage. Sources: src/workspace.rs:112-118, src/workspace.rs:197-210
History and State Validation
The Tracker and History modules include logic to manage a maximum of 35 entries. Testing here ensures that upsert operations correctly update existing entries (based on path and behavior) rather than creating duplicates, and that timestamps are updated correctly using the chrono crate. Sources: src/history.rs:17-48, src/history.rs:101-115
History Logic Summary
| Component | Responsibility | Relevant Logic |
|---|---|---|
Entry::eq | Equality testing | Compares path and behavior, ignores last_opened |
History::upsert | State management | Finds existing entries to update or inserts new ones |
Tracker::store | Persistence | Limits history to MAX_HISTORY_ENTRIES (35) |
Sources: src/history.rs:32-40, src/history.rs:101-110, src/history.rs:170-185
Conclusion
Testing in vscli is integrated into the core architecture through fixture-based unit tests and a robust dry-run capability. This ensures that the complex logic of dev container detection and path transformation for various environments remains accurate across updates while providing developers a safe way to verify CLI behavior without launching external editors.