dep_graph_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. }
  68. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  69. if query.Needles[0] == "jellyfin" {
  70. jfinFn := getFromFile(t, "testdata/jellyfin.json")
  71. return jfinFn(ctx, query)
  72. }
  73. if query.Needles[0] == "jellyfin-web" {
  74. jfinWebFn := getFromFile(t, "testdata/jellyfin-web.json")
  75. return jfinWebFn(ctx, query)
  76. }
  77. if query.Needles[0] == "jellyfin-server" {
  78. jfinServerFn := getFromFile(t, "testdata/jellyfin-server.json")
  79. return jfinServerFn(ctx, query)
  80. }
  81. panic(fmt.Sprintf("implement me %v", query.Needles))
  82. }}
  83. type fields struct {
  84. dbExecutor db.Executor
  85. aurCache aurc.QueryClient
  86. noDeps bool
  87. noCheckDeps bool
  88. }
  89. type args struct {
  90. targets []string
  91. }
  92. tests := []struct {
  93. name string
  94. fields fields
  95. args args
  96. want []map[string]*InstallInfo
  97. wantErr bool
  98. }{
  99. {
  100. name: "noDeps",
  101. fields: fields{
  102. dbExecutor: mockDB,
  103. aurCache: mockAUR,
  104. noDeps: true,
  105. noCheckDeps: false,
  106. },
  107. args: args{
  108. targets: []string{"jellyfin"},
  109. },
  110. want: []map[string]*InstallInfo{
  111. {
  112. "jellyfin": {
  113. Source: AUR,
  114. Reason: Explicit,
  115. Version: "10.8.8-1",
  116. AURBase: ptrString("jellyfin"),
  117. },
  118. },
  119. {
  120. "dotnet-sdk-6.0": {
  121. Source: Sync,
  122. Reason: MakeDep,
  123. Version: "6.0.100-1",
  124. SyncDBName: ptrString("community"),
  125. },
  126. },
  127. },
  128. wantErr: false,
  129. },
  130. {
  131. name: "deps",
  132. fields: fields{
  133. dbExecutor: mockDB,
  134. aurCache: mockAUR,
  135. noDeps: false,
  136. noCheckDeps: false,
  137. },
  138. args: args{
  139. targets: []string{"jellyfin"},
  140. },
  141. want: []map[string]*InstallInfo{
  142. {
  143. "jellyfin": {
  144. Source: AUR,
  145. Reason: Explicit,
  146. Version: "10.8.8-1",
  147. AURBase: ptrString("jellyfin"),
  148. },
  149. },
  150. {
  151. "jellyfin-web": {
  152. Source: AUR,
  153. Reason: Dep,
  154. Version: "10.8.8-1",
  155. AURBase: ptrString("jellyfin"),
  156. },
  157. "jellyfin-server": {
  158. Source: AUR,
  159. Reason: Dep,
  160. Version: "10.8.8-1",
  161. AURBase: ptrString("jellyfin"),
  162. },
  163. },
  164. {
  165. "dotnet-sdk-6.0": {
  166. Source: Sync,
  167. Reason: MakeDep,
  168. Version: "6.0.100-1",
  169. SyncDBName: ptrString("community"),
  170. },
  171. "dotnet-runtime-6.0": {
  172. Source: Sync,
  173. Reason: Dep,
  174. Version: "6.0.100-1",
  175. SyncDBName: ptrString("community"),
  176. },
  177. },
  178. },
  179. wantErr: false,
  180. },
  181. }
  182. for _, tt := range tests {
  183. t.Run(tt.name, func(t *testing.T) {
  184. g := NewGrapher(tt.fields.dbExecutor,
  185. tt.fields.aurCache, false, true,
  186. tt.fields.noDeps, tt.fields.noCheckDeps, false,
  187. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  188. got, err := g.GraphFromTargets(context.Background(), nil, tt.args.targets)
  189. require.NoError(t, err)
  190. layers := got.TopoSortedLayerMap(nil)
  191. require.EqualValues(t, tt.want, layers, layers)
  192. })
  193. }
  194. }
  195. func TestGrapher_GraphProvides_androidsdk(t *testing.T) {
  196. mockDB := &mock.DBExecutor{
  197. SyncPackageFn: func(string) mock.IPackage { return nil },
  198. SyncSatisfierFn: func(s string) mock.IPackage {
  199. switch s {
  200. case "android-sdk":
  201. return nil
  202. case "jdk11-openjdk":
  203. return &mock.Package{
  204. PName: "jdk11-openjdk",
  205. PVersion: "11.0.12.u7-1",
  206. PDB: mock.NewDB("community"),
  207. PProvides: mock.DependList{
  208. Depends: []alpm.Depend{
  209. {Name: "java-environment", Version: "11", Mod: alpm.DepModEq},
  210. {Name: "java-environment-openjdk", Version: "11", Mod: alpm.DepModEq},
  211. {Name: "jdk11-openjdk", Version: "11.0.19.u7-1", Mod: alpm.DepModEq},
  212. },
  213. },
  214. }
  215. case "java-environment":
  216. panic("not supposed to be called")
  217. }
  218. panic("implement me " + s)
  219. },
  220. PackagesFromGroupFn: func(string) []mock.IPackage { return nil },
  221. LocalSatisfierExistsFn: func(s string) bool {
  222. switch s {
  223. case "java-environment":
  224. return false
  225. }
  226. switch s {
  227. case "libxtst", "fontconfig", "freetype2", "lib32-gcc-libs", "lib32-glibc", "libx11", "libxext", "libxrender", "zlib", "gcc-libs":
  228. return true
  229. }
  230. panic("implement me " + s)
  231. },
  232. }
  233. mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
  234. if query.Needles[0] == "android-sdk" {
  235. jfinFn := getFromFile(t, "testdata/android-sdk.json")
  236. return jfinFn(ctx, query)
  237. }
  238. panic(fmt.Sprintf("implement me %v", query.Needles))
  239. }}
  240. type fields struct {
  241. dbExecutor db.Executor
  242. aurCache aurc.QueryClient
  243. noDeps bool
  244. noCheckDeps bool
  245. }
  246. type args struct {
  247. targets []string
  248. }
  249. tests := []struct {
  250. name string
  251. fields fields
  252. args args
  253. want []map[string]*InstallInfo
  254. wantErr bool
  255. }{
  256. {
  257. name: "explicit dep",
  258. fields: fields{
  259. dbExecutor: mockDB,
  260. aurCache: mockAUR,
  261. noDeps: false,
  262. noCheckDeps: false,
  263. },
  264. args: args{
  265. targets: []string{"android-sdk", "jdk11-openjdk"},
  266. },
  267. want: []map[string]*InstallInfo{
  268. {
  269. "android-sdk": {
  270. Source: AUR,
  271. Reason: Explicit,
  272. Version: "26.1.1-2",
  273. AURBase: ptrString("android-sdk"),
  274. },
  275. },
  276. {
  277. "jdk11-openjdk": {
  278. Source: Sync,
  279. Reason: Explicit,
  280. Version: "11.0.12.u7-1",
  281. SyncDBName: ptrString("community"),
  282. },
  283. },
  284. },
  285. wantErr: false,
  286. },
  287. }
  288. for _, tt := range tests {
  289. t.Run(tt.name, func(t *testing.T) {
  290. g := NewGrapher(tt.fields.dbExecutor,
  291. tt.fields.aurCache, false, true,
  292. tt.fields.noDeps, tt.fields.noCheckDeps, false,
  293. text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
  294. got, err := g.GraphFromTargets(context.Background(), nil, tt.args.targets)
  295. require.NoError(t, err)
  296. layers := got.TopoSortedLayerMap(nil)
  297. require.EqualValues(t, tt.want, layers, layers)
  298. })
  299. }
  300. }