Skip to content

Commit ff42157

Browse files
committed
fix(cli): validate an empty --target-dir instead of falling back
doctor and uninstall treated `--target-dir ""` as an absent flag: doctor ran the host runtime report, and uninstall ran the canonical all-roots uninstall, so `cortex uninstall --target-dir "$TARGET"` with an unset TARGET removed every Cortex-owned skill and exited 0. update refused the same input as invalid_command. All three now track whether the flag was given and refuse an empty value with error=invalid_target, exit 64, like install. The refusal test adds the empty value, Windows path shapes (drive-relative, rooted without a drive, mixed separators), install's --target-dir= form, target-before-catalog precedence for update, and an unchanged home. Its uninstall dependencies fail the test if the canonical roots are ever resolved, so it cannot touch a real home. docs/operating.md now states exactly which inputs every command refuses and which answers stay command-specific. Refs #372
1 parent 1227271 commit ff42157

4 files changed

Lines changed: 85 additions & 27 deletions

File tree

‎docs/operating.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ runtime=directory presence=present compatibility=compatible action=configure tou
7979
```
8080

8181
- `error=invalid_target` (exit 64) — the path is missing, relative, or not a directory.
82-
Doctor, update, and uninstall refuse a relative or unclean `--target-dir` the same way,
83-
before reading or writing the directory.
82+
Doctor, update, and uninstall refuse an empty, relative, or unclean `--target-dir` with the
83+
same error before reading or writing anything; an empty value never falls back to the
84+
runtime mode. A missing absolute target keeps each command's own answer (doctor reports
85+
`presence=absent`, update refuses with `reason=target_absent`), and uninstall also
86+
refuses a filesystem root.
8487
- Update and uninstall work on the recorded ownership:
8588
`cortex update --target-dir <dir> --catalog ./catalog [--apply]` (a target with no prior
8689
state is refused with `reason=not_installed` instead of becoming a first install), and

‎internal/cli/directory_lifecycle_test.go‎

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package cli
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"os"
78
"path/filepath"
9+
"runtime"
810
"strings"
911
"testing"
12+
13+
"github.com/refactor-ia/cortex/internal/skillroot"
1014
)
1115

1216
// directoryLifecycleInstall installs the shipped catalog into a target
@@ -127,6 +131,19 @@ func TestUninstallDirectoryTargetRemovesOnlyOwnedFiles(t *testing.T) {
127131
}
128132
}
129133

134+
// guardedUninstallDependencies fails the test if a command falls back to the
135+
// canonical all-roots uninstall, so a refusal test can never reach the real
136+
// user home even when the refusal is missing.
137+
func guardedUninstallDependencies(t *testing.T) uninstallDependencies {
138+
t.Helper()
139+
deps := defaultUninstallDependencies()
140+
deps.resolveRoots = func() ([]skillroot.UninstallRoot, error) {
141+
t.Error("canonical uninstall roots were resolved")
142+
return nil, errors.New("canonical uninstall roots must not be resolved")
143+
}
144+
return deps
145+
}
146+
130147
// TestDirectoryTargetCommandsRefuseRelativeTargets pins issue #372: every
131148
// command that consumes --target-dir refuses a path install would refuse,
132149
// with the same diagnostic and exit code, and writes nothing.
@@ -139,18 +156,18 @@ func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
139156
if err := os.WriteFile(filepath.Join(existing, "foreign-sentinel.txt"), []byte("user bytes"), 0o600); err != nil {
140157
t.Fatal(err)
141158
}
142-
listing := func() []string {
159+
listing := func(root string) string {
143160
var names []string
144-
err := filepath.WalkDir(existing, func(path string, _ os.DirEntry, err error) error {
161+
err := filepath.WalkDir(root, func(path string, _ os.DirEntry, err error) error {
145162
names = append(names, path)
146163
return err
147164
})
148165
if err != nil {
149166
t.Fatal(err)
150167
}
151-
return names
168+
return strings.Join(names, "\n")
152169
}
153-
before := listing()
170+
before := listing(existing)
154171
// Extra arguments make each command reach its target check: update gets a
155172
// valid catalog and --apply so a refusal cannot come from the catalog.
156173
commands := map[string][]string{
@@ -164,26 +181,56 @@ func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
164181
t.Fatal(err)
165182
}
166183
// unclean is absolute and names the existing directory, so only the clean
167-
// check can refuse it.
184+
// check can refuse it. empty is what an unset shell variable passes.
168185
unclean := cwd + string(filepath.Separator) + "." + string(filepath.Separator) + existing
169-
targets := map[string]string{"existing": existing, "missing": "relative/missing-target", "unclean": unclean}
186+
targets := map[string]string{"existing": existing, "missing": "relative/missing-target", "unclean": unclean, "empty": ""}
187+
if runtime.GOOS == "windows" {
188+
volume := filepath.VolumeName(cwd)
189+
targets["drive-relative"] = volume + existing
190+
targets["rooted-without-drive"] = strings.TrimPrefix(cwd, volume) + `\` + existing
191+
targets["mixed-separators"] = cwd + "/" + existing
192+
}
170193
for command, extra := range commands {
171194
for name, target := range targets {
172195
t.Run(command+"/"+name, func(t *testing.T) {
173196
home := canonicalHome(t)
174-
var stdout, stderr bytes.Buffer
175197
args := append([]string{command, "--target-dir", target}, extra...)
176-
code := runWithDependencies(context.Background(), args, &stdout, &stderr, nil, directoryInstallDependencies(t, home), defaultUninstallDependencies())
177-
if code != exitUsage || stdout.Len() != 0 || stderr.String() != "error=invalid_target\n" {
178-
t.Fatalf("exit = %d stdout = %q stderr = %q, want %d \"\" %q", code, stdout.String(), stderr.String(), exitUsage, "error=invalid_target\n")
198+
code, stdout, stderr := runRefusal(t, home, args)
199+
if code != exitUsage || stdout != "" || stderr != invalidTargetRefusal {
200+
t.Fatalf("exit = %d stdout = %q stderr = %q, want %d \"\" %q", code, stdout, stderr, exitUsage, invalidTargetRefusal)
179201
}
180-
if after := listing(); strings.Join(after, "\n") != strings.Join(before, "\n") {
202+
if after := listing(existing); after != before {
181203
t.Fatalf("relative target changed: %q, want %q", after, before)
182204
}
205+
if after := listing(home); after != home {
206+
t.Fatalf("home changed on refusal: %q", after)
207+
}
183208
if _, err := os.Stat("relative"); !os.IsNotExist(err) {
184209
t.Fatalf("missing relative target was created: %v", err)
185210
}
186211
})
187212
}
188213
}
214+
t.Run("install/equals-form", func(t *testing.T) {
215+
code, stdout, stderr := runRefusal(t, canonicalHome(t), []string{"install", "--target-dir=" + existing})
216+
if code != exitUsage || stdout != "" || stderr != invalidTargetRefusal {
217+
t.Fatalf("exit = %d stdout = %q stderr = %q", code, stdout, stderr)
218+
}
219+
})
220+
t.Run("update/target-checked-before-catalog", func(t *testing.T) {
221+
code, stdout, stderr := runRefusal(t, canonicalHome(t), []string{"update", "--target-dir", existing, "--catalog", "missing-catalog", "--apply"})
222+
if code != exitUsage || stdout != "" || stderr != invalidTargetRefusal {
223+
t.Fatalf("exit = %d stdout = %q stderr = %q", code, stdout, stderr)
224+
}
225+
})
226+
}
227+
228+
const invalidTargetRefusal = "error=invalid_target\n"
229+
230+
// runRefusal runs one CLI invocation with guarded uninstall dependencies.
231+
func runRefusal(t *testing.T, home string, args []string) (int, string, string) {
232+
t.Helper()
233+
var stdout, stderr bytes.Buffer
234+
code := runWithDependencies(context.Background(), args, &stdout, &stderr, nil, directoryInstallDependencies(t, home), guardedUninstallDependencies(t))
235+
return code, stdout.String(), stderr.String()
189236
}

‎internal/cli/doctor.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,24 @@ func runWithDependencies(ctx context.Context, args []string, stdout, stderr io.W
9797
// directory has no host to probe, and its presence, writability, and prior
9898
// ownership are the whole answer to whether an install into it can proceed.
9999
func runDoctorCommand(ctx context.Context, args []string, stdout, stderr io.Writer, runner runtimeprobe.Runner, policy runtimecompat.Policy, resolveRoots func() ([]skillroot.UninstallRoot, error)) int {
100-
targetDir := ""
100+
targetDir, targetSet := "", false
101101
for index := 0; index < len(args); index++ {
102102
switch args[index] {
103103
case "--target-dir":
104-
if targetDir != "" || index+1 >= len(args) {
104+
if targetSet || index+1 >= len(args) {
105105
writeError(stderr, "invalid_command")
106106
return exitUsage
107107
}
108-
targetDir = args[index+1]
108+
targetDir, targetSet = args[index+1], true
109109
index++
110110
default:
111111
writeError(stderr, "invalid_command")
112112
return exitUsage
113113
}
114114
}
115-
if targetDir == "" {
115+
// A given --target-dir is validated even when empty: an unset shell
116+
// variable must not silently turn into the host runtime report.
117+
if !targetSet {
116118
return runDoctor(ctx, stdout, stderr, runner, policy, resolveRoots)
117119
}
118120
if !validTargetDir(targetDir) {
@@ -571,22 +573,24 @@ func defaultUninstallDependencies() uninstallDependencies {
571573
// scan: exactly the directory the caller installed into is inspected, and
572574
// only the files its ownership records name are removed.
573575
func runUninstallCommand(args []string, stdout, stderr io.Writer, deps uninstallDependencies) int {
574-
targetDir := ""
576+
targetDir, targetSet := "", false
575577
for index := 0; index < len(args); index++ {
576578
switch args[index] {
577579
case "--target-dir":
578-
if targetDir != "" || index+1 >= len(args) {
580+
if targetSet || index+1 >= len(args) {
579581
writeError(stderr, "invalid_command")
580582
return exitUsage
581583
}
582-
targetDir = args[index+1]
584+
targetDir, targetSet = args[index+1], true
583585
index++
584586
default:
585587
writeError(stderr, "invalid_command")
586588
return exitUsage
587589
}
588590
}
589-
if targetDir == "" {
591+
// A given --target-dir is validated even when empty: an unset shell
592+
// variable must never fall through to the all-roots uninstall.
593+
if !targetSet {
590594
return runUninstall(stdout, stderr, deps)
591595
}
592596
return runDirectoryUninstall(targetDir, stdout, stderr, deps)

‎internal/cli/update.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ func runWithUpdateDependencies(ctx context.Context, args []string, stdout, stder
7777
writeError(stderr, "invalid_command")
7878
return exitUsage
7979
}
80-
if targetDir != "" {
80+
// The runtime mode always carries a runtime ID, so its absence means the
81+
// directory mode, including an empty --target-dir value.
82+
if runtimeID == "" {
8183
return runDirectoryUpdate(stdout, stderr, targetDir, catalogDir, apply)
8284
}
8385
reports, err := probe(ctx, runner)
@@ -138,21 +140,21 @@ func runWithUpdateDependencies(ctx context.Context, args []string, stdout, stder
138140
// default runtime or target. The two selection modes are mutually exclusive.
139141
func parseUpdateArgs(args []string) (runtimematrix.RuntimeID, string, bool, bool, string, bool) {
140142
var runtimeID, catalogDir, targetDir string
141-
apply, adoptGuidance := false, false
143+
apply, adoptGuidance, targetSet := false, false, false
142144
for len(args) > 0 {
143145
flag := args[0]
144146
args = args[1:]
145147
switch flag {
146148
case "--runtime":
147-
if runtimeID != "" || targetDir != "" || len(args) == 0 {
149+
if runtimeID != "" || targetSet || len(args) == 0 {
148150
return "", "", false, false, "", false
149151
}
150152
runtimeID, args = args[0], args[1:]
151153
case "--target-dir":
152-
if targetDir != "" || runtimeID != "" || len(args) == 0 {
154+
if targetSet || runtimeID != "" || len(args) == 0 {
153155
return "", "", false, false, "", false
154156
}
155-
targetDir, args = args[0], args[1:]
157+
targetDir, args, targetSet = args[0], args[1:], true
156158
case "--catalog":
157159
if catalogDir != "" || len(args) == 0 {
158160
return "", "", false, false, "", false
@@ -172,7 +174,9 @@ func parseUpdateArgs(args []string) (runtimematrix.RuntimeID, string, bool, bool
172174
return "", "", false, false, "", false
173175
}
174176
}
175-
if targetDir != "" {
177+
// A given --target-dir selects the directory mode even when its value is
178+
// empty, so the target check refuses it rather than the runtime mode.
179+
if targetSet {
176180
if catalogDir == "" || adoptGuidance {
177181
return "", "", false, false, "", false
178182
}

0 commit comments

Comments
 (0)