@@ -82,6 +82,80 @@ func TestUpdateDirectoryTargetFailsClosedOnUnknownState(t *testing.T) {
8282 }
8383}
8484
85+ // TestUpdateDirectoryTargetRefusesMissingOrNonDirectory pins the #390 ruling:
86+ // update refuses an absolute target that is missing or not a directory as
87+ // invalid_target, the same as install, and writes nothing.
88+ func TestUpdateDirectoryTargetRefusesMissingOrNonDirectory (t * testing.T ) {
89+ for _ , apply := range []bool {false , true } {
90+ for _ , kind := range []string {"missing" , "file" } {
91+ name := kind
92+ if apply {
93+ name += "/apply"
94+ }
95+ t .Run (name , func (t * testing.T ) {
96+ home := canonicalHome (t )
97+ target := filepath .Join (home , "agent-skills" )
98+ if kind == "file" {
99+ if err := os .WriteFile (target , []byte ("user bytes" ), 0o600 ); err != nil {
100+ t .Fatal (err )
101+ }
102+ }
103+ before := homeListing (t , home )
104+ args := []string {"update" , "--target-dir" , target , "--catalog" , "../../catalog" }
105+ if apply {
106+ args = append (args , "--apply" )
107+ }
108+ code , stdout , stderr := runRefusal (t , home , args )
109+ if code != exitUsage || stdout != "" || stderr != invalidTargetRefusal {
110+ t .Fatalf ("exit = %d stdout = %q stderr = %q, want %d \" \" %q" , code , stdout , stderr , exitUsage , invalidTargetRefusal )
111+ }
112+ if after := homeListing (t , home ); after != before {
113+ t .Fatalf ("home changed on refusal: %q, want %q" , after , before )
114+ }
115+ })
116+ }
117+ }
118+ }
119+
120+ // Doctor keeps reporting a missing absolute target instead of refusing it
121+ // (#390): exit 0, presence=absent, and nothing created.
122+ func TestDoctorDirectoryTargetReportsMissingAsAbsent (t * testing.T ) {
123+ home := canonicalHome (t )
124+ target := filepath .Join (home , "agent-skills" )
125+ code , stdout , stderr := runRefusal (t , home , []string {"doctor" , "--target-dir" , target })
126+ if code != exitOK || stderr != "" || stdout != "target=" + target + " presence=absent\n " {
127+ t .Fatalf ("exit = %d stdout = %q stderr = %q" , code , stdout , stderr )
128+ }
129+ if _ , err := os .Lstat (target ); ! os .IsNotExist (err ) {
130+ t .Fatalf ("missing target must not be created: %v" , err )
131+ }
132+ }
133+
134+ // homeListing lists every path and file content under root.
135+ func homeListing (t * testing.T , root string ) string {
136+ t .Helper ()
137+ var entries []string
138+ err := filepath .WalkDir (root , func (path string , entry os.DirEntry , err error ) error {
139+ if err != nil {
140+ return err
141+ }
142+ line := path
143+ if ! entry .IsDir () {
144+ data , err := os .ReadFile (path )
145+ if err != nil {
146+ return err
147+ }
148+ line += "=" + string (data )
149+ }
150+ entries = append (entries , line )
151+ return nil
152+ })
153+ if err != nil {
154+ t .Fatal (err )
155+ }
156+ return strings .Join (entries , "\n " )
157+ }
158+
85159func TestUninstallDirectoryTargetRemovesOnlyOwnedFiles (t * testing.T ) {
86160 home := canonicalHome (t )
87161 target := filepath .Join (home , "agent-skills" )
@@ -149,10 +223,16 @@ func guardedUninstallDependencies(t *testing.T) uninstallDependencies {
149223// with the same diagnostic and exit code, and writes nothing.
150224func TestDirectoryTargetCommandsRefuseRelativeTargets (t * testing.T ) {
151225 const existing = "test-relative-target-372"
226+ shipped , err := filepath .Abs (filepath .Join (".." , ".." , "catalog" ))
227+ if err != nil {
228+ t .Fatal (err )
229+ }
230+ // The relative fixture lives in a temporary working directory, never in
231+ // the package source tree.
232+ t .Chdir (t .TempDir ())
152233 if err := os .MkdirAll (existing , 0o700 ); err != nil {
153234 t .Fatal (err )
154235 }
155- t .Cleanup (func () { _ = os .RemoveAll (existing ) })
156236 if err := os .WriteFile (filepath .Join (existing , "foreign-sentinel.txt" ), []byte ("user bytes" ), 0o600 ); err != nil {
157237 t .Fatal (err )
158238 }
@@ -173,13 +253,17 @@ func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
173253 commands := map [string ][]string {
174254 "install" : nil ,
175255 "doctor" : nil ,
176- "update" : {"--catalog" , "../../catalog" , "--apply" },
256+ "update" : {"--catalog" , shipped , "--apply" },
177257 "uninstall" : nil ,
178258 }
179259 cwd , err := os .Getwd ()
180260 if err != nil {
181261 t .Fatal (err )
182262 }
263+ cwd , err = filepath .EvalSymlinks (cwd )
264+ if err != nil {
265+ t .Fatal (err )
266+ }
183267 // unclean is absolute and names the existing directory, so only the clean
184268 // check can refuse it. trailing-separator is the form shell completion
185269 // produces. empty is what an unset shell variable passes.
@@ -228,11 +312,33 @@ func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
228312 // An explicit empty --runtime is a usage error, so the runtime mode never
229313 // reaches the directory mode through an empty runtime ID.
230314 t .Run ("update/empty-runtime-is-a-usage-error" , func (t * testing.T ) {
231- code , stdout , stderr := runRefusal (t , canonicalHome (t ), []string {"update" , "--runtime" , "" , "--catalog" , "../../catalog" })
315+ code , stdout , stderr := runRefusal (t , canonicalHome (t ), []string {"update" , "--runtime" , "" , "--catalog" , shipped })
232316 if code != exitUsage || stdout != "" || stderr != "error=invalid_command\n " {
233317 t .Fatalf ("exit = %d stdout = %q stderr = %q" , code , stdout , stderr )
234318 }
235319 })
320+ // Contradictory modes are a usage error even when the runtime is empty,
321+ // the same as with a non-empty runtime.
322+ t .Run ("update/empty-runtime-with-target-is-a-usage-error" , func (t * testing.T ) {
323+ code , stdout , stderr := runRefusal (t , canonicalHome (t ), []string {"update" , "--runtime" , "" , "--target-dir" , filepath .Join (cwd , existing ), "--catalog" , shipped })
324+ if code != exitUsage || stdout != "" || stderr != "error=invalid_command\n " {
325+ t .Fatalf ("exit = %d stdout = %q stderr = %q" , code , stdout , stderr )
326+ }
327+ })
328+ // An empty first --target-dir still counts as given, so a second one is a
329+ // duplicate flag rather than a silent override.
330+ for command , extra := range map [string ][]string {"doctor" : nil , "update" : {"--catalog" , shipped }, "uninstall" : nil } {
331+ t .Run (command + "/duplicate-target-after-empty" , func (t * testing.T ) {
332+ args := append ([]string {command , "--target-dir" , "" , "--target-dir" , filepath .Join (cwd , existing )}, extra ... )
333+ code , stdout , stderr := runRefusal (t , canonicalHome (t ), args )
334+ if code != exitUsage || stdout != "" || stderr != "error=invalid_command\n " {
335+ t .Fatalf ("exit = %d stdout = %q stderr = %q" , code , stdout , stderr )
336+ }
337+ if after := listing (existing ); after != before {
338+ t .Fatalf ("target changed: %q, want %q" , after , before )
339+ }
340+ })
341+ }
236342 // Control: the shared check must not over-refuse a valid absolute target.
237343 t .Run ("doctor/accepts-valid-absolute" , func (t * testing.T ) {
238344 code , stdout , stderr := runRefusal (t , canonicalHome (t ), []string {"doctor" , "--target-dir" , filepath .Join (cwd , existing )})
0 commit comments