service_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/v12/pkg/db"
  11. "github.com/Jguer/yay/v12/pkg/db/mock"
  12. "github.com/Jguer/yay/v12/pkg/dep"
  13. "github.com/Jguer/yay/v12/pkg/settings"
  14. "github.com/Jguer/yay/v12/pkg/settings/parser"
  15. "github.com/Jguer/yay/v12/pkg/text"
  16. "github.com/Jguer/yay/v12/pkg/topo"
  17. "github.com/Jguer/yay/v12/pkg/vcs"
  18. mockaur "github.com/Jguer/yay/v12/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. wantExclude []string
  126. wantErr bool
  127. }{
  128. {
  129. name: "no input",
  130. fields: fields{
  131. input: strings.NewReader("\n"),
  132. output: io.Discard,
  133. noConfirm: false,
  134. },
  135. args: args{
  136. graph: nil,
  137. enableDowngrade: false,
  138. },
  139. mustExist: map[string]*dep.InstallInfo{
  140. "yay": yayDepInfo,
  141. "linux": linuxDepInfo,
  142. "example-git": exampleDepInfoAUR,
  143. },
  144. mustNotExist: map[string]bool{},
  145. wantErr: false,
  146. wantExclude: []string{},
  147. },
  148. {
  149. name: "no input devel",
  150. fields: fields{
  151. input: strings.NewReader("\n"),
  152. output: io.Discard,
  153. noConfirm: false,
  154. devel: true,
  155. },
  156. args: args{
  157. graph: nil,
  158. enableDowngrade: false,
  159. },
  160. mustExist: map[string]*dep.InstallInfo{
  161. "yay": yayDepInfo,
  162. "linux": linuxDepInfo,
  163. "example-git": exampleDepInfoDevel,
  164. },
  165. mustNotExist: map[string]bool{},
  166. wantErr: false,
  167. wantExclude: []string{},
  168. },
  169. {
  170. name: "exclude yay",
  171. fields: fields{
  172. input: strings.NewReader("1\n"),
  173. output: io.Discard,
  174. noConfirm: false,
  175. },
  176. args: args{
  177. graph: nil,
  178. enableDowngrade: false,
  179. },
  180. mustExist: map[string]*dep.InstallInfo{
  181. "linux": linuxDepInfo,
  182. "example-git": exampleDepInfoAUR,
  183. },
  184. mustNotExist: map[string]bool{"yay": true},
  185. wantErr: false,
  186. wantExclude: []string{"yay"},
  187. },
  188. {
  189. name: "exclude linux",
  190. fields: fields{
  191. input: strings.NewReader("3\n"),
  192. output: io.Discard,
  193. noConfirm: false,
  194. },
  195. args: args{
  196. graph: nil,
  197. enableDowngrade: false,
  198. },
  199. mustExist: map[string]*dep.InstallInfo{
  200. "yay": yayDepInfo,
  201. "example-git": exampleDepInfoAUR,
  202. },
  203. mustNotExist: map[string]bool{"linux": true},
  204. wantErr: false,
  205. wantExclude: []string{"linux"},
  206. },
  207. {
  208. name: "only linux",
  209. fields: fields{
  210. input: strings.NewReader("^3\n"),
  211. output: io.Discard,
  212. noConfirm: false,
  213. },
  214. args: args{
  215. graph: nil,
  216. enableDowngrade: false,
  217. },
  218. mustExist: map[string]*dep.InstallInfo{
  219. "linux": linuxDepInfo,
  220. },
  221. mustNotExist: map[string]bool{"yay": true, "example-git": true},
  222. wantErr: false,
  223. wantExclude: []string{"yay", "example-git"},
  224. },
  225. {
  226. name: "exclude all",
  227. fields: fields{
  228. input: strings.NewReader("1-3\n"),
  229. output: io.Discard,
  230. noConfirm: false,
  231. },
  232. args: args{
  233. graph: nil,
  234. enableDowngrade: false,
  235. },
  236. mustExist: map[string]*dep.InstallInfo{},
  237. mustNotExist: map[string]bool{"yay": true, "example-git": true, "linux": true},
  238. wantErr: false,
  239. wantExclude: []string{"yay", "example-git", "linux"},
  240. },
  241. }
  242. for _, tt := range tests {
  243. t.Run(tt.name, func(t *testing.T) {
  244. grapher := dep.NewGrapher(dbExe, mockAUR,
  245. false, true, false, false, false, text.NewLogger(tt.fields.output,
  246. tt.fields.input, true, "test"))
  247. cfg := &settings.Configuration{
  248. Runtime: &settings.Runtime{Mode: parser.ModeAny},
  249. Devel: tt.fields.devel,
  250. }
  251. u := &UpgradeService{
  252. log: text.NewLogger(tt.fields.output,
  253. tt.fields.input, true, "test"),
  254. grapher: grapher,
  255. aurCache: mockAUR,
  256. dbExecutor: dbExe,
  257. vcsStore: vcsStore,
  258. runtime: cfg.Runtime,
  259. cfg: cfg,
  260. noConfirm: tt.fields.noConfirm,
  261. }
  262. excluded, got, err := u.GraphUpgrades(context.Background(), tt.args.graph, tt.args.enableDowngrade)
  263. if (err != nil) != tt.wantErr {
  264. t.Errorf("UpgradeService.GraphUpgrades() error = %v, wantErr %v", err, tt.wantErr)
  265. return
  266. }
  267. for node, info := range tt.mustExist {
  268. assert.True(t, got.Exists(node), node)
  269. assert.Equal(t, info, got.GetNodeInfo(node).Value)
  270. }
  271. for node := range tt.mustNotExist {
  272. assert.False(t, got.Exists(node), node)
  273. }
  274. assert.ElementsMatch(t, tt.wantExclude, excluded)
  275. })
  276. }
  277. }