Skip to content

Commit 56407ba

Browse files
committed
Fix core upgrading not uninstalling unused tools
1 parent 436277f commit 56407ba

File tree

6 files changed

+122
-22
lines changed

6 files changed

+122
-22
lines changed

commands/core/install.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,34 @@ func installPlatform(pm *packagemanager.PackageManager,
9898
for _, tool := range toolsToInstall {
9999
err := commands.InstallToolRelease(pm, tool, taskCB)
100100
if err != nil {
101-
// TODO: handle error
101+
return err
102102
}
103103
}
104104

105-
// Are we installing or upgrading?
106-
platform := platformRelease.Platform
107-
installed := pm.GetInstalledPlatformRelease(platform)
105+
installed := pm.GetInstalledPlatformRelease(platformRelease.Platform)
106+
installedTools := []*cores.ToolRelease{}
108107
if installed == nil {
108+
// No version of this platform is installed
109109
log.Info("Installing platform")
110110
taskCB(&rpc.TaskProgress{Name: "Installing " + platformRelease.String()})
111111
} else {
112-
log.Info("Updating platform " + installed.String())
113-
taskCB(&rpc.TaskProgress{Name: "Updating " + installed.String() + " with " + platformRelease.String()})
112+
// A platform with a different version is already installed
113+
log.Info("Upgrading platform " + installed.String())
114+
taskCB(&rpc.TaskProgress{Name: "Upgrading " + installed.String() + " with " + platformRelease.String()})
115+
platformRef := &packagemanager.PlatformReference{
116+
Package: platformRelease.Platform.Package.Name,
117+
PlatformArchitecture: platformRelease.Platform.Architecture,
118+
PlatformVersion: installed.Version,
119+
}
120+
121+
// Get a list of tools used by the currently installed platform version.
122+
// This must be done so tools used by the currently installed version are
123+
// removed if not used also by the newly installed version.
124+
var err error
125+
_, installedTools, err = pm.FindPlatformReleaseDependencies(platformRef)
126+
if err != nil {
127+
return fmt.Errorf("can't find dependencies for platform %s: %w", platformRef, err)
128+
}
114129
}
115130

116131
// Install
@@ -126,17 +141,25 @@ func installPlatform(pm *packagemanager.PackageManager,
126141

127142
// In case of error try to rollback
128143
if errUn != nil {
129-
log.WithError(errUn).Error("Error updating platform.")
130-
taskCB(&rpc.TaskProgress{Message: "Error updating platform: " + err.Error()})
144+
log.WithError(errUn).Error("Error upgrading platform.")
145+
taskCB(&rpc.TaskProgress{Message: "Error upgrading platform: " + err.Error()})
131146

132147
// Rollback
133148
if err := pm.UninstallPlatform(platformRelease); err != nil {
134149
log.WithError(err).Error("Error rolling-back changes.")
135150
taskCB(&rpc.TaskProgress{Message: "Error rolling-back changes: " + err.Error()})
136151
}
137152

138-
return fmt.Errorf("updating platform: %s", errUn)
153+
return fmt.Errorf("upgrading platform: %s", errUn)
139154
}
155+
156+
// Uninstall unused tools
157+
for _, tool := range installedTools {
158+
if !pm.IsToolRequired(tool) {
159+
uninstallToolRelease(pm, tool, taskCB)
160+
}
161+
}
162+
140163
}
141164

142165
// Perform post install

commands/core/uninstall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq, taskC
6363

6464
for _, tool := range tools {
6565
if !pm.IsToolRequired(tool) {
66-
uninstallToolReleasse(pm, tool, taskCB)
66+
uninstallToolRelease(pm, tool, taskCB)
6767
}
6868
}
6969

@@ -90,7 +90,7 @@ func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease
9090
return nil
9191
}
9292

93-
func uninstallToolReleasse(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB commands.TaskProgressCB) error {
93+
func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB commands.TaskProgressCB) error {
9494
log := pm.Log.WithField("Tool", toolRelease)
9595

9696
log.Info("Uninstalling tool")

commands/core/upgrade.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemana
6464
}
6565

6666
// Search the latest version for all specified platforms
67-
toInstallRefs := []*packagemanager.PlatformReference{}
6867
platform := pm.FindPlatform(platformRef)
6968
if platform == nil {
7069
return fmt.Errorf("platform %s not found", platformRef)
@@ -78,17 +77,15 @@ func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemana
7877
return ErrAlreadyLatest
7978
}
8079
platformRef.PlatformVersion = latest.Version
81-
toInstallRefs = append(toInstallRefs, platformRef)
8280

83-
for _, platformRef := range toInstallRefs {
84-
platform, tools, err := pm.FindPlatformReleaseDependencies(platformRef)
85-
if err != nil {
86-
return fmt.Errorf("platform %s is not installed", platformRef)
87-
}
88-
err = installPlatform(pm, platform, tools, downloadCB, taskCB, skipPostInstall)
89-
if err != nil {
90-
return err
91-
}
81+
platformRelease, tools, err := pm.FindPlatformReleaseDependencies(platformRef)
82+
if err != nil {
83+
return fmt.Errorf("platform %s is not installed", platformRef)
84+
}
85+
err = installPlatform(pm, platformRelease, tools, downloadCB, taskCB, skipPostInstall)
86+
if err != nil {
87+
return err
9288
}
89+
9390
return nil
9491
}

commands/instances.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,17 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
519519
}
520520

521521
ref := &packagemanager.PlatformReference{
522+
Package: installedRelease.Platform.Package.Name,
523+
PlatformArchitecture: installedRelease.Platform.Architecture,
524+
PlatformVersion: installedRelease.Version,
525+
}
526+
// Get list of installed tools needed by the currently installed version
527+
_, installedTools, err := pm.FindPlatformReleaseDependencies(ref)
528+
if err != nil {
529+
return err
530+
}
531+
532+
ref = &packagemanager.PlatformReference{
522533
Package: latest.Platform.Package.Name,
523534
PlatformArchitecture: latest.Platform.Architecture,
524535
PlatformVersion: latest.Version,
@@ -590,6 +601,24 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
590601
}
591602
}
592603

604+
// Uninstall unused tools
605+
for _, toolRelease := range installedTools {
606+
if !pm.IsToolRequired(toolRelease) {
607+
log := pm.Log.WithField("Tool", toolRelease)
608+
609+
log.Info("Uninstalling tool")
610+
taskCB(&rpc.TaskProgress{Name: "Uninstalling " + toolRelease.String() + ", tool is no more required"})
611+
612+
if err := pm.UninstallTool(toolRelease); err != nil {
613+
log.WithError(err).Error("Error uninstalling")
614+
return err
615+
}
616+
617+
log.Info("Tool uninstalled")
618+
taskCB(&rpc.TaskProgress{Message: toolRelease.String() + " uninstalled", Completed: true})
619+
}
620+
}
621+
593622
// Perform post install
594623
if !req.SkipPostInstall {
595624
logrus.Info("Running post_install script")

test/test_core.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,37 @@ def test_core_list_updatable_all_flags(run_command, data_dir):
440440
assert expected_core_id in mapped
441441
assert "Arduino AVR Boards" == mapped[expected_core_id]["Name"]
442442
assert "1.8.3" == mapped[expected_core_id]["Latest"]
443+
444+
445+
def test_core_upgrade_removes_unused_tools(run_command, data_dir):
446+
assert run_command("update")
447+
448+
# Installs a core
449+
assert run_command("core install arduino:avr@1.8.2")
450+
451+
# Verifies expected tool is installed
452+
tool_path = Path(data_dir, "packages", "arduino", "tools", "avr-gcc", "7.3.0-atmel3.6.1-arduino5")
453+
assert tool_path.exists()
454+
455+
# Upgrades core
456+
assert run_command("core upgrade arduino:avr")
457+
458+
# Verifies tool is uninstalled since it's not used by newer core version
459+
assert not tool_path.exists()
460+
461+
462+
def test_core_install_removes_unused_tools(run_command, data_dir):
463+
assert run_command("update")
464+
465+
# Installs a core
466+
assert run_command("core install arduino:avr@1.8.2")
467+
468+
# Verifies expected tool is installed
469+
tool_path = Path(data_dir, "packages", "arduino", "tools", "avr-gcc", "7.3.0-atmel3.6.1-arduino5")
470+
assert tool_path.exists()
471+
472+
# Installs newer version of already installed core
473+
assert run_command("core install arduino:avr@1.8.3")
474+
475+
# Verifies tool is uninstalled since it's not used by newer core version
476+
assert not tool_path.exists()

test/test_upgrade.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@ def test_upgrade_using_library_with_invalid_version(run_command, data_dir):
6565
res = run_command("upgrade")
6666
assert res.ok
6767
assert "WiFi101" in res.stdout
68+
69+
70+
def test_upgrade_unused_core_tools_are_removed(run_command, data_dir):
71+
assert run_command("update")
72+
73+
# Installs a core
74+
assert run_command("core install arduino:avr@1.8.2")
75+
76+
# Verifies expected tool is installed
77+
tool_path = Path(data_dir, "packages", "arduino", "tools", "avr-gcc", "7.3.0-atmel3.6.1-arduino5")
78+
assert tool_path.exists()
79+
80+
# Upgrades everything
81+
assert run_command("upgrade")
82+
83+
# Verifies tool is uninstalled since it's not used by newer core version
84+
assert not tool_path.exists()

0 commit comments

Comments
 (0)