Skip to content

Commit

Permalink
uninstall: fix uninstalling with if no symlink in bin_dir
Browse files Browse the repository at this point in the history
tt uninstall does not work if there is no symbolic link in bin_dir,
so a check for the presence of a symbolic link has been added.

Closes: [900](#900)
  • Loading branch information
Japsty committed Nov 14, 2024
1 parent f87a566 commit 335fe45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
30 changes: 20 additions & 10 deletions cli/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,29 @@ func remove(program string, programVersion string, directory string,
} else if err != nil {
return false, fmt.Errorf("there was some problem locating %s", path)
}
// Get path where symlink point.
resolvedPath, err := util.ResolveSymlink(linkPath)
if err != nil {
return false, fmt.Errorf("failed to resolve symlink %s: %s", linkPath, err)
}

var isSymlinkRemoved bool
// Remove symlink if it points to program.
if strings.Contains(resolvedPath, fileName) {
err = os.Remove(linkPath)

_, err = os.Lstat(linkPath)
if err != nil {
if !os.IsNotExist(err) {
return false, fmt.Errorf("failed to access %q: %s", linkPath, err)
}
isSymlinkRemoved = false
} else {
// Get path where symlink point.
resolvedPath, err := util.ResolveSymlink(linkPath)
if err != nil {
return false, err
return false, fmt.Errorf("failed to resolve symlink %q: %s", linkPath, err)
}

// Remove symlink if it points to program.
if strings.Contains(resolvedPath, fileName) {
if err = os.Remove(linkPath); err != nil {
return false, err
}
isSymlinkRemoved = true
}
isSymlinkRemoved = true
}
err = os.RemoveAll(path)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions test/integration/uninstall/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,23 @@ def test_uninstall_tt_missing_version_character(
assert os.path.isfile(os.path.join(tmp_path, "tt_" +
"v" if "v" not in version_to_uninstall else "" +
version_to_uninstall)) is False


def test_uninstall_tt_missing_symlink(tt_cmd, tmp_path):
configPath = os.path.join(tmp_path, config_name)
# Create test config.
with open(configPath, 'w') as f:
f.write(f'env:\n bin_dir: {tmp_path}\n inc_dir:\n')

shutil.copy(tt_cmd, os.path.join(tmp_path, "tt_94ba971"))

symlink_path = os.path.join(tmp_path, 'tt')
assert not os.path.exists(symlink_path)

uninstall_cmd = [tt_cmd, "uninstall", "tt", "94ba971"]
uninstall_rc, uninstall_output = run_command_and_get_output(uninstall_cmd, cwd=tmp_path)

assert uninstall_rc == 0
assert "tt=94ba971 is uninstalled." in uninstall_output

assert os.path.exists(os.path.join(tmp_path, "tt_" + "94ba971") is False)
Empty file.

0 comments on commit 335fe45

Please sign in to comment.