service_test.go 6.8 KB

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