bazel: enable bzlmod

This change enables bzlmod without migrating any dependencies.
Instead, WORKSPACE dependencies can be migrated one by one.
This commit is contained in:
Malte Poll 2024-05-22 13:25:35 +02:00
parent d0bab9eb08
commit 8796edc9ad
10 changed files with 33 additions and 33 deletions

View file

@ -53,12 +53,12 @@ func (h *Helper) FindFiles() ([]BazelFile, error) {
return append(bzlFiles, workspaceFile), nil
}
// findWorkspaceFile returns the path to the Bazel WORKSPACE.bazel file (or WORKSPACE if the former doesn't exist).
// findWorkspaceFile returns the path to the Bazel WORKSPACE.bzlmod file (or WORKSPACE if the former doesn't exist).
func (h *Helper) findWorkspaceFile() (BazelFile, error) {
if _, err := h.fs.Stat("WORKSPACE.bazel"); err == nil {
if _, err := h.fs.Stat("WORKSPACE.bzlmod"); err == nil {
return BazelFile{
RelPath: "WORKSPACE.bazel",
AbsPath: filepath.Join(h.workspaceRoot, "WORKSPACE.bazel"),
RelPath: "WORKSPACE.bzlmod",
AbsPath: filepath.Join(h.workspaceRoot, "WORKSPACE.bzlmod"),
Type: BazelFileTypeWorkspace,
}, nil
}
@ -151,7 +151,7 @@ type BazelFileType int
const (
BazelFileTypeBzl = iota // BazelFileTypeBzl is a .bzl file
BazelFileTypeWorkspace // BazelFileTypeWorkspace is a WORKSPACE or WORKSPACE.bazel file
BazelFileTypeWorkspace // BazelFileTypeWorkspace is a WORKSPACE or WORKSPACE.bzlmod file
)
// LookupEnv can be the real os.LookupEnv or a mock for testing.

View file

@ -42,22 +42,22 @@ func TestFindFiles(t *testing.T) {
},
},
},
"only WORKSPACE.bazel file": {
files: []string{"WORKSPACE.bazel"},
"only WORKSPACE.bzlmod file": {
files: []string{"WORKSPACE.bzlmod"},
wantFiles: []BazelFile{
{
RelPath: "WORKSPACE.bazel",
AbsPath: "/WORKSPACE.bazel",
RelPath: "WORKSPACE.bzlmod",
AbsPath: "/WORKSPACE.bzlmod",
Type: BazelFileTypeWorkspace,
},
},
},
"both WORKSPACE and WORKSPACE.bazel files": {
files: []string{"WORKSPACE", "WORKSPACE.bazel"},
"both WORKSPACE and WORKSPACE.bzlmod files": {
files: []string{"WORKSPACE", "WORKSPACE.bzlmod"},
wantFiles: []BazelFile{
{
RelPath: "WORKSPACE.bazel",
AbsPath: "/WORKSPACE.bazel",
RelPath: "WORKSPACE.bzlmod",
AbsPath: "/WORKSPACE.bzlmod",
Type: BazelFileTypeWorkspace,
},
},
@ -67,11 +67,11 @@ func TestFindFiles(t *testing.T) {
wantErr: true,
},
"all kinds": {
files: []string{"WORKSPACE", "WORKSPACE.bazel", "foo.bzl", "bar.bzl", "unused.txt", "folder/baz.bzl"},
files: []string{"WORKSPACE", "WORKSPACE.bzlmod", "foo.bzl", "bar.bzl", "unused.txt", "folder/baz.bzl"},
wantFiles: []BazelFile{
{
RelPath: "WORKSPACE.bazel",
AbsPath: "/WORKSPACE.bazel",
RelPath: "WORKSPACE.bzlmod",
AbsPath: "/WORKSPACE.bzlmod",
Type: BazelFileTypeWorkspace,
},
{
@ -216,15 +216,15 @@ func TestDiff(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
fs := afero.NewMemMapFs()
err := afero.WriteFile(fs, "WORKSPACE.bazel", []byte(""), 0o644)
err := afero.WriteFile(fs, "WORKSPACE.bzlmod", []byte(""), 0o644)
require.NoError(err)
helper := Helper{
fs: fs,
workspaceRoot: "/",
}
fileRef := BazelFile{
RelPath: "WORKSPACE.bazel",
AbsPath: "/WORKSPACE.bazel",
RelPath: "WORKSPACE.bzlmod",
AbsPath: "/WORKSPACE.bzlmod",
Type: BazelFileTypeWorkspace,
}
bf, err := helper.LoadFile(fileRef)
@ -247,10 +247,10 @@ func TestDiff(t *testing.T) {
)
diff, err = helper.Diff(fileRef, bf)
require.NoError(err)
assert.Equal("--- a/WORKSPACE.bazel\n+++ b/WORKSPACE.bazel\n@@ -1 +1 @@\n+workspace(name = \"foo\")\n", diff)
assert.Equal("--- a/WORKSPACE.bzlmod\n+++ b/WORKSPACE.bzlmod\n@@ -1 +1 @@\n+workspace(name = \"foo\")\n", diff)
err = helper.WriteFile(fileRef, bf)
require.NoError(err)
contents, err := afero.ReadFile(fs, "WORKSPACE.bazel")
contents, err := afero.ReadFile(fs, "WORKSPACE.bzlmod")
assert.NoError(err)
assert.Equal("workspace(name = \"foo\")\n", string(contents))
diff, err = helper.Diff(fileRef, bf)