-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add symlink hook #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,21 @@ func TestExecutePostCreateHooks_NoHooks(t *testing.T) { | |
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func requireSymlinkSupport(t *testing.T) { | ||
| t.Helper() | ||
| if runtime.GOOS != "windows" { | ||
| return | ||
| } | ||
|
|
||
| tempDir := t.TempDir() | ||
| srcPath := filepath.Join(tempDir, "src") | ||
| dstPath := filepath.Join(tempDir, "dst") | ||
| require.NoError(t, os.WriteFile(srcPath, []byte("ok"), 0644)) | ||
| if err := os.Symlink(srcPath, dstPath); err != nil { | ||
| t.Skipf("symlink not supported on this system: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestExecutePostCreateHooks_InvalidHookType(t *testing.T) { | ||
| cfg := &config.Config{ | ||
| Hooks: config.Hooks{ | ||
|
|
@@ -100,6 +115,58 @@ func TestExecutePostCreateHooks_CopyFile(t *testing.T) { | |
| assert.Contains(t, output, "✓ Hook 1 completed") | ||
| } | ||
|
|
||
| func TestExecutePostCreateHooks_Symlink(t *testing.T) { | ||
| requireSymlinkSupport(t) | ||
|
|
||
| tempDir := t.TempDir() | ||
| repoRoot := filepath.Join(tempDir, "repo") | ||
| worktreeDir := filepath.Join(tempDir, "worktree") | ||
|
|
||
| err := os.MkdirAll(repoRoot, directoryPermissions) | ||
| require.NoError(t, err) | ||
| err = os.MkdirAll(worktreeDir, directoryPermissions) | ||
| require.NoError(t, err) | ||
|
|
||
| srcDir := filepath.Join(repoRoot, ".bin") | ||
| require.NoError(t, os.MkdirAll(srcDir, directoryPermissions)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(srcDir, "tool"), []byte("bin"), 0644)) | ||
|
|
||
| cfg := &config.Config{ | ||
| Hooks: config.Hooks{ | ||
| PostCreate: []config.Hook{ | ||
| { | ||
| Type: config.HookTypeSymlink, | ||
| From: ".bin", | ||
| To: ".bin", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| executor := NewExecutor(cfg, repoRoot) | ||
| var buf bytes.Buffer | ||
| err = executor.ExecutePostCreateHooks(&buf, worktreeDir) | ||
| assert.NoError(t, err) | ||
|
|
||
| dstPath := filepath.Join(worktreeDir, ".bin") | ||
| info, err := os.Lstat(dstPath) | ||
| require.NoError(t, err) | ||
| assert.NotZero(t, info.Mode()&os.ModeSymlink) | ||
|
|
||
| linkTarget, err := os.Readlink(dstPath) | ||
| require.NoError(t, err) | ||
|
|
||
| resolvedTarget := linkTarget | ||
| if !filepath.IsAbs(resolvedTarget) { | ||
| resolvedTarget = filepath.Join(filepath.Dir(dstPath), resolvedTarget) | ||
| } | ||
| assert.Equal(t, filepath.Clean(srcDir), filepath.Clean(resolvedTarget)) | ||
|
|
||
| output := buf.String() | ||
| assert.Contains(t, output, "Symlinking: .bin → .bin") | ||
| assert.Contains(t, output, "✓ Hook 1 completed") | ||
| } | ||
|
Comment on lines
+118
to
+165
|
||
|
|
||
| func TestExecutePostCreateHooks_Command(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| t.Skip("Skipping command test on Windows") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The symlink implementation creates relative symlinks when possible, which differs from the copy hook behavior that always uses absolute paths. This inconsistency could lead to unexpected behavior. Consider whether symlinks should always be absolute (like copy operations) for consistency, or document this difference clearly. Relative symlinks can break if the symlink is moved, while absolute symlinks remain valid but are less portable.