feat(detector): implement NVM detection with mocked tests#2
Merged
Conversation
NVM is a shell function, not a binary, so the implementation needs a
hybrid strategy per nodeup.md §5:
- Detect : check $NVM_DIR or ~/.nvm/nvm.sh existence (cheap,
no subprocess)
- ListInstalled: read ~/.nvm/versions/node/* directory entries
directly (Strategy C, no subprocess)
- Version : source nvm.sh via platform.RunShellScript and run
nvm --version (Strategy A)
Mutation methods (Install, Uninstall, Use, SetDefault, GlobalNpmPrefix)
return a new ErrNVMNotImplemented sentinel, matching the FNM pattern.
Tests cover 23 cases on two package-level seams:
- var listDir = os.ReadDir (filesystem)
- var runScript = platform.RunShellScript (shell)
plus a withStubNVMScript helper that writes a real nvm.sh file to a
temp dir for Detect() tests, which avoids the complexity of stubbing
os.Stat.
Live probe against a real ~/.nvm install (nvm 0.40.5) confirms:
- Detect: true
- Version: 0.40.5
- Installed: [16.19.0 18.14.0 19.6.0] (sorted asc)
3c24280 to
043e100
Compare
|
✔️ 043e100 - Conventional commits check succeeded. |
This was referenced Jun 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(detector): implement NVM detection with mocked tests
Implements manager #2 of 8 for Phase 1 (detection engine). Closes the
NVM stub that has been living in
internal/detector/nvm.gosincescaffolding.
What this PR does
Replaces the zero-value stub methods on
NVMwith realDetect/Version/ListInstalledimplementations, and adds 23unit tests covering each one.
Why NVM is special
NVM is a shell function, not a binary. It only exists after
source ~/.nvm/nvm.sh. There is nonvmexecutable on PATH. Thisbreaks the simple
which fnm && fnm --versionpattern that worksfor every other manager.
Per
nodeup.md§5 ("The nvm Special Case"), three strategies exist.This PR uses a hybrid:
Detect()~/.nvm/nvm.sh(or$NVM_DIR/nvm.sh)ListInstalled()<nvmDir>/versions/node/*directory entries (Strategy C)Version()source nvm.sh && nvm --versionviaplatform.RunShellScript(Strategy A)The plan itself recommends Strategy C for reads ("most reliable for
listing/detecting versions") and Strategy A only for mutating
operations. We follow that.
Test seams
Two package-level vars, swappable per-test, mirroring the FNM
pattern from PR #1:
Plus a
withStubNVMScripthelper that writes a real (empty)nvm.shfile to a temp dir for
Detect()tests, sidestepping the complexityof stubbing
os.Stat.23 tests cover:
parseNVMVersion× 5 (bare, prefixed, whitespace, empty, whitespace-only)parseNVMInstalledEntries× 7 (standard layout, bare versions,skips
system, skips non-dirs, skips unparseable, empty, only-system)NVM.Name,NVM.Detect(2 paths),NVM.Version(3 paths),NVM.ListInstalled(4 paths), mutation stubs,GlobalNpmPrefixLive verification
Probe (one-off dev binary, since deleted) against a real
~/.nvminstall:The actual
~/.nvm/versions/node/containsv16.19.0,v18.14.0,v19.6.0. Output matches.Out of scope (intentional)
Mutation methods (
Install,Uninstall,Use,SetDefault,GlobalNpmPrefix) returnErrNVMNotImplemented, same as FNM. Theywill be filled in when the upgrade engine (Phase 2) needs them.
Test plan
go vet ./internal/detector/...— cleango test -race ./internal/detector/...— 23 NVM tests pass,full package (FNM + registry + NVM) green
go build ./...— repo-wide build cleango test -race ./...— all packages green~/.nvm