dep_graph_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. //go:build !integration
  2. // +build !integration
  3. package dep
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "os"
  10. "testing"
  11. aurc "github.com/Jguer/aur"
  12. alpm "github.com/Jguer/go-alpm/v2"
  13. "github.com/stretchr/testify/require"
  14. "github.com/Jguer/yay/v12/pkg/db"
  15. "github.com/Jguer/yay/v12/pkg/db/mock"
  16. mockaur "github.com/Jguer/yay/v12/pkg/dep/mock"
  17. aur "github.com/Jguer/yay/v12/pkg/query"
  18. "github.com/Jguer/yay/v12/pkg/settings"
  19. "github.com/Jguer/yay/v12/pkg/settings/exe"
  20. "github.com/Jguer/yay/v12/pkg/text"
  21. )
  22. func ptrString(s string) *string {
  23. return &s
  24. }
  25. func getFromFile(t *testing.T, filePath string) mockaur.GetFunc {
  26. f, err := os.Open(filePath)
  27. require.NoError(t, err)
  28. fBytes, err := io.ReadAll(f)
  29. require.NoError(t, err)
  30. pkgs := []aur.Pkg{}
  31. err = json.Unmarshal(fBytes, &pkgs)
  32. require.NoError(t, err)
  33. return func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  34. return pkgs, nil
  35. }
  36. }
  37. func TestGrapher_GraphFromTargets_jellyfin(t *testing.T) {
  38. mockDB := &mock.DBExecutor{
  39. SyncPackageFn: func(string) mock.IPackage { return nil },
  40. SyncSatisfierFn: func(s string) mock.IPackage {
  41. switch s {
  42. case "jellyfin":
  43. return nil
  44. case "dotnet-runtime-6.0":
  45. return &mock.Package{
  46. PName: "dotnet-runtime-6.0",
  47. PBase: "dotnet-runtime-6.0",
  48. PVersion: "6.0.100-1",
  49. PDB: mock.NewDB("community"),
  50. }
  51. case "dotnet-sdk-6.0":
  52. return &mock.Package{
  53. PName: "dotnet-sdk-6.0",
  54. PBase: "dotnet-sdk-6.0",
  55. PVersion: "6.0.100-1",
  56. PDB: mock.NewDB("community"),
  57. }
  58. }
  59. return nil
  60. },
  61. PackagesFromGroupFn: func(string) []mock.IPackage { return nil },
  62. LocalSatisfierExistsFn: func(s string) bool {
  63. switch s {
  64. case "dotnet-sdk-6.0", "dotnet-runtime-6.0", "jellyfin-server=10.8.8", "jellyfin-web=10.8.8":
  65. return false
  66. }
  67. return true
  68. },
  69. LocalPackageFn: func(string) mock.IPackage { return nil },
  70. }
  71. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  72. if query.Needles[0] == "jellyfin" {
  73. jfinFn := getFromFile(t, "testdata/jellyfin.json")
  74. return jfinFn(ctx, query)
  75. }
  76. if query.Needles[0] == "jellyfin-web" {
  77. jfinWebFn := getFromFile(t, "testdata/jellyfin-web.json")
  78. return jfinWebFn(ctx, query)
  79. }
  80. if query.Needles[0] == "jellyfin-server" {
  81. jfinServerFn := getFromFile(t, "testdata/jellyfin-server.json")
  82. return jfinServerFn(ctx, query)
  83. }
  84. panic(fmt.Sprintf("implement me %v", query.Needles))
  85. }}
  86. type fields struct {
  87. dbExecutor db.Executor
  88. aurCache aurc.QueryClient
  89. noDeps bool
  90. noCheckDeps bool
  91. }
  92. type args struct {
  93. targets []string
  94. }
  95. tests := []struct {
  96. name string
  97. fields fields
  98. args args
  99. want []map[string]*InstallInfo
  100. wantErr bool
  101. }{
  102. {
  103. name: "noDeps",
  104. fields: fields{
  105. dbExecutor: mockDB,
  106. aurCache: mockAUR,
  107. noDeps: true,
  108. noCheckDeps: false,
  109. },
  110. args: args{
  111. targets: []string{"jellyfin"},
  112. },
  113. want: []map[string]*InstallInfo{
  114. {
  115. "jellyfin": {
  116. Source: AUR,
  117. Reason: Explicit,
  118. Version: "10.8.8-1",
  119. AURBase: ptrString("jellyfin"),
  120. },
  121. },
  122. {
  123. "dotnet-sdk-6.0": {
  124. Source: Sync,
  125. Reason: MakeDep,
  126. Version: "6.0.100-1",
  127. SyncDBName: ptrString("community"),
  128. },
  129. },
  130. },
  131. wantErr: false,
  132. },
  133. {
  134. name: "deps",
  135. fields: fields{
  136. dbExecutor: mockDB,
  137. aurCache: mockAUR,
  138. noDeps: false,
  139. noCheckDeps: false,
  140. },
  141. args: args{
  142. targets: []string{"jellyfin"},
  143. },
  144. want: []map[string]*InstallInfo{
  145. {
  146. "jellyfin": {
  147. Source: AUR,
  148. Reason: Explicit,
  149. Version: "10.8.8-1",
  150. AURBase: ptrString("jellyfin"),
  151. },
  152. },
  153. {
  154. "jellyfin-web": {
  155. Source: AUR,
  156. Reason: Dep,
  157. Version: "10.8.8-1",
  158. AURBase: ptrString("jellyfin"),
  159. },
  160. "jellyfin-server": {
  161. Source: AUR,
  162. Reason: Dep,
  163. Version: "10.8.8-1",
  164. AURBase: ptrString("jellyfin"),
  165. },
  166. },
  167. {
  168. "dotnet-sdk-6.0": {
  169. Source: Sync,
  170. Reason: MakeDep,
  171. Version: "6.0.100-1",
  172. SyncDBName: ptrString("community"),
  173. },
  174. "dotnet-runtime-6.0": {
  175. Source: Sync,
  176. Reason: Dep,
  177. Version: "6.0.100-1",
  178. SyncDBName: ptrString("community"),
  179. },
  180. },
  181. },
  182. wantErr: false,
  183. },
  184. }
  185. for _, tt := range tests {
  186. t.Run(tt.name, func(t *testing.T) {
  187. g := NewGrapher(tt.fields.dbExecutor, &settings.Configuration{},
  188. tt.fields.aurCache, &exe.MockBuilder{Runner: &exe.MockRunner{}}, false, true,
  189. tt.fields.noDeps, tt.fields.noCheckDeps, false,
  190. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  191. got, err := g.GraphFromTargets(context.Background(), nil, tt.args.targets)
  192. require.NoError(t, err)
  193. layers := got.TopoSortedLayerMap(nil)
  194. require.EqualValues(t, tt.want, layers, layers)
  195. })
  196. }
  197. }
  198. func TestGrapher_GraphProvides_androidsdk(t *testing.T) {
  199. mockDB := &mock.DBExecutor{
  200. SyncPackageFn: func(string) mock.IPackage { return nil },
  201. SyncSatisfierFn: func(s string) mock.IPackage {
  202. switch s {
  203. case "android-sdk":
  204. return nil
  205. case "jdk11-openjdk":
  206. return &mock.Package{
  207. PName: "jdk11-openjdk",
  208. PVersion: "11.0.12.u7-1",
  209. PDB: mock.NewDB("community"),
  210. PProvides: mock.DependList{
  211. Depends: []alpm.Depend{
  212. {Name: "java-environment", Version: "11", Mod: alpm.DepModEq},
  213. {Name: "java-environment-openjdk", Version: "11", Mod: alpm.DepModEq},
  214. {Name: "jdk11-openjdk", Version: "11.0.19.u7-1", Mod: alpm.DepModEq},
  215. },
  216. },
  217. }
  218. case "java-environment":
  219. panic("not supposed to be called")
  220. }
  221. panic("implement me " + s)
  222. },
  223. PackagesFromGroupFn: func(string) []mock.IPackage { return nil },
  224. LocalSatisfierExistsFn: func(s string) bool {
  225. switch s {
  226. case "java-environment":
  227. return false
  228. }
  229. switch s {
  230. case "libxtst", "fontconfig", "freetype2", "lib32-gcc-libs", "lib32-glibc", "libx11", "libxext", "libxrender", "zlib", "gcc-libs":
  231. return true
  232. }
  233. panic("implement me " + s)
  234. },
  235. LocalPackageFn: func(string) mock.IPackage { return nil },
  236. }
  237. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  238. if query.Needles[0] == "android-sdk" {
  239. jfinFn := getFromFile(t, "testdata/android-sdk.json")
  240. return jfinFn(ctx, query)
  241. }
  242. panic(fmt.Sprintf("implement me %v", query.Needles))
  243. }}
  244. type fields struct {
  245. dbExecutor db.Executor
  246. aurCache aurc.QueryClient
  247. noDeps bool
  248. noCheckDeps bool
  249. }
  250. type args struct {
  251. targets []string
  252. }
  253. tests := []struct {
  254. name string
  255. fields fields
  256. args args
  257. want []map[string]*InstallInfo
  258. wantErr bool
  259. }{
  260. {
  261. name: "explicit dep",
  262. fields: fields{
  263. dbExecutor: mockDB,
  264. aurCache: mockAUR,
  265. noDeps: false,
  266. noCheckDeps: false,
  267. },
  268. args: args{
  269. targets: []string{"android-sdk", "jdk11-openjdk"},
  270. },
  271. want: []map[string]*InstallInfo{
  272. {
  273. "android-sdk": {
  274. Source: AUR,
  275. Reason: Explicit,
  276. Version: "26.1.1-2",
  277. AURBase: ptrString("android-sdk"),
  278. },
  279. },
  280. {
  281. "jdk11-openjdk": {
  282. Source: Sync,
  283. Reason: Explicit,
  284. Version: "11.0.12.u7-1",
  285. SyncDBName: ptrString("community"),
  286. },
  287. },
  288. },
  289. wantErr: false,
  290. },
  291. }
  292. for _, tt := range tests {
  293. t.Run(tt.name, func(t *testing.T) {
  294. g := NewGrapher(tt.fields.dbExecutor, &settings.Configuration{},
  295. tt.fields.aurCache, &exe.MockBuilder{Runner: &exe.MockRunner{}}, false, true,
  296. tt.fields.noDeps, tt.fields.noCheckDeps, false,
  297. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  298. got, err := g.GraphFromTargets(context.Background(), nil, tt.args.targets)
  299. require.NoError(t, err)
  300. layers := got.TopoSortedLayerMap(nil)
  301. require.EqualValues(t, tt.want, layers, layers)
  302. })
  303. }
  304. }
  305. func TestGrapher_GraphFromAUR_Deps_ceph_bin(t *testing.T) {
  306. mockDB := &mock.DBExecutor{
  307. SyncPackageFn: func(string) mock.IPackage { return nil },
  308. PackagesFromGroupFn: func(string) []mock.IPackage { return []mock.IPackage{} },
  309. SyncSatisfierFn: func(s string) mock.IPackage {
  310. switch s {
  311. case "ceph-bin", "ceph-libs-bin":
  312. return nil
  313. case "ceph", "ceph-libs", "ceph-libs=17.2.6-2":
  314. return nil
  315. }
  316. panic("implement me " + s)
  317. },
  318. LocalSatisfierExistsFn: func(s string) bool {
  319. switch s {
  320. case "ceph-libs", "ceph-libs=17.2.6-2":
  321. return false
  322. case "dep1", "dep2", "dep3", "makedep1", "makedep2", "checkdep1":
  323. return true
  324. }
  325. panic("implement me " + s)
  326. },
  327. LocalPackageFn: func(string) mock.IPackage { return nil },
  328. }
  329. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  330. mockPkgs := map[string]aur.Pkg{
  331. "ceph-bin": {
  332. Name: "ceph-bin",
  333. PackageBase: "ceph-bin",
  334. Version: "17.2.6-2",
  335. Depends: []string{"ceph-libs=17.2.6-2", "dep1"},
  336. Provides: []string{"ceph=17.2.6-2"},
  337. },
  338. "ceph-libs-bin": {
  339. Name: "ceph-libs-bin",
  340. PackageBase: "ceph-bin",
  341. Version: "17.2.6-2",
  342. Depends: []string{"dep1", "dep2"},
  343. Provides: []string{"ceph-libs=17.2.6-2"},
  344. },
  345. "ceph": {
  346. Name: "ceph",
  347. PackageBase: "ceph",
  348. Version: "17.2.6-2",
  349. Depends: []string{"ceph-libs=17.2.6-2", "dep1"},
  350. MakeDepends: []string{"makedep1"},
  351. CheckDepends: []string{"checkdep1"},
  352. Provides: []string{"ceph=17.2.6-2"},
  353. },
  354. "ceph-libs": {
  355. Name: "ceph-libs",
  356. PackageBase: "ceph",
  357. Version: "17.2.6-2",
  358. Depends: []string{"dep1", "dep2", "dep3"},
  359. MakeDepends: []string{"makedep1", "makedep2"},
  360. CheckDepends: []string{"checkdep1"},
  361. Provides: []string{"ceph-libs=17.2.6-2"},
  362. },
  363. }
  364. pkgs := []aur.Pkg{}
  365. for _, needle := range query.Needles {
  366. if pkg, ok := mockPkgs[needle]; ok {
  367. pkgs = append(pkgs, pkg)
  368. } else {
  369. panic(fmt.Sprintf("implement me %v", needle))
  370. }
  371. }
  372. return pkgs, nil
  373. }}
  374. installInfos := map[string]*InstallInfo{
  375. "ceph-bin exp": {
  376. Source: AUR,
  377. Reason: Explicit,
  378. Version: "17.2.6-2",
  379. AURBase: ptrString("ceph-bin"),
  380. },
  381. "ceph-libs-bin exp": {
  382. Source: AUR,
  383. Reason: Explicit,
  384. Version: "17.2.6-2",
  385. AURBase: ptrString("ceph-bin"),
  386. },
  387. "ceph exp": {
  388. Source: AUR,
  389. Reason: Explicit,
  390. Version: "17.2.6-2",
  391. AURBase: ptrString("ceph"),
  392. },
  393. "ceph-libs exp": {
  394. Source: AUR,
  395. Reason: Explicit,
  396. Version: "17.2.6-2",
  397. AURBase: ptrString("ceph"),
  398. },
  399. "ceph-libs dep": {
  400. Source: AUR,
  401. Reason: Dep,
  402. Version: "17.2.6-2",
  403. AURBase: ptrString("ceph"),
  404. },
  405. }
  406. tests := []struct {
  407. name string
  408. targets []string
  409. wantLayers []map[string]*InstallInfo
  410. wantErr bool
  411. }{
  412. {
  413. name: "ceph-bin ceph-libs-bin",
  414. targets: []string{"ceph-bin", "ceph-libs-bin"},
  415. wantLayers: []map[string]*InstallInfo{
  416. {"ceph-bin": installInfos["ceph-bin exp"]},
  417. {"ceph-libs-bin": installInfos["ceph-libs-bin exp"]},
  418. },
  419. wantErr: false,
  420. },
  421. {
  422. name: "ceph-libs-bin ceph-bin (reversed order)",
  423. targets: []string{"ceph-libs-bin", "ceph-bin"},
  424. wantLayers: []map[string]*InstallInfo{
  425. {"ceph-bin": installInfos["ceph-bin exp"]},
  426. {"ceph-libs-bin": installInfos["ceph-libs-bin exp"]},
  427. },
  428. wantErr: false,
  429. },
  430. {
  431. name: "ceph",
  432. targets: []string{"ceph"},
  433. wantLayers: []map[string]*InstallInfo{
  434. {"ceph": installInfos["ceph exp"]},
  435. {"ceph-libs": installInfos["ceph-libs dep"]},
  436. },
  437. wantErr: false,
  438. },
  439. {
  440. name: "ceph-bin",
  441. targets: []string{"ceph-bin"},
  442. wantLayers: []map[string]*InstallInfo{
  443. {"ceph-bin": installInfos["ceph-bin exp"]},
  444. {"ceph-libs": installInfos["ceph-libs dep"]},
  445. },
  446. wantErr: false,
  447. },
  448. {
  449. name: "ceph-bin ceph-libs",
  450. targets: []string{"ceph-bin", "ceph-libs"},
  451. wantLayers: []map[string]*InstallInfo{
  452. {"ceph-bin": installInfos["ceph-bin exp"]},
  453. {"ceph-libs": installInfos["ceph-libs exp"]},
  454. },
  455. wantErr: false,
  456. },
  457. {
  458. name: "ceph-libs ceph-bin (reversed order)",
  459. targets: []string{"ceph-libs", "ceph-bin"},
  460. wantLayers: []map[string]*InstallInfo{
  461. {"ceph-bin": installInfos["ceph-bin exp"]},
  462. {"ceph-libs": installInfos["ceph-libs exp"]},
  463. },
  464. wantErr: false,
  465. },
  466. {
  467. name: "ceph ceph-libs-bin",
  468. targets: []string{"ceph", "ceph-libs-bin"},
  469. wantLayers: []map[string]*InstallInfo{
  470. {"ceph": installInfos["ceph exp"]},
  471. {"ceph-libs-bin": installInfos["ceph-libs-bin exp"]},
  472. },
  473. wantErr: false,
  474. },
  475. {
  476. name: "ceph-libs-bin ceph (reversed order)",
  477. targets: []string{"ceph-libs-bin", "ceph"},
  478. wantLayers: []map[string]*InstallInfo{
  479. {"ceph": installInfos["ceph exp"]},
  480. {"ceph-libs-bin": installInfos["ceph-libs-bin exp"]},
  481. },
  482. wantErr: false,
  483. },
  484. }
  485. for _, tt := range tests {
  486. t.Run(tt.name, func(t *testing.T) {
  487. g := NewGrapher(mockDB, &settings.Configuration{}, mockAUR,
  488. &exe.MockBuilder{Runner: &exe.MockRunner{}}, false, true, false, false, false,
  489. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  490. got, err := g.GraphFromTargets(context.Background(), nil, tt.targets)
  491. require.NoError(t, err)
  492. layers := got.TopoSortedLayerMap(nil)
  493. require.EqualValues(t, tt.wantLayers, layers, layers)
  494. })
  495. }
  496. }
  497. func TestGrapher_GraphFromAUR_Deps_gourou(t *testing.T) {
  498. mockDB := &mock.DBExecutor{
  499. SyncPackageFn: func(string) mock.IPackage { return nil },
  500. PackagesFromGroupFn: func(string) []mock.IPackage { return []mock.IPackage{} },
  501. SyncSatisfierFn: func(s string) mock.IPackage {
  502. switch s {
  503. case "gourou", "libzip-git":
  504. return nil
  505. case "libzip":
  506. return &mock.Package{
  507. PName: "libzip",
  508. PVersion: "1.9.2-1",
  509. PDB: mock.NewDB("extra"),
  510. }
  511. }
  512. panic("implement me " + s)
  513. },
  514. LocalSatisfierExistsFn: func(s string) bool {
  515. switch s {
  516. case "gourou", "libzip", "libzip-git":
  517. return false
  518. case "dep1", "dep2":
  519. return true
  520. }
  521. panic("implement me " + s)
  522. },
  523. LocalPackageFn: func(string) mock.IPackage { return nil },
  524. }
  525. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  526. mockPkgs := map[string]aur.Pkg{
  527. "gourou": {
  528. Name: "gourou",
  529. PackageBase: "gourou",
  530. Version: "0.8.1",
  531. Depends: []string{"libzip"},
  532. },
  533. "libzip-git": {
  534. Name: "libzip-git",
  535. PackageBase: "libzip-git",
  536. Version: "1.9.2.r159.gb3ac716c-1",
  537. Depends: []string{"dep1", "dep2"},
  538. Provides: []string{"libzip=1.9.2.r159.gb3ac716c"},
  539. },
  540. }
  541. pkgs := []aur.Pkg{}
  542. for _, needle := range query.Needles {
  543. if pkg, ok := mockPkgs[needle]; ok {
  544. pkgs = append(pkgs, pkg)
  545. } else {
  546. panic(fmt.Sprintf("implement me %v", needle))
  547. }
  548. }
  549. return pkgs, nil
  550. }}
  551. installInfos := map[string]*InstallInfo{
  552. "gourou exp": {
  553. Source: AUR,
  554. Reason: Explicit,
  555. Version: "0.8.1",
  556. AURBase: ptrString("gourou"),
  557. },
  558. "libzip dep": {
  559. Source: Sync,
  560. Reason: Dep,
  561. Version: "1.9.2-1",
  562. SyncDBName: ptrString("extra"),
  563. },
  564. "libzip exp": {
  565. Source: Sync,
  566. Reason: Explicit,
  567. Version: "1.9.2-1",
  568. SyncDBName: ptrString("extra"),
  569. },
  570. "libzip-git exp": {
  571. Source: AUR,
  572. Reason: Explicit,
  573. Version: "1.9.2.r159.gb3ac716c-1",
  574. AURBase: ptrString("libzip-git"),
  575. },
  576. }
  577. tests := []struct {
  578. name string
  579. targets []string
  580. wantLayers []map[string]*InstallInfo
  581. wantErr bool
  582. }{
  583. {
  584. name: "gourou",
  585. targets: []string{"gourou"},
  586. wantLayers: []map[string]*InstallInfo{
  587. {"gourou": installInfos["gourou exp"]},
  588. {"libzip": installInfos["libzip dep"]},
  589. },
  590. wantErr: false,
  591. },
  592. {
  593. name: "gourou libzip",
  594. targets: []string{"gourou", "libzip"},
  595. wantLayers: []map[string]*InstallInfo{
  596. {"gourou": installInfos["gourou exp"]},
  597. {"libzip": installInfos["libzip exp"]},
  598. },
  599. wantErr: false,
  600. },
  601. {
  602. name: "gourou libzip-git",
  603. targets: []string{"gourou", "libzip-git"},
  604. wantLayers: []map[string]*InstallInfo{
  605. {"gourou": installInfos["gourou exp"]},
  606. {"libzip-git": installInfos["libzip-git exp"]},
  607. },
  608. wantErr: false,
  609. },
  610. {
  611. name: "libzip-git gourou (reversed order)",
  612. targets: []string{"libzip-git", "gourou"},
  613. wantLayers: []map[string]*InstallInfo{
  614. {"gourou": installInfos["gourou exp"]},
  615. {"libzip-git": installInfos["libzip-git exp"]},
  616. },
  617. wantErr: false,
  618. },
  619. }
  620. for _, tt := range tests {
  621. t.Run(tt.name, func(t *testing.T) {
  622. g := NewGrapher(mockDB, &settings.Configuration{}, mockAUR, &exe.MockBuilder{Runner: &exe.MockRunner{}},
  623. false, true, false, false, false,
  624. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  625. got, err := g.GraphFromTargets(context.Background(), nil, tt.targets)
  626. require.NoError(t, err)
  627. layers := got.TopoSortedLayerMap(nil)
  628. require.EqualValues(t, tt.wantLayers, layers, layers)
  629. })
  630. }
  631. }
  632. func TestGrapher_GraphFromTargets_ReinstalledDeps(t *testing.T) {
  633. mockDB := &mock.DBExecutor{
  634. SyncPackageFn: func(string) mock.IPackage { return nil },
  635. PackagesFromGroupFn: func(string) []mock.IPackage { return []mock.IPackage{} },
  636. SyncSatisfierFn: func(s string) mock.IPackage {
  637. switch s {
  638. case "gourou":
  639. return nil
  640. case "libzip":
  641. return &mock.Package{
  642. PName: "libzip",
  643. PVersion: "1.9.2-1",
  644. PDB: mock.NewDB("extra"),
  645. }
  646. }
  647. panic("implement me " + s)
  648. },
  649. SatisfierFromDBFn: func(s, s2 string) (mock.IPackage, error) {
  650. if s2 == "extra" {
  651. switch s {
  652. case "libzip":
  653. return &mock.Package{
  654. PName: "libzip",
  655. PVersion: "1.9.2-1",
  656. PDB: mock.NewDB("extra"),
  657. }, nil
  658. }
  659. }
  660. panic("implement me " + s2 + "/" + s)
  661. },
  662. LocalSatisfierExistsFn: func(s string) bool {
  663. switch s {
  664. case "gourou", "libzip":
  665. return true
  666. }
  667. panic("implement me " + s)
  668. },
  669. LocalPackageFn: func(s string) mock.IPackage {
  670. switch s {
  671. case "libzip":
  672. return &mock.Package{
  673. PName: "libzip",
  674. PVersion: "1.9.2-1",
  675. PDB: mock.NewDB("extra"),
  676. PReason: alpm.PkgReasonDepend,
  677. }
  678. case "gourou":
  679. return &mock.Package{
  680. PName: "gourou",
  681. PVersion: "0.8.1",
  682. PDB: mock.NewDB("aur"),
  683. PReason: alpm.PkgReasonDepend,
  684. }
  685. }
  686. return nil
  687. },
  688. }
  689. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  690. mockPkgs := map[string]aur.Pkg{
  691. "gourou": {
  692. Name: "gourou",
  693. PackageBase: "gourou",
  694. Version: "0.8.1",
  695. Depends: []string{"libzip"},
  696. },
  697. }
  698. pkgs := []aur.Pkg{}
  699. for _, needle := range query.Needles {
  700. if pkg, ok := mockPkgs[needle]; ok {
  701. pkgs = append(pkgs, pkg)
  702. } else {
  703. panic(fmt.Sprintf("implement me %v", needle))
  704. }
  705. }
  706. return pkgs, nil
  707. }}
  708. installInfos := map[string]*InstallInfo{
  709. "gourou dep": {
  710. Source: AUR,
  711. Reason: Dep,
  712. Version: "0.8.1",
  713. AURBase: ptrString("gourou"),
  714. },
  715. "libzip dep": {
  716. Source: Sync,
  717. Reason: Dep,
  718. Version: "1.9.2-1",
  719. SyncDBName: ptrString("extra"),
  720. },
  721. }
  722. tests := []struct {
  723. name string
  724. targets []string
  725. wantLayers []map[string]*InstallInfo
  726. wantErr bool
  727. }{
  728. {
  729. name: "gourou libzip",
  730. targets: []string{"gourou", "libzip"},
  731. wantLayers: []map[string]*InstallInfo{
  732. {"gourou": installInfos["gourou dep"]},
  733. {"libzip": installInfos["libzip dep"]},
  734. },
  735. wantErr: false,
  736. },
  737. {
  738. name: "aur/gourou extra/libzip",
  739. targets: []string{"aur/gourou", "extra/libzip"},
  740. wantLayers: []map[string]*InstallInfo{
  741. {"gourou": installInfos["gourou dep"]},
  742. {"libzip": installInfos["libzip dep"]},
  743. },
  744. wantErr: false,
  745. },
  746. }
  747. for _, tt := range tests {
  748. t.Run(tt.name, func(t *testing.T) {
  749. g := NewGrapher(mockDB, &settings.Configuration{}, mockAUR, &exe.MockBuilder{Runner: &exe.MockRunner{}},
  750. false, true, false, false, false,
  751. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  752. got, err := g.GraphFromTargets(context.Background(), nil, tt.targets)
  753. require.NoError(t, err)
  754. layers := got.TopoSortedLayerMap(nil)
  755. require.EqualValues(t, tt.wantLayers, layers, layers)
  756. })
  757. }
  758. }