From aaae299f9eb82f9aafcf2fbe9884d0588af728d3 Mon Sep 17 00:00:00 2001 From: Willie Hung Date: Mon, 19 Jan 2026 22:18:08 +0800 Subject: [PATCH] feat: require target path to exist and show sync reminder - Change TargetPath validation to return error when path doesn't exist instead of just a warning - Add sync reminder message after successful target add - Add TestTargetPath unit tests and integration test for non-existent path --- cmd/skillshare/target.go | 1 + internal/validate/validate.go | 18 +++------ internal/validate/validate_test.go | 62 ++++++++++++++++++++++++++++++ tests/integration/target_test.go | 18 +++++++++ 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/cmd/skillshare/target.go b/cmd/skillshare/target.go index fa025223d..3e5ba5400 100644 --- a/cmd/skillshare/target.go +++ b/cmd/skillshare/target.go @@ -96,6 +96,7 @@ func targetAdd(args []string) error { } ui.Success("Added target: %s -> %s", name, path) + ui.Info("Run 'skillshare sync' to sync skills to this target") return nil } diff --git a/internal/validate/validate.go b/internal/validate/validate.go index 69d700c81..d5020e01f 100644 --- a/internal/validate/validate.go +++ b/internal/validate/validate.go @@ -89,10 +89,11 @@ func Path(path string) error { // TargetPath validates a target path for adding as a skill sync target. // Returns warnings (non-fatal) and errors (fatal). +// Errors are returned when: +// - The path does not exist +// - The path is not a directory // Warnings are returned when: -// - The path doesn't end with "skills" -// - The path doesn't exist yet -// - The parent directory doesn't exist +// - The path doesn't end with "skills" or "skill" func TargetPath(path string) (warnings []string, err error) { // Basic path validation if err := Path(path); err != nil { @@ -109,16 +110,9 @@ func TargetPath(path string) (warnings []string, err error) { info, statErr := os.Stat(path) if statErr != nil { if os.IsNotExist(statErr) { - // Path doesn't exist - check parent - parent := filepath.Dir(path) - if _, parentErr := os.Stat(parent); os.IsNotExist(parentErr) { - warnings = append(warnings, fmt.Sprintf("parent directory doesn't exist: %s", parent)) - } else { - warnings = append(warnings, "target directory doesn't exist yet (will be created on sync)") - } - } else { - return nil, fmt.Errorf("cannot access path: %w", statErr) + return nil, fmt.Errorf("path does not exist: %s", path) } + return nil, fmt.Errorf("cannot access path: %w", statErr) } else if !info.IsDir() { return nil, fmt.Errorf("path exists but is not a directory") } diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index f2771e52f..7cb40f16e 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -1,6 +1,8 @@ package validate import ( + "os" + "path/filepath" "strings" "testing" ) @@ -91,3 +93,63 @@ func TestIsLikelySkillsPath(t *testing.T) { }) } } + +func TestTargetPath(t *testing.T) { + // Create temp directory for testing + tempDir := t.TempDir() + existingDir := filepath.Join(tempDir, "skills") + if err := os.MkdirAll(existingDir, 0755); err != nil { + t.Fatalf("failed to create test dir: %v", err) + } + + // Create a file (not a directory) + existingFile := filepath.Join(tempDir, "file.txt") + if err := os.WriteFile(existingFile, []byte("test"), 0644); err != nil { + t.Fatalf("failed to create test file: %v", err) + } + + tests := []struct { + name string + path string + wantErr bool + errContains string + }{ + { + name: "existing directory", + path: existingDir, + wantErr: false, + }, + { + name: "path does not exist", + path: filepath.Join(tempDir, "nonexistent"), + wantErr: true, + errContains: "does not exist", + }, + { + name: "path is file not directory", + path: existingFile, + wantErr: true, + errContains: "not a directory", + }, + { + name: "empty path", + path: "", + wantErr: true, + errContains: "cannot be empty", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := TargetPath(tt.path) + if (err != nil) != tt.wantErr { + t.Errorf("TargetPath(%q) error = %v, wantErr %v", tt.path, err, tt.wantErr) + } + if tt.wantErr && tt.errContains != "" && err != nil { + if !strings.Contains(err.Error(), tt.errContains) { + t.Errorf("TargetPath(%q) error = %v, want error containing %q", tt.path, err, tt.errContains) + } + } + }) + } +} diff --git a/tests/integration/target_test.go b/tests/integration/target_test.go index fb7ab4de2..b29640789 100644 --- a/tests/integration/target_test.go +++ b/tests/integration/target_test.go @@ -24,6 +24,7 @@ targets: {} result.AssertSuccess(t) result.AssertOutputContains(t, "Added target") + result.AssertOutputContains(t, "Run 'skillshare sync'") // Verify config was updated configContent := sb.ReadFile(sb.ConfigPath) @@ -66,6 +67,23 @@ targets: result.AssertAnyOutputContains(t, "already exists") } +func TestTargetAdd_PathNotExists_ReturnsError(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.WriteConfig(`source: ` + sb.SourcePath + ` +targets: {} +`) + + // Path that doesn't exist + nonExistentPath := filepath.Join(sb.Home, ".nonexistent", "skills") + + result := sb.RunCLI("target", "add", "myapp", nonExistentPath) + + result.AssertFailure(t) + result.AssertAnyOutputContains(t, "does not exist") +} + func TestTargetRemove_ExistingTarget_Removes(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup()