service_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package upgrade
  2. import (
  3. "context"
  4. "io"
  5. "strings"
  6. "testing"
  7. "github.com/Jguer/aur"
  8. "github.com/Jguer/go-alpm/v2"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/Jguer/yay/v11/pkg/db"
  11. "github.com/Jguer/yay/v11/pkg/db/mock"
  12. "github.com/Jguer/yay/v11/pkg/dep"
  13. "github.com/Jguer/yay/v11/pkg/settings"
  14. "github.com/Jguer/yay/v11/pkg/settings/parser"
  15. "github.com/Jguer/yay/v11/pkg/text"
  16. "github.com/Jguer/yay/v11/pkg/topo"
  17. "github.com/Jguer/yay/v11/pkg/vcs"
  18. mockaur "github.com/Jguer/yay/v11/pkg/dep/mock"
  19. )
  20. func ptrString(s string) *string {
  21. return &s
  22. }
  23. func TestUpgradeService_GraphUpgrades(t *testing.T) {
  24. linuxDepInfo := &dep.InstallInfo{
  25. Reason: dep.Explicit,
  26. Source: dep.Sync,
  27. AURBase: nil,
  28. LocalVersion: "4.5.0-1",
  29. Version: "5.0.0-1",
  30. SyncDBName: ptrString("core"),
  31. Upgrade: true,
  32. Devel: false,
  33. }
  34. exampleDepInfoDevel := &dep.InstallInfo{
  35. Source: dep.AUR,
  36. Reason: dep.Dep,
  37. AURBase: ptrString("example"),
  38. LocalVersion: "2.2.1.r32.41baa362-1",
  39. Version: "latest-commit",
  40. Upgrade: true,
  41. Devel: true,
  42. }
  43. exampleDepInfoAUR := &dep.InstallInfo{
  44. Source: dep.AUR,
  45. Reason: dep.Dep,
  46. AURBase: ptrString("example"),
  47. LocalVersion: "2.2.1.r32.41baa362-1",
  48. Version: "2.2.1.r69.g8a10460-1",
  49. Upgrade: true,
  50. Devel: false,
  51. }
  52. yayDepInfo := &dep.InstallInfo{
  53. Reason: dep.Explicit,
  54. Source: dep.AUR,
  55. AURBase: ptrString("yay"),
  56. LocalVersion: "10.2.3",
  57. Version: "10.2.4",
  58. Upgrade: true,
  59. Devel: false,
  60. }
  61. dbExe := &mock.DBExecutor{
  62. InstalledRemotePackageNamesFn: func() []string {
  63. return []string{"yay", "example-git"}
  64. },
  65. InstalledRemotePackagesFn: func() map[string]mock.IPackage {
  66. mapRemote := make(map[string]mock.IPackage)
  67. mapRemote["yay"] = &mock.Package{
  68. PName: "yay",
  69. PBase: "yay",
  70. PVersion: "10.2.3",
  71. PReason: alpm.PkgReasonExplicit,
  72. }
  73. mapRemote["example-git"] = &mock.Package{
  74. PName: "example-git",
  75. PBase: "example",
  76. PVersion: "2.2.1.r32.41baa362-1",
  77. PReason: alpm.PkgReasonDepend,
  78. }
  79. return mapRemote
  80. },
  81. SyncUpgradesFn: func(bool) (map[string]db.SyncUpgrade, error) {
  82. mapUpgrades := make(map[string]db.SyncUpgrade)
  83. coreDB := mock.NewDB("core")
  84. mapUpgrades["linux"] = db.SyncUpgrade{
  85. Package: &mock.Package{
  86. PName: "linux",
  87. PVersion: "5.0.0-1",
  88. PReason: alpm.PkgReasonDepend,
  89. PDB: coreDB,
  90. },
  91. LocalVersion: "4.5.0-1",
  92. Reason: alpm.PkgReasonExplicit,
  93. }
  94. return mapUpgrades, nil
  95. },
  96. ReposFn: func() []string { return []string{"core"} },
  97. }
  98. vcsStore := &vcs.Mock{
  99. ToUpgradeReturn: []string{"example-git"},
  100. }
  101. mockAUR := &mockaur.MockAUR{
  102. GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
  103. return []aur.Pkg{
  104. {Name: "yay", Version: "10.2.4", PackageBase: "yay"},
  105. {Name: "example-git", Version: "2.2.1.r69.g8a10460-1", PackageBase: "example"},
  106. }, nil
  107. },
  108. }
  109. type fields struct {
  110. input io.Reader
  111. output io.Writer
  112. noConfirm bool
  113. devel bool
  114. }
  115. type args struct {
  116. graph *topo.Graph[string, *dep.InstallInfo]
  117. enableDowngrade bool
  118. }
  119. tests := []struct {
  120. name string
  121. fields fields
  122. args args
  123. mustExist map[string]*dep.InstallInfo
  124. mustNotExist map[string]bool
  125. wantErr bool
  126. }{
  127. {
  128. name: "no input",
  129. fields: fields{
  130. input: strings.NewReader("\n"),
  131. output: io.Discard,
  132. noConfirm: false,
  133. },
  134. args: args{
  135. graph: nil,
  136. enableDowngrade: false,
  137. },
  138. mustExist: map[string]*dep.InstallInfo{
  139. "yay": yayDepInfo,
  140. "linux": linuxDepInfo,
  141. "example-git": exampleDepInfoAUR,
  142. },
  143. mustNotExist: map[string]bool{},
  144. wantErr: false,
  145. },
  146. {
  147. name: "no input devel",
  148. fields: fields{
  149. input: strings.NewReader("\n"),
  150. output: io.Discard,
  151. noConfirm: false,
  152. devel: true,
  153. },
  154. args: args{
  155. graph: nil,
  156. enableDowngrade: false,
  157. },
  158. mustExist: map[string]*dep.InstallInfo{
  159. "yay": yayDepInfo,
  160. "linux": linuxDepInfo,
  161. "example-git": exampleDepInfoDevel,
  162. },
  163. mustNotExist: map[string]bool{},
  164. wantErr: false,
  165. },
  166. {
  167. name: "exclude yay",
  168. fields: fields{
  169. input: strings.NewReader("1\n"),
  170. output: io.Discard,
  171. noConfirm: false,
  172. },
  173. args: args{
  174. graph: nil,
  175. enableDowngrade: false,
  176. },
  177. mustExist: map[string]*dep.InstallInfo{
  178. "linux": linuxDepInfo,
  179. "example-git": exampleDepInfoAUR,
  180. },
  181. mustNotExist: map[string]bool{"yay": true},
  182. wantErr: false,
  183. },
  184. {
  185. name: "exclude linux",
  186. fields: fields{
  187. input: strings.NewReader("3\n"),
  188. output: io.Discard,
  189. noConfirm: false,
  190. },
  191. args: args{
  192. graph: nil,
  193. enableDowngrade: false,
  194. },
  195. mustExist: map[string]*dep.InstallInfo{
  196. "yay": yayDepInfo,
  197. "example-git": exampleDepInfoAUR,
  198. },
  199. mustNotExist: map[string]bool{"linux": true},
  200. wantErr: false,
  201. },
  202. {
  203. name: "only linux",
  204. fields: fields{
  205. input: strings.NewReader("^3\n"),
  206. output: io.Discard,
  207. noConfirm: false,
  208. },
  209. args: args{
  210. graph: nil,
  211. enableDowngrade: false,
  212. },
  213. mustExist: map[string]*dep.InstallInfo{
  214. "linux": linuxDepInfo,
  215. },
  216. mustNotExist: map[string]bool{"yay": true, "example-git": true},
  217. wantErr: false,
  218. },
  219. {
  220. name: "exclude all",
  221. fields: fields{
  222. input: strings.NewReader("1-3\n"),
  223. output: io.Discard,
  224. noConfirm: false,
  225. },
  226. args: args{
  227. graph: nil,
  228. enableDowngrade: false,
  229. },
  230. mustExist: map[string]*dep.InstallInfo{},
  231. mustNotExist: map[string]bool{"yay": true, "example-git": true, "linux": true},
  232. wantErr: false,
  233. },
  234. }
  235. for _, tt := range tests {
  236. t.Run(tt.name, func(t *testing.T) {
  237. grapher := dep.NewGrapher(dbExe, mockAUR,
  238. false, true, false, false, false, text.NewLogger(tt.fields.output,
  239. tt.fields.input, true, "test"))
  240. cfg := &settings.Configuration{
  241. Runtime: &settings.Runtime{Mode: parser.ModeAny},
  242. Devel: tt.fields.devel,
  243. }
  244. u := &UpgradeService{
  245. log: text.NewLogger(tt.fields.output,
  246. tt.fields.input, true, "test"),
  247. grapher: grapher,
  248. aurCache: mockAUR,
  249. dbExecutor: dbExe,
  250. vcsStore: vcsStore,
  251. runtime: cfg.Runtime,
  252. cfg: cfg,
  253. noConfirm: tt.fields.noConfirm,
  254. }
  255. got, err := u.GraphUpgrades(context.Background(), tt.args.graph, tt.args.enableDowngrade)
  256. if (err != nil) != tt.wantErr {
  257. t.Errorf("UpgradeService.GraphUpgrades() error = %v, wantErr %v", err, tt.wantErr)
  258. return
  259. }
  260. for node, info := range tt.mustExist {
  261. assert.True(t, got.Exists(node), node)
  262. assert.Equal(t, info, got.GetNodeInfo(node).Value)
  263. }
  264. for node := range tt.mustNotExist {
  265. assert.False(t, got.Exists(node), node)
  266. }
  267. })
  268. }
  269. }