Refactor: extract shared mv/cp relocation driver#343
Merged
Conversation
mv and cp had ~115 lines of near-identical body: the same two-phase validate/execute loop, destination resolution, skip and conflict handling, dry-run preview, and JSON accumulation, differing only in a handful of values (command name, verb, success status, missing-args message, and which API call to make). Introduce a relocationSpec describing those differences and a single runRelocation driver in relocation.go. mv and cp become one-line delegations to runRelocation with moveSpec / copySpec. No behavior change: all command-specific strings (including mv's distinct missing-args wording) are preserved via the spec. Add relocation_test.go covering runRelocation over both specs: the distinct missing-args messages, the dry-run/skip/result-build error branches (each asserting the correct verb), and Move-vs-Copy API routing with the correct status. Raises runRelocation coverage from 85% to 97%; the only uncovered block is a pre-existing unreachable validation branch.
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.
Summary
mvandcphad ~115 lines of near-identical body — the same two-phase validate/execute loop, destination resolution, skip and conflict handling, dry-run preview, and JSON accumulation — differing only in a handful of values (command name, verb, success status, missing-args message, and which API call to make).This introduces a
relocationSpecdescribing those differences and a singlerunRelocationdriver incmd/relocation.go.mvandcpbecome one-line delegations:No behavior change. All command-specific strings are preserved via the spec, including mv's distinct
"mv command requires…"vs cp's"cp requires…"missing-args wording.Net: 4 files, +367/−229 (cp.go −116, mv.go −115, relocation.go +168, relocation_test.go +197).
Tests
New
cmd/relocation_test.goexercisesrunRelocationover both specs (table-driven mv/cp):Raises
runRelocationcoverage from 85% → 97%. The only uncovered block is a pre-existing unreachable validation branch (makeRelocationArg—validatePathnever errors), carried over verbatim; a candidate for separate cleanup.