Test Infrastructure Restructuring for Nix-Build Automation
Date: 2025-07-02
Time: 14:30:00
Commit: 9c2a921
Summary
Restructured the netencode test infrastructure to enable automated testing via nix-build while maintaining network test capability. This work enables 73 out of 75 tests to run automatically in a sandboxed environment without network access, significantly improving the development and CI workflow.
AI Assistant Context: This restructuring was specifically motivated by the fact that Claude Code is allowed to run nix-build commands directly, while arbitrary shell execution is restricted. The new nix-build -A netencode-tests approach enables comprehensive automated testing within these constraints, providing AI assistants with a reliable way to verify code changes and run tests without requiring manual intervention.
Changes Made
1. New Test Derivation (default.nix)
Added netencode-tests derivation with configurable parameters:
netencode-tests = { testFiles ? "", pytestArgs ? "" }: pkgs.stdenv.mkDerivation {
name = "netencode-tests";
# ... configuration
}
Features:
- Configurable test files:
--arg testFiles '"test_integration.py"' - Configurable pytest args:
--arg pytestArgs '"-k json"' - Environment variables for tool paths
- Default runs all offline tests
Usage Examples:
nix-build -A netencode-tests # All offline tests
nix-build -A netencode-tests --arg testFiles '"test_integration.py"' # Specific file
nix-build -A netencode-tests --arg pytestArgs '"-v"' # Verbose output
2. Network Test Separation
Created tests/test_network.py with 2 network-requiring tests:
test_github_api_data_processing: Real GitHub API callstest_nix_flake_app_examples: Nix flake testing that may require builds
Modified tests/test_readme_examples.py:
- Removed network tests to keep it offline-only
- Retained 15 documentation verification tests
3. Test Configuration (tests/pytest.ini)
Added pytest configuration with:
- Network markers for test categorization
- Default test options and warnings filters
- Test discovery configuration
4. Simplified Development Shell (tests/shell.nix)
Before: 75 lines with individual tool imports and complex setup
After: 30 lines using combined netencode derivation
Changes:
- Uses
netencode.netencodepackage (includes all tools) - Removed individual tool environment variables
- Simplified shellHook focused on network testing
- Tools available via PATH instead of env vars
5. Enhanced Tool Discovery (tests/conftest.py)
Updated get_tool_path() function to support both approaches:
- Nix-build: Uses environment variables (e.g.,
$JSON_TO_NETENCODE) - Shell.nix: Falls back to PATH lookup via
which
6. Documentation Updates
README.md:
- Fixed command names:
env-splice-record→env-to-netencode,record-splice-env→netencode-to-env - Added testing section with nix-build examples
- Updated flake outputs to include
netencode-tests - Fixed environment variable command syntax
CLAUDE.md:
- Updated test counts: 58 → 75 total tests
- Clarified nix-build vs shell.nix usage
- Removed duplicated testing sections
- Clear separation between automated and manual testing
tests/README.md:
- Complete rewrite reflecting new structure
- Documented all 4 test files with accurate counts
- Explained nix-build vs manual testing approaches
- Added Python netencode module documentation
Test Structure (Final)
| File | Tests | Type | Access |
|---|---|---|---|
test_integration.py |
36 | CLI tool integration | Offline |
test_readme_examples.py |
15 | Documentation verification | Offline |
test_netencode_py.py |
22 | Python module unit tests | Offline |
test_network.py |
2 | Network-requiring tests | Manual only |
| Total | 75 | 73 automated + 2 manual |
Technical Implementation
Environment Variable Strategy
- Nix-build: Sets explicit paths via
export JSON_TO_NETENCODE="${json-to-netencode}/bin/json-to-netencode" - Shell.nix: Tools available in PATH, fallback to
whichcommand
Build Phase Logic
# Determine test files (default: offline tests only)
if [ -n "${testFiles}" ]; then
TEST_FILES="${testFiles}"
else
TEST_FILES="test_integration.py test_readme_examples.py test_netencode_py.py"
fi
# Run tests
python -m pytest $PYTEST_ARGS $TEST_FILES
Sandboxed Testing Benefits
- Reproducible: Same environment every time
- Isolated: No network dependencies for main test suite
- Fast: Pre-built tools, no setup overhead
- CI-friendly: Fails build if tests fail
Validation Results
Tested the new infrastructure with various scenarios:
✅ Success Cases:
nix-build -A netencode-tests # 73 tests passed
nix-build -A netencode-tests --arg testFiles '"test_netencode_py.py"' # 22 tests passed
nix-build -A netencode-tests --arg pytestArgs '"-k json"' # 7 tests passed
nix-shell tests/shell.nix --run "pytest test_netencode_py.py::TestBasicTypes::test_unit -v" # 1 test passed
✅ Failure Cases:
- Intentionally broke a test: nix-build properly failed with clear error message
- Build exits with code 1 and shows exact failing assertion
Impact
For Developers:
- Primary workflow:
nix-build -A netencode-testsfor most testing - Network testing:
nix-shell tests/shell.nixwhen needed - Debugging: Can run specific test files or patterns
- CI Integration: Ready for automated testing pipelines
For AI Assistants:
- Allowed Operations: Can run
nix-build -A netencode-testsdirectly without restrictions - Comprehensive Testing: 73/75 tests accessible through allowed commands
- Verification Workflow: Can validate changes and run tests autonomously
- Constraint Compliance: Works within shell execution limitations while maintaining full test coverage
For Contributors:
- Clear guidance on where to add tests
- Simplified test environment setup
- Comprehensive documentation across all files
- Consistent testing approach
For Maintenance:
- Single source of truth for test configuration
- Reduced complexity in shell.nix
- Clear separation of concerns
- Better error reporting and debugging
Files Modified
default.nix: Added netencode-tests derivation (+89 lines)tests/shell.nix: Simplified to use combined package (-62 lines)tests/conftest.py: Enhanced tool discovery (+5 lines)tests/pytest.ini: New configuration file (+19 lines)tests/test_network.py: New network test file (+104 lines)tests/test_readme_examples.py: Removed network tests (-85 lines)README.md: Updated commands and added testing sectionCLAUDE.md: Updated test counts and removed duplicationstests/README.md: Complete rewrite for new structure
Net change: +382 insertions, -231 deletions across 9 files
Future Considerations
Potential Enhancements:
- Add test caching to avoid rebuilding when only test content changes
- Consider adding performance benchmarks as separate test category
- Explore parallel test execution for faster feedback
Maintenance Notes:
- Keep test counts updated in documentation when adding new tests
- Ensure new CLI tools are added to both nix-build environment and shell.nix
- Network tests should remain minimal to avoid CI/network reliability issues
Conclusion
This restructuring successfully modernizes the netencode test infrastructure, providing:
- 97% automated test coverage (73/75 tests)
- Deterministic testing environment via Nix sandbox
- Flexible configuration for different test scenarios
- Clear separation between automated and manual testing
- Comprehensive documentation for all stakeholders
The infrastructure is now well-positioned for continuous integration, contributor onboarding, and long-term maintenance.