dep_graph_test.go 7.4 KB

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