You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

formatimports_test.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package codeformat
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestFormatImportsSimple(t *testing.T) {
  9. formatted, err := formatGoImports([]byte(`
  10. package codeformat
  11. import (
  12. "github.com/stretchr/testify/assert"
  13. "testing"
  14. )
  15. `))
  16. expected := `
  17. package codeformat
  18. import (
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. `
  23. assert.NoError(t, err)
  24. assert.Equal(t, expected, string(formatted))
  25. }
  26. func TestFormatImportsGroup(t *testing.T) {
  27. // gofmt/goimports won't group the packages, for example, they produce such code:
  28. // "bytes"
  29. // "image"
  30. // (a blank line)
  31. // "fmt"
  32. // "image/color/palette"
  33. // our formatter does better, and these packages are grouped into one.
  34. formatted, err := formatGoImports([]byte(`
  35. package test
  36. import (
  37. "bytes"
  38. "fmt"
  39. "image"
  40. "image/color"
  41. _ "image/gif" // for processing gif images
  42. _ "image/jpeg" // for processing jpeg images
  43. _ "image/png" // for processing png images
  44. "code.gitea.io/other/package"
  45. "code.gitea.io/gitea/modules/setting"
  46. "code.gitea.io/gitea/modules/util"
  47. "xorm.io/the/package"
  48. "github.com/issue9/identicon"
  49. "github.com/nfnt/resize"
  50. "github.com/oliamb/cutter"
  51. )
  52. `))
  53. expected := `
  54. package test
  55. import (
  56. "bytes"
  57. "fmt"
  58. "image"
  59. "image/color"
  60. _ "image/gif" // for processing gif images
  61. _ "image/jpeg" // for processing jpeg images
  62. _ "image/png" // for processing png images
  63. "code.gitea.io/gitea/modules/setting"
  64. "code.gitea.io/gitea/modules/util"
  65. "code.gitea.io/other/package"
  66. "github.com/issue9/identicon"
  67. "github.com/nfnt/resize"
  68. "github.com/oliamb/cutter"
  69. "xorm.io/the/package"
  70. )
  71. `
  72. assert.NoError(t, err)
  73. assert.Equal(t, expected, string(formatted))
  74. }
  75. func TestFormatImportsInvalidComment(t *testing.T) {
  76. // why we shouldn't write comments between imports: it breaks the grouping of imports
  77. // for example:
  78. // "pkg1"
  79. // "pkg2"
  80. // // a comment
  81. // "pkgA"
  82. // "pkgB"
  83. // the comment splits the packages into two groups, pkg1/2 are sorted separately, pkgA/B are sorted separately
  84. // we don't want such code, so the code should be:
  85. // "pkg1"
  86. // "pkg2"
  87. // "pkgA" // a comment
  88. // "pkgB"
  89. _, err := formatGoImports([]byte(`
  90. package test
  91. import (
  92. "image/jpeg"
  93. // for processing gif images
  94. "image/gif"
  95. )
  96. `))
  97. assert.ErrorIs(t, err, errInvalidCommentBetweenImports)
  98. }