github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/tests/bzlmod/go_mod_test.bzl (about)

     1  load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
     2  load("//internal/bzlmod:go_mod.bzl", "parse_go_mod", "parse_go_sum", "parse_go_work", "use_spec_to_label")
     3  
     4  _GO_MOD_CONTENT = """ go 1.18
     5  
     6  require (
     7    github.com/bazelbuild/buildtools v0.0.0-20220531122519-a43aed7014c8
     8  	github.com/bazelbuild/rules_go "v0.\\n\\\\\\"33.0"
     9  github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect
    10  	// some comment
    11  	`golang.org/x/tools` "v0.1.11" // foobar
    12  	github.com/go-fsnotify/fsnotify v1.5.4
    13  )
    14  
    15  replace github.com/go-fsnotify/fsnotify => github.com/fsnotify/fsnotify v1.4.2
    16  replace github.com/bmatcuk/doublestar/v4 v4.0.2 => github.com/bmatcuk/doublestar/v4 v4.0.3
    17  replace example.org/hello => ../fixtures/hello
    18  
    19  module github.com/bazelbuild/bazel-gazelle
    20  
    21  	exclude    (
    22  	github.com/bazelbuild/rules_go v0.33.0
    23    )
    24  
    25    retract v1.0.0
    26  
    27  require golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
    28  """
    29  
    30  _EXPECTED_GO_MOD_PARSE_RESULT = struct(
    31      go = (1, 18),
    32      module = "github.com/bazelbuild/bazel-gazelle",
    33      replace_map = {
    34          "github.com/go-fsnotify/fsnotify": struct(local_path = None, from_version = None, to_path = "github.com/fsnotify/fsnotify", version = "1.4.2"),
    35          "github.com/bmatcuk/doublestar/v4": struct(local_path = None, from_version = "4.0.2", to_path = "github.com/bmatcuk/doublestar/v4", version = "4.0.3"),
    36          "example.org/hello": struct(local_path = "../fixtures/hello", from_version = None, to_path = "example.org/hello", version = "{"),
    37      },
    38      require = (
    39          struct(indirect = False, path = "github.com/bazelbuild/buildtools", version = "v0.0.0-20220531122519-a43aed7014c8"),
    40          struct(indirect = False, path = "github.com/bazelbuild/rules_go", version = "v0.n\\\"33.0"),
    41          struct(indirect = True, path = "github.com/bmatcuk/doublestar/v4", version = "v4.0.2"),
    42          struct(indirect = False, path = "golang.org/x/tools", version = "v0.1.11"),
    43          struct(indirect = False, path = "github.com/go-fsnotify/fsnotify", version = "v1.5.4"),
    44          struct(indirect = True, path = "golang.org/x/sys", version = "v0.0.0-20220624220833-87e55d714810"),
    45      ),
    46  )
    47  
    48  def _go_mod_test_impl(ctx):
    49      env = unittest.begin(ctx)
    50      asserts.equals(env, _EXPECTED_GO_MOD_PARSE_RESULT, parse_go_mod(_GO_MOD_CONTENT, "/go.mod"))
    51      return unittest.end(env)
    52  
    53  go_mod_test = unittest.make(_go_mod_test_impl)
    54  
    55  _GO_MOD_21_CONTENT = """go 1.21.0rc1
    56  
    57  module example.com
    58  
    59  toolchain go1.22.2
    60  """
    61  
    62  _EXPECTED_GO_MOD_21_PARSE_RESULT = struct(
    63      go = (1, 21),
    64      module = "example.com",
    65      replace_map = {},
    66      require = (),
    67  )
    68  
    69  def _use_spec_to_label_test_impl(ctx):
    70      env = unittest.begin(ctx)
    71  
    72      asserts.equals(env, Label("@@org_example//go_mod_one:go.mod"), use_spec_to_label("org_example", "./go_mod_one"))
    73      asserts.equals(env, Label("@@org_example//go_mod_one:go.mod"), use_spec_to_label("org_example", "./go_mod_one/"))
    74      asserts.equals(env, Label("@@org_example//bar/go_mod_one:go.mod"), use_spec_to_label("org_example", "./bar/go_mod_one"))
    75  
    76      return unittest.end(env)
    77  
    78  use_spec_test = unittest.make(_use_spec_to_label_test_impl)
    79  
    80  def _go_mod_21_test_impl(ctx):
    81      env = unittest.begin(ctx)
    82      asserts.equals(env, _EXPECTED_GO_MOD_21_PARSE_RESULT, parse_go_mod(_GO_MOD_21_CONTENT, "/go.mod"))
    83      return unittest.end(env)
    84  
    85  go_mod_21_test = unittest.make(_go_mod_21_test_impl)
    86  
    87  _GO_SUM_CONTENT = """cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
    88  github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
    89  github.com/bazelbuild/buildtools v0.0.0-20220531122519-a43aed7014c8 h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=
    90  github.com/bazelbuild/buildtools v0.0.0-20220531122519-a43aed7014c8/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo=
    91  github.com/bazelbuild/rules_go v0.33.0 h1:WW9CHmFxbE+Lm4qiLOFAPogmiAUzZtvQsWxUcm4wwaU=
    92  github.com/bazelbuild/rules_go v0.33.0/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
    93  """
    94  
    95  _EXPECTED_GO_SUM_PARSE_RESULT = {
    96      ("github.com/bazelbuild/buildtools", "0.0.0-20220531122519-a43aed7014c8"): "h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=",
    97      ("github.com/bazelbuild/rules_go", "0.33.0"): "h1:WW9CHmFxbE+Lm4qiLOFAPogmiAUzZtvQsWxUcm4wwaU=",
    98  }
    99  
   100  def _go_sum_test_impl(ctx):
   101      env = unittest.begin(ctx)
   102      asserts.equals(env, _EXPECTED_GO_SUM_PARSE_RESULT, parse_go_sum(_GO_SUM_CONTENT))
   103      return unittest.end(env)
   104  
   105  go_sum_test = unittest.make(_go_sum_test_impl)
   106  
   107  _GO_WORK_CONTENT = """go 1.18
   108  use ./go_mod_one
   109  use (
   110      ./foo/go_mod_two
   111      ./bar/baz/go_mod_three
   112  )
   113  
   114  replace github.com/go-fsnotify/fsnotify => github.com/fsnotify/fsnotify v1.4.2
   115  replace github.com/bmatcuk/doublestar/v4 v4.0.2 => github.com/bmatcuk/doublestar/v4 v4.0.3
   116  replace example.org/hello => ../fixtures/hello
   117  """
   118  
   119  _EXPECTED_GO_WORK_PARSE_RESULT = struct(
   120      go = (1, 18),
   121      from_file_tags = [
   122          struct(_is_dev_dependency = False, go_mod = Label("//go_mod_one:go.mod")),
   123          struct(_is_dev_dependency = False, go_mod = Label("//foo/go_mod_two:go.mod")),
   124          struct(_is_dev_dependency = False, go_mod = Label("//bar/baz/go_mod_three:go.mod")),
   125      ],
   126      module_tags = [
   127          struct(indirect = False, _parent_label = Label("//:go.work"), local_path = None, path = "github.com/fsnotify/fsnotify", version = "1.4.2"),
   128          struct(indirect = False, _parent_label = Label("//:go.work"), local_path = None, path = "github.com/bmatcuk/doublestar/v4", version = "4.0.3"),
   129          struct(indirect = False, _parent_label = Label("//:go.work"), local_path = "../fixtures/hello", path = "example.org/hello", version = "{"),
   130      ],
   131      replace_map = {
   132          "github.com/go-fsnotify/fsnotify": struct(local_path = None, from_version = None, to_path = "github.com/fsnotify/fsnotify", version = "1.4.2"),
   133          "github.com/bmatcuk/doublestar/v4": struct(local_path = None, from_version = "4.0.2", to_path = "github.com/bmatcuk/doublestar/v4", version = "4.0.3"),
   134          "example.org/hello": struct(local_path = "../fixtures/hello", from_version = None, to_path = "example.org/hello", version = "{"),
   135      },
   136      use = [
   137          "./go_mod_one",
   138          "./foo/go_mod_two",
   139          "./bar/baz/go_mod_three",
   140      ],
   141  )
   142  
   143  def _go_work_test_impl(ctx):
   144      env = unittest.begin(ctx)
   145      asserts.equals(env, _EXPECTED_GO_WORK_PARSE_RESULT, parse_go_work(_GO_WORK_CONTENT, Label("@@//:go.work")))
   146      return unittest.end(env)
   147  
   148  go_work_test = unittest.make(_go_work_test_impl)
   149  
   150  def go_mod_test_suite(name):
   151      unittest.suite(
   152          name,
   153          go_mod_test,
   154          go_mod_21_test,
   155          go_sum_test,
   156          go_work_test,
   157          use_spec_test,
   158      )