Make coder dotfiles symlinking idempotent (#7525)

Signed-off-by: Spike Curtis <spike@coder.com>
This commit is contained in:
Spike Curtis
2023-05-15 21:00:50 +04:00
committed by GitHub
parent 424f954b91
commit 0b156420de
2 changed files with 15 additions and 0 deletions
+4
View File
@@ -225,6 +225,10 @@ func (r *RootCmd) dotfiles() *clibase.Cmd {
}
}
// attempt to delete the file before creating a new symlink. This overwrites any existing symlinks
// which are typically leftover from a previous call to coder dotfiles. We do this best effort and
// ignore errors because the symlink may or may not exist. Any regular files are backed up above.
_ = os.Remove(to)
err = os.Symlink(from, to)
if err != nil {
return xerrors.Errorf("symlinking %s to %s: %w", from, to, err)
+11
View File
@@ -116,6 +116,17 @@ func TestDotfiles(t *testing.T) {
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc.bak"))
require.NoError(t, err)
require.Equal(t, string(b), "backup")
// check for idempotency
inv, _ = clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
err = inv.Run()
require.NoError(t, err)
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc"))
require.NoError(t, err)
require.Equal(t, string(b), "wow")
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc.bak"))
require.NoError(t, err)
require.Equal(t, string(b), "backup")
})
}