-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatpak_test.go
More file actions
111 lines (97 loc) · 2.97 KB
/
Copy pathflatpak_test.go
File metadata and controls
111 lines (97 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"strings"
"testing"
)
func TestInstallFlatpakPackages(t *testing.T) {
defer resetMocks()
// Case 1: flatpak already installed
hasCmd = func(name string) bool {
return name == "flatpak"
}
var runCmdCalls [][]string
runCmd = func(argv []string, opts CmdOpts) CmdResult {
runCmdCalls = append(runCmdCalls, argv)
return CmdResult{ExitCode: 0}
}
installFlatpakPackages([]string{"org.gimp.GIMP"})
if len(runCmdCalls) != 2 {
t.Fatalf("expected 2 runCmd calls, got %d: %v", len(runCmdCalls), runCmdCalls)
}
// First call should add flathub remote
if runCmdCalls[0][1] != "remote-add" {
t.Errorf("expected remote-add, got %v", runCmdCalls[0])
}
// Second call should install GIMP
if runCmdCalls[1][1] != "install" || runCmdCalls[1][4] != "org.gimp.GIMP" {
t.Errorf("expected install GIMP, got %v", runCmdCalls[1])
}
// Case 2: flatpak not installed, choose not to install
resetMocks()
var askedPrompt string
stdin = strings.NewReader("n\n") // Abort flatpak installation
hasCmd = func(name string) bool {
return false
}
runCmdCalls = nil
installFlatpakPackages([]string{"org.gimp.GIMP"})
if len(runCmdCalls) != 0 {
t.Errorf("expected no flatpak installs if skipped, got calls: %v", runCmdCalls)
}
_ = askedPrompt
// Case 3: flatpak not installed, choose to install
resetMocks()
pkgMgr = "dnf"
stdin = strings.NewReader("y\n")
flatpakInstalled := false
hasCmd = func(name string) bool {
if name == "flatpak" {
return flatpakInstalled
}
return false
}
var runCalls [][]string
runCmd = func(argv []string, opts CmdOpts) CmdResult {
runCalls = append(runCalls, argv)
if len(argv) >= 4 && argv[0] == "dnf" && argv[1] == "install" && argv[3] == "flatpak" {
flatpakInstalled = true
}
return CmdResult{ExitCode: 0}
}
installFlatpakPackages([]string{"org.gimp.GIMP"})
// It should call dnf install flatpak, then remote-add, then install GIMP
foundInstall := false
for _, call := range runCalls {
if len(call) >= 4 && call[0] == "dnf" && call[1] == "install" && call[3] == "flatpak" {
foundInstall = true
}
}
if !foundInstall {
t.Errorf("expected dnf install flatpak to be called, got calls: %v", runCalls)
}
}
func TestInstallFlatpakFailures(t *testing.T) {
defer resetMocks()
stdin = strings.NewReader("y\n")
hasCmd = func(name string) bool { return false }
runCmd = func(argv []string, opts CmdOpts) CmdResult {
return CmdResult{ExitCode: 1}
}
installFlatpakPackages([]string{"org.gimp.GIMP"})
resetMocks()
stdin = strings.NewReader("y\n")
hasCmd = func(name string) bool { return false }
runCmd = func(argv []string, opts CmdOpts) CmdResult {
return CmdResult{ExitCode: 0}
}
installFlatpakPackages([]string{"org.gimp.GIMP"})
resetMocks()
hasCmd = func(name string) bool { return name == "flatpak" }
runCmd = func(argv []string, opts CmdOpts) CmdResult {
if argv[1] == "install" {
return CmdResult{ExitCode: 1}
}
return CmdResult{ExitCode: 0}
}
installFlatpakPackages([]string{"org.gimp.GIMP"})
}