vcs_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //go:build !integration
  2. // +build !integration
  3. package vcs
  4. import (
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "strings"
  13. "testing"
  14. gosrc "github.com/Morganamilo/go-srcinfo"
  15. "github.com/bradleyjkemp/cupaloy"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. "github.com/Jguer/yay/v12/pkg/db"
  19. "github.com/Jguer/yay/v12/pkg/settings/exe"
  20. "github.com/Jguer/yay/v12/pkg/text"
  21. )
  22. func TestParsing(t *testing.T) {
  23. t.Parallel()
  24. type source struct {
  25. URL string
  26. Branch string
  27. Protocols []string
  28. }
  29. urls := []string{
  30. "git+https://github.com/neovim/neovim.git",
  31. "git://github.com/jguer/yay.git#branch=master",
  32. "git://github.com/davidgiven/ack",
  33. "git://github.com/jguer/yay.git#tag=v3.440",
  34. "git://github.com/jguer/yay.git#commit=e5470c88c6e2f9e0f97deb4728659ffa70ef5d0c",
  35. "a+b+c+d+e+f://github.com/jguer/yay.git#branch=foo",
  36. }
  37. sources := []source{
  38. {"github.com/neovim/neovim.git", "HEAD", []string{"https"}},
  39. {"github.com/jguer/yay.git", "master", []string{"git"}},
  40. {"github.com/davidgiven/ack", "HEAD", []string{"git"}},
  41. {"", "", nil},
  42. {"", "", nil},
  43. {"", "", nil},
  44. }
  45. for n, url := range urls {
  46. url, branch, protocols := parseSource(url)
  47. compare := sources[n]
  48. assert.Equal(t, compare.URL, url)
  49. assert.Equal(t, compare.Branch, branch)
  50. assert.Equal(t, compare.Protocols, protocols)
  51. }
  52. }
  53. func TestNewInfoStore(t *testing.T) {
  54. t.Parallel()
  55. type args struct {
  56. filePath string
  57. cmdBuilder *exe.CmdBuilder
  58. }
  59. tests := []struct {
  60. name string
  61. args args
  62. }{
  63. {
  64. name: "normal",
  65. args: args{
  66. "/tmp/a.json",
  67. &exe.CmdBuilder{GitBin: "git", GitFlags: []string{"--a", "--b"}, Runner: &exe.OSRunner{
  68. Log: text.NewLogger(io.Discard, os.Stderr, strings.NewReader(""), true, "test"),
  69. }},
  70. },
  71. },
  72. }
  73. for _, tt := range tests {
  74. tt := tt
  75. t.Run(tt.name, func(t *testing.T) {
  76. t.Parallel()
  77. got := NewInfoStore(tt.args.filePath, tt.args.cmdBuilder,
  78. text.NewLogger(io.Discard, os.Stderr, strings.NewReader(""), true, "test"))
  79. assert.NotNil(t, got)
  80. assert.Equal(t, []string{"--a", "--b"}, got.CmdBuilder.(*exe.CmdBuilder).GitFlags)
  81. assert.Equal(t, tt.args.cmdBuilder, got.CmdBuilder)
  82. assert.Equal(t, "/tmp/a.json", got.FilePath)
  83. })
  84. }
  85. }
  86. type MockRunner struct {
  87. Returned []string
  88. Index int
  89. }
  90. func (r *MockRunner) Show(cmd *exec.Cmd) error {
  91. return nil
  92. }
  93. func (r *MockRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
  94. stdout = r.Returned[r.Index]
  95. if r.Returned[0] == "error" {
  96. err = errors.New("possible error")
  97. }
  98. return stdout, stderr, err
  99. }
  100. func TestInfoStoreToUpgrade(t *testing.T) {
  101. t.Parallel()
  102. type fields struct {
  103. CmdBuilder *exe.CmdBuilder
  104. }
  105. type args struct {
  106. infos OriginInfoByURL
  107. }
  108. tests := []struct {
  109. name string
  110. fields fields
  111. args args
  112. want bool
  113. }{
  114. {
  115. name: "simple-has_update",
  116. args: args{infos: OriginInfoByURL{
  117. "github.com/Jguer/z.git": OriginInfo{
  118. Protocols: []string{"https"},
  119. Branch: "0",
  120. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  121. },
  122. }}, fields: fields{
  123. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  124. Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD"},
  125. }},
  126. },
  127. want: true,
  128. },
  129. {
  130. name: "double-has_update",
  131. args: args{infos: OriginInfoByURL{
  132. "github.com/Jguer/z.git": OriginInfo{
  133. Protocols: []string{"https"},
  134. Branch: "0",
  135. SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  136. },
  137. "github.com/Jguer/a.git": OriginInfo{
  138. Protocols: []string{"https"},
  139. Branch: "0",
  140. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  141. },
  142. }}, fields: fields{
  143. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  144. Returned: []string{
  145. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
  146. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
  147. },
  148. }},
  149. },
  150. want: true,
  151. },
  152. {
  153. name: "simple-no_update",
  154. args: args{infos: OriginInfoByURL{
  155. "github.com/Jguer/z.git": OriginInfo{
  156. Protocols: []string{"https"},
  157. Branch: "0",
  158. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  159. },
  160. }}, fields: fields{
  161. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  162. Returned: []string{"991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD"},
  163. }},
  164. },
  165. want: false,
  166. },
  167. {
  168. name: "simple-no_split",
  169. args: args{infos: OriginInfoByURL{
  170. "github.com/Jguer/z.git": OriginInfo{
  171. Protocols: []string{"https"},
  172. Branch: "0",
  173. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  174. },
  175. }}, fields: fields{
  176. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  177. Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
  178. }},
  179. },
  180. want: false,
  181. },
  182. {
  183. name: "simple-error",
  184. args: args{infos: OriginInfoByURL{
  185. "github.com/Jguer/z.git": OriginInfo{
  186. Protocols: []string{"https"},
  187. Branch: "0",
  188. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  189. },
  190. }}, fields: fields{
  191. CmdBuilder: &exe.CmdBuilder{
  192. GitBin: "git", GitFlags: []string{""},
  193. Runner: &MockRunner{
  194. Returned: []string{"error"},
  195. },
  196. },
  197. },
  198. want: false,
  199. },
  200. {
  201. name: "simple-no protocol",
  202. args: args{infos: OriginInfoByURL{
  203. "github.com/Jguer/z.git": OriginInfo{
  204. Protocols: []string{},
  205. Branch: "0",
  206. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  207. },
  208. }}, fields: fields{
  209. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  210. Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
  211. }},
  212. },
  213. want: false,
  214. },
  215. }
  216. for _, tt := range tests {
  217. tt := tt
  218. t.Run(tt.name, func(t *testing.T) {
  219. t.Parallel()
  220. v := &InfoStore{
  221. logger: text.GlobalLogger,
  222. CmdBuilder: tt.fields.CmdBuilder,
  223. OriginsByPackage: map[string]OriginInfoByURL{
  224. "yay": tt.args.infos,
  225. },
  226. }
  227. got := v.ToUpgrade(context.Background(), "yay")
  228. assert.Equal(t, tt.want, got)
  229. })
  230. }
  231. }
  232. func TestInfoStore_NeedsUpdate(t *testing.T) {
  233. t.Parallel()
  234. type fields struct {
  235. CmdBuilder *exe.CmdBuilder
  236. }
  237. type args struct {
  238. infos OriginInfoByURL
  239. }
  240. tests := []struct {
  241. name string
  242. fields fields
  243. args args
  244. want bool
  245. }{
  246. {
  247. name: "simple-has_update",
  248. args: args{infos: OriginInfoByURL{
  249. "github.com/Jguer/z.git": OriginInfo{
  250. Protocols: []string{"https"},
  251. Branch: "0",
  252. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  253. },
  254. }}, fields: fields{
  255. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  256. Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD"},
  257. }},
  258. },
  259. want: true,
  260. },
  261. {
  262. name: "double-has_update",
  263. args: args{infos: OriginInfoByURL{
  264. "github.com/Jguer/z.git": OriginInfo{
  265. Protocols: []string{"https"},
  266. Branch: "0",
  267. SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  268. },
  269. "github.com/Jguer/a.git": OriginInfo{
  270. Protocols: []string{"https"},
  271. Branch: "0",
  272. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  273. },
  274. }}, fields: fields{
  275. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  276. Returned: []string{
  277. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
  278. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
  279. },
  280. }},
  281. },
  282. want: true,
  283. },
  284. {
  285. name: "simple-no_update",
  286. args: args{infos: OriginInfoByURL{
  287. "github.com/Jguer/z.git": OriginInfo{
  288. Protocols: []string{"https"},
  289. Branch: "0",
  290. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  291. },
  292. }}, fields: fields{
  293. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  294. Returned: []string{"991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD"},
  295. }},
  296. },
  297. want: false,
  298. },
  299. {
  300. name: "simple-no_split",
  301. args: args{infos: OriginInfoByURL{
  302. "github.com/Jguer/z.git": OriginInfo{
  303. Protocols: []string{"https"},
  304. Branch: "0",
  305. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  306. },
  307. }}, fields: fields{
  308. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  309. Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
  310. }},
  311. },
  312. want: false,
  313. },
  314. {
  315. name: "simple-error",
  316. args: args{infos: OriginInfoByURL{
  317. "github.com/Jguer/z.git": OriginInfo{
  318. Protocols: []string{"https"},
  319. Branch: "0",
  320. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  321. },
  322. }}, fields: fields{
  323. CmdBuilder: &exe.CmdBuilder{
  324. GitBin: "git", GitFlags: []string{""},
  325. Runner: &MockRunner{
  326. Returned: []string{"error"},
  327. },
  328. },
  329. },
  330. want: false,
  331. },
  332. {
  333. name: "simple-no protocol",
  334. args: args{infos: OriginInfoByURL{
  335. "github.com/Jguer/z.git": OriginInfo{
  336. Protocols: []string{},
  337. Branch: "0",
  338. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  339. },
  340. }}, fields: fields{
  341. CmdBuilder: &exe.CmdBuilder{GitBin: "git", GitFlags: []string{""}, Runner: &MockRunner{
  342. Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
  343. }},
  344. },
  345. want: false,
  346. },
  347. }
  348. for _, tt := range tests {
  349. tt := tt
  350. t.Run(tt.name, func(t *testing.T) {
  351. t.Parallel()
  352. v := &InfoStore{
  353. logger: text.GlobalLogger,
  354. CmdBuilder: tt.fields.CmdBuilder,
  355. }
  356. got := v.needsUpdate(context.Background(), tt.args.infos)
  357. assert.Equal(t, tt.want, got)
  358. })
  359. }
  360. }
  361. func TestInfoStore_Update(t *testing.T) {
  362. t.Parallel()
  363. type fields struct {
  364. OriginsByPackage map[string]OriginInfoByURL
  365. CmdBuilder *exe.CmdBuilder
  366. }
  367. type args struct {
  368. pkgName string
  369. sources []gosrc.ArchString
  370. }
  371. tests := []struct {
  372. name string
  373. fields fields
  374. args args
  375. }{
  376. {
  377. name: "simple",
  378. args: args{
  379. pkgName: "hello",
  380. sources: []gosrc.ArchString{{Value: "git://github.com/jguer/yay.git#branch=master"}},
  381. },
  382. fields: fields{
  383. OriginsByPackage: make(map[string]OriginInfoByURL),
  384. CmdBuilder: &exe.CmdBuilder{
  385. GitBin: "git", GitFlags: []string{""},
  386. Runner: &MockRunner{Returned: []string{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD"}},
  387. },
  388. },
  389. },
  390. }
  391. file, err := os.CreateTemp("/tmp", "yay-infostore-*-test")
  392. filePath := file.Name()
  393. require.NoError(t, err)
  394. for _, tt := range tests {
  395. tt := tt
  396. t.Run(tt.name, func(t *testing.T) {
  397. t.Parallel()
  398. v := &InfoStore{
  399. OriginsByPackage: tt.fields.OriginsByPackage,
  400. logger: text.GlobalLogger,
  401. FilePath: filePath,
  402. CmdBuilder: tt.fields.CmdBuilder,
  403. }
  404. v.Update(context.Background(), tt.args.pkgName, tt.args.sources)
  405. assert.Len(t, tt.fields.OriginsByPackage, 1)
  406. marshalledinfo, err := json.MarshalIndent(tt.fields.OriginsByPackage, "", "\t")
  407. assert.NoError(t, err)
  408. cupaloy.SnapshotT(t, marshalledinfo)
  409. v.Load()
  410. fmt.Println(v.OriginsByPackage)
  411. assert.Len(t, tt.fields.OriginsByPackage, 1)
  412. marshalledinfo, err = json.MarshalIndent(tt.fields.OriginsByPackage, "", "\t")
  413. assert.NoError(t, err)
  414. cupaloy.SnapshotT(t, marshalledinfo)
  415. })
  416. }
  417. require.NoError(t, os.Remove(filePath))
  418. }
  419. func TestInfoStore_Remove(t *testing.T) {
  420. t.Parallel()
  421. type fields struct {
  422. OriginsByPackage map[string]OriginInfoByURL
  423. }
  424. type args struct {
  425. pkgs []string
  426. }
  427. tests := []struct {
  428. name string
  429. fields fields
  430. args args
  431. }{
  432. {
  433. name: "simple",
  434. args: args{pkgs: []string{"a", "c"}},
  435. fields: fields{
  436. OriginsByPackage: map[string]OriginInfoByURL{
  437. "a": {},
  438. "b": {},
  439. "c": {},
  440. "d": {},
  441. },
  442. },
  443. },
  444. }
  445. file, err := os.CreateTemp("/tmp", "yay-vcs-*-test")
  446. filePath := file.Name()
  447. require.NoError(t, err)
  448. for _, tt := range tests {
  449. tt := tt
  450. t.Run(tt.name, func(t *testing.T) {
  451. t.Parallel()
  452. v := &InfoStore{
  453. OriginsByPackage: tt.fields.OriginsByPackage,
  454. logger: text.GlobalLogger,
  455. FilePath: filePath,
  456. }
  457. v.RemovePackages(tt.args.pkgs)
  458. assert.Len(t, tt.fields.OriginsByPackage, 2)
  459. })
  460. }
  461. require.NoError(t, os.Remove(filePath))
  462. }
  463. func TestInfoStore_CleanOrphans(t *testing.T) {
  464. t.Parallel()
  465. type fields struct {
  466. OriginsByPackage map[string]OriginInfoByURL
  467. }
  468. type args struct {
  469. pkgs map[string]db.IPackage
  470. }
  471. tests := []struct {
  472. name string
  473. fields fields
  474. args args
  475. }{
  476. {
  477. name: "simple",
  478. args: args{pkgs: map[string]db.IPackage{"a": nil, "b": nil, "d": nil}},
  479. fields: fields{
  480. OriginsByPackage: map[string]OriginInfoByURL{
  481. "a": {},
  482. "b": {},
  483. "c": {},
  484. "d": {},
  485. },
  486. },
  487. },
  488. }
  489. file, err := os.CreateTemp("/tmp", "yay-vcs-*-test")
  490. filePath := file.Name()
  491. require.NoError(t, err)
  492. for _, tt := range tests {
  493. tt := tt
  494. t.Run(tt.name, func(t *testing.T) {
  495. t.Parallel()
  496. v := &InfoStore{
  497. OriginsByPackage: tt.fields.OriginsByPackage,
  498. FilePath: filePath,
  499. logger: text.NewLogger(io.Discard, os.Stderr, strings.NewReader(""), false, "test"),
  500. }
  501. v.CleanOrphans(tt.args.pkgs)
  502. assert.Len(t, tt.fields.OriginsByPackage, 3)
  503. })
  504. }
  505. require.NoError(t, os.Remove(filePath))
  506. }