dep_graph_test.go 20 KB

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