Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions MacUtilGUI.Tests/BrewClientTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ open MacUtilGUI.Services
module BrewClientTests =

type FakeBrew(casks: string list, formulas: string list, ?outdated: string list) =
let mutable casks = casks
let mutable formulas = formulas
let outdated = defaultArg outdated []
let calls = ResizeArray<string list>()

member _.Calls = List.ofSeq calls

member _.SetCasks xs = casks <- xs

member _.SetFormulas xs = formulas <- xs

member _.InstallCalls =
calls
|> Seq.filter (fun args ->
Expand All @@ -20,6 +26,14 @@ module BrewClientTests =
| _ -> false)
|> Seq.toList

member _.UninstallCalls =
calls
|> Seq.filter (fun args ->
match args with
| "uninstall" :: _ -> true
| _ -> false)
|> Seq.toList

member _.UpgradeCalls =
calls
|> Seq.filter (fun args ->
Expand All @@ -37,6 +51,7 @@ module BrewClientTests =
| [ "outdated" ]
| [ "outdated"; "--verbose" ] -> 0, String.concat "\n" outdated, ""
| "install" :: _ -> 0, "", ""
| "uninstall" :: _ -> 0, "", ""
| "upgrade" :: _ -> 0, "", ""
| _ -> 1, "", "unexpected brew args"

Expand Down Expand Up @@ -67,3 +82,23 @@ module BrewClientTests =
| Error msg -> Assert.Fail msg

Assert.Empty(brew.InstallCalls)

[<Fact>]
let UninstallMissingIsOk () =
let brew = FakeBrew([], [])

match BrewClient.uninstall brew.Exec vscode with
| Ok() -> ()
| Error msg -> Assert.Fail msg

Assert.Empty(brew.UninstallCalls)

[<Fact>]
let UninstallUsesCaskToken () =
let brew = FakeBrew([ "visual-studio-code" ], [])

match BrewClient.uninstall brew.Exec vscode with
| Ok() -> ()
| Error msg -> Assert.Fail msg

Assert.Contains([ "uninstall"; "--cask"; "visual-studio-code" ], brew.UninstallCalls)
31 changes: 31 additions & 0 deletions MacUtilGUI.Tests/InstallTabTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ module InstallTabTests =
Assert.Equal("Nothing is selected.", vm.StatusText)
Assert.Empty(vm.SelectedAppIds)

[<Fact>]
let EmptyUninstallWritesNothing () =
let brew = BrewClientTests.FakeBrew([ "visual-studio-code" ], [])
let vm = makeVm brew
uncheckAll vm

vm.UninstallSelected()
Assert.Empty(brew.UninstallCalls)
Assert.Equal("Nothing is selected.", vm.StatusText)
Assert.Empty(vm.SelectedAppIds)

[<Fact>]
let RefreshDetectChecksBox () =
let brew = BrewClientTests.FakeBrew([ "visual-studio-code" ], [ "neovim" ])
let vm = makeVm brew
let vscode = findApp vm.AllApps "visual-studio-code"
let neovim = findApp vm.AllApps "neovim"
let discord = findApp vm.AllApps "discord"

Assert.True(vscode.IsChecked)
Assert.True(neovim.IsChecked)
Assert.False(discord.IsChecked)

brew.SetCasks []
brew.SetFormulas [ "neovim" ]
vm.RefreshInstalled()

Assert.False(vscode.IsChecked)
Assert.True(neovim.IsChecked)
Assert.False(discord.IsChecked)

[<Fact>]
let SearchFiltersList () =
let brew = BrewClientTests.FakeBrew([], [])
Expand Down
23 changes: 23 additions & 0 deletions MacUtilGUI/Services/BrewClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ module BrewClient =
Ok()
| _, _, stderr -> Error(stderr.Trim())

let uninstall (exec: BrewExec) (app: AppEntry) =
if not (installed exec app) then
Ok()
else
match pkg app with
| None -> Error $"App '{app.Id}' has no cask or formula"
| Some token ->
let args =
match token with
| Cask name -> [ "uninstall"; "--cask"; name ]
| Formula name -> [ "uninstall"; "--formula"; name ]

match exec args with
| 0, _, _ -> Ok()
| _, _, stderr when
stderr.IndexOf("not installed", StringComparison.OrdinalIgnoreCase)
>= 0
|| stderr.IndexOf("No such keg", StringComparison.OrdinalIgnoreCase)
>= 0
->
Ok()
| _, _, stderr -> Error(stderr.Trim())

let private brewPath () =
[ "/opt/homebrew/bin/brew"; "/usr/local/bin/brew" ]
|> List.tryFind File.Exists
Expand Down
34 changes: 34 additions & 0 deletions MacUtilGUI/ViewModels/MainWindowViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ type MainWindowViewModel
else
setStatus (String.concat "\n" (List.rev errors))

let uninstallSelected () =
let selected = selectedAppRows ()

if selected.IsEmpty then
setStatus "Nothing is selected."
else
let mutable errors = []

for row in selected do
match BrewClient.uninstall brew row.App with
| Ok() -> ()
| Error msg -> errors <- msg :: errors

refreshAppDetect ()

if errors.IsEmpty then
setStatus (sprintf "Uninstalled %d app(s)." selected.Length)
else
setStatus (String.concat "\n" (List.rev errors))

let refreshInstalled () =
refreshAppDetect ()
setStatus "Refreshed installed apps."

let selectIds (ids: string list) =
let wanted = Set.ofList ids

Expand Down Expand Up @@ -216,6 +240,8 @@ type MainWindowViewModel
let applyCommand = RelayCommand(fun _ -> applySelected ())
let undoCommand = RelayCommand(fun _ -> undoSelected ())
let installCommand = RelayCommand(fun _ -> installSelected ())
let uninstallCommand = RelayCommand(fun _ -> uninstallSelected ())
let refreshInstalledCommand = RelayCommand(fun _ -> refreshInstalled ())
let selectStandardCommand = RelayCommand(fun _ -> selectPreset "Standard")
let selectMinimalCommand = RelayCommand(fun _ -> selectPreset "Minimal")
let brewUpdateCommand = RelayCommand(fun _ -> runMaintenance BrewUpdate)
Expand Down Expand Up @@ -320,6 +346,10 @@ type MainWindowViewModel

member _.InstallSelected() = installSelected ()

member _.UninstallSelected() = uninstallSelected ()

member _.RefreshInstalled() = refreshInstalled ()

member _.RunBrewUpdate() = runMaintenance BrewUpdate

member _.RunBrewCleanup() = runMaintenance BrewCleanup
Expand All @@ -342,6 +372,10 @@ type MainWindowViewModel

member _.InstallCommand = installCommand

member _.UninstallCommand = uninstallCommand

member _.RefreshInstalledCommand = refreshInstalledCommand

member _.SelectStandardCommand = selectStandardCommand

member _.SelectMinimalCommand = selectMinimalCommand
Expand Down
8 changes: 8 additions & 0 deletions MacUtilGUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@
Command="{Binding InstallCommand}"
Classes="macos-accent"
Padding="20,10"/>
<Button Content="Uninstall"
Command="{Binding UninstallCommand}"
Classes="macos-quiet"
Padding="20,10"/>
<Button Content="Refresh"
Command="{Binding RefreshInstalledCommand}"
Classes="macos-quiet"
Padding="20,10"/>
<TextBlock Text="{Binding StatusText}"
Classes="macos-secondary"
VerticalAlignment="Center"/>
Expand Down
Loading