Skip to content

Commit 6b51097

Browse files
Japstyelhimov
authored andcommitted
uninstall: fix uninstalling with if no symlink in bin_dir
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)
1 parent 84b0491 commit 6b51097

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

cli/uninstall/uninstall.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,29 @@ func remove(program string, programVersion string, directory string,
6161
} else if err != nil {
6262
return false, fmt.Errorf("there was some problem locating %s", path)
6363
}
64-
// Get path where symlink point.
65-
resolvedPath, err := util.ResolveSymlink(linkPath)
66-
if err != nil {
67-
return false, fmt.Errorf("failed to resolve symlink %s: %s", linkPath, err)
68-
}
64+
6965
var isSymlinkRemoved bool
70-
// Remove symlink if it points to program.
71-
if strings.Contains(resolvedPath, fileName) {
72-
err = os.Remove(linkPath)
66+
67+
_, err = os.Lstat(linkPath)
68+
if err != nil {
69+
if !os.IsNotExist(err) {
70+
return false, fmt.Errorf("failed to access %q: %s", linkPath, err)
71+
}
72+
isSymlinkRemoved = false
73+
} else {
74+
// Get path where symlink point.
75+
resolvedPath, err := util.ResolveSymlink(linkPath)
7376
if err != nil {
74-
return false, err
77+
return false, fmt.Errorf("failed to resolve symlink %q: %s", linkPath, err)
78+
}
79+
80+
// Remove symlink if it points to program.
81+
if strings.Contains(resolvedPath, fileName) {
82+
if err = os.Remove(linkPath); err != nil {
83+
return false, err
84+
}
85+
isSymlinkRemoved = true
7586
}
76-
isSymlinkRemoved = true
7787
}
7888
err = os.RemoveAll(path)
7989
if err != nil {

test/integration/uninstall/test_uninstall.py

+20
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,23 @@ def test_uninstall_tt_missing_version_character(
348348
assert os.path.isfile(os.path.join(tmp_path, "tt_" +
349349
"v" if "v" not in version_to_uninstall else "" +
350350
version_to_uninstall)) is False
351+
352+
353+
def test_uninstall_tt_missing_symlink(tt_cmd, tmp_path):
354+
configPath = os.path.join(tmp_path, config_name)
355+
# Create test config.
356+
with open(configPath, 'w') as f:
357+
f.write(f'env:\n bin_dir: {tmp_path}\n inc_dir:\n')
358+
359+
shutil.copy(tt_cmd, os.path.join(tmp_path, "tt_94ba971"))
360+
361+
symlink_path = os.path.join(tmp_path, 'tt')
362+
assert not os.path.exists(symlink_path)
363+
364+
uninstall_cmd = [tt_cmd, "uninstall", "tt", "94ba971"]
365+
uninstall_rc, uninstall_output = run_command_and_get_output(uninstall_cmd, cwd=tmp_path)
366+
367+
assert uninstall_rc == 0
368+
assert "tt=94ba971 is uninstalled." in uninstall_output
369+
370+
assert os.path.exists(os.path.join(tmp_path, "tt_" + "94ba971") is False)

test/integration/uninstall/testdata/tt_nosymlink/bin/tt_94ba971

Whitespace-only changes.

0 commit comments

Comments
 (0)