dep_graph_test.go 20 KB

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