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
This commit is contained in:
Willie Hung
2026-01-19 22:18:08 +08:00
parent 5e15e6bc8a
commit aaae299f9e
4 changed files with 87 additions and 12 deletions
+1
View File
@@ -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
}
+6 -12
View File
@@ -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")
}
+62
View File
@@ -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)
}
}
})
}
}
+18
View File
@@ -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()