cabal-buck2-0.6.0.0: buck2/prelude/go/tools/gobuckify/lib/render_test.go
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
package gobuckifylib
import (
"strings"
"testing"
"text/template"
)
func TestTargetTemplate(t *testing.T) {
tests := []struct {
name string
config BuckConfig
target *BuckTarget
expectedOutput string
}{
{
name: "binary target with common deps and no target label prefix",
config: BuckConfig{
Preambule: "# Generated by gobuckify\n",
LoadGoBinaryRule: "load(\":defs.bzl\", \"go_binary\")\n",
GoBinaryRule: "go_binary",
DepsAttr: "go_external_deps",
GenerateEmbedSrcs: true,
},
target: &BuckTarget{
Name: "test_binary",
ImportPath: "github.com/example/test_binary",
IsBinary: true,
EmbedFiles: *NewFromList([]string{"embed1.txt", "embed2.txt"}),
CommonDeps: []string{"github.com/example/dep1", "github.com/example/dep2"},
},
expectedOutput: `
# Generated by gobuckify
load(":defs.bzl", "go_binary")
go_binary(
name = "test_binary",
package_name = "github.com/example/test_binary",
srcs = native.glob(["*.go", "*.s", "*.h", "*.c", "*.cc", "*.cpp", "*.S"]),
header_namespace = "",
go_external_deps = [
"github.com/example/dep1",
"github.com/example/dep2",
],
embed_srcs = [
"embed1.txt",
"embed2.txt",
],
visibility = ["PUBLIC"],
)`,
},
{
name: "library target with platform and cgo deps",
config: BuckConfig{
Preambule: "# Generated by gobuckify\n",
LoadGoLibraryRule: "load(\":defs.bzl\", \"go_library\")\n",
GoLibraryRule: "go_library",
DepsAttr: "deps",
DepsTargetLabelPrefix: "//third-party/go/",
},
target: &BuckTarget{
Name: "test_lib",
ImportPath: "github.com/example/test_lib",
CommonDeps: []string{"github.com/example/common_dep"},
PlatformDeps: map[string]*OSDeps{
"linux": {
OS: "linux",
ArchDeps: map[string]*ArchDeps{
"x86_64": {
Arch: "x86_64",
CgoDeps: map[string]*StringSet{
"prelude//go/constraints:cgo_enabled[false]": NewFromList([]string{
"github.com/example/nocgo_dep",
}),
"prelude//go/constraints:cgo_enabled[true]": NewFromList([]string{
"github.com/example/cgo_dep",
}),
},
},
},
},
},
},
expectedOutput: `
# Generated by gobuckify
load(":defs.bzl", "go_library")
go_library(
name = "test_lib",
package_name = "github.com/example/test_lib",
srcs = native.glob(["*.go", "*.s", "*.h", "*.c", "*.cc", "*.cpp", "*.S"]),
header_namespace = "",
deps = [
"//third-party/go/github.com/example/common_dep:common_dep",
] + select({
"DEFAULT": [],
"linux": select({
"DEFAULT": [],
"x86_64": select({
"DEFAULT": [],
"prelude//go/constraints:cgo_enabled[false]": [
"//third-party/go/github.com/example/nocgo_dep:nocgo_dep",
],
"prelude//go/constraints:cgo_enabled[true]": [
"//third-party/go/github.com/example/cgo_dep:cgo_dep",
],
}),
}),
}),
visibility = ["PUBLIC"],
)`,
},
{
name: "library target with no deps",
config: BuckConfig{
Preambule: "# Generated by gobuckify\n",
LoadGoLibraryRule: "load(\":defs.bzl\", \"go_library\")\n",
GoLibraryRule: "go_library",
},
target: &BuckTarget{
Name: "simple_lib",
ImportPath: "github.com/example/simple_lib",
},
expectedOutput: `
# Generated by gobuckify
load(":defs.bzl", "go_library")
go_library(
name = "simple_lib",
package_name = "github.com/example/simple_lib",
srcs = native.glob(["*.go", "*.s", "*.h", "*.c", "*.cc", "*.cpp", "*.S"]),
header_namespace = "",
visibility = ["PUBLIC"],
)`,
},
{
name: "library target with multiple platforms and cgo compatibility",
config: BuckConfig{
Preambule: "# Generated by gobuckify\n",
LoadGoLibraryRule: "load(\":defs.bzl\", \"go_library\")\n",
GoLibraryRule: "go_library",
DepsAttr: "deps",
DepsTargetLabelPrefix: "//third-party/go/",
},
target: &BuckTarget{
Name: "platform_lib",
ImportPath: "github.com/example/platform_lib",
PlatformDeps: map[string]*OSDeps{
"linux": {
OS: "linux",
ArchDeps: map[string]*ArchDeps{
"x86_64": {
Arch: "x86_64",
CommonDeps: NewFromList([]string{"github.com/example/linux_x86_dep"}),
},
"arm64": {
Arch: "arm64",
CgoDeps: map[string]*StringSet{
"prelude//go/constraints:cgo_enabled[true]": NewFromList([]string{
"github.com/example/linux_arm_dep",
}),
},
},
},
},
"darwin": {
OS: "darwin",
ArchDeps: map[string]*ArchDeps{
"x86_64": {
Arch: "x86_64",
CgoDeps: map[string]*StringSet{
"prelude//go/constraints:cgo_enabled[true]": NewFromList([]string{
"github.com/example/darwin_dep",
}),
},
},
},
},
},
TargetCompatibleWith: map[string]map[string]*StringSet{
"linux": {
"x86_64": NewFromList([]string{"prelude//go/constraints:cgo_enabled[true]"}),
"arm64": NewFromList([]string{"prelude//go/constraints:cgo_enabled[true]"}),
},
"darwin": {
"x86_64": NewFromList([]string{"prelude//go/constraints:cgo_enabled[true]"}),
},
},
},
expectedOutput: `
# Generated by gobuckify
load(":defs.bzl", "go_library")
go_library(
name = "platform_lib",
package_name = "github.com/example/platform_lib",
srcs = native.glob(["*.go", "*.s", "*.h", "*.c", "*.cc", "*.cpp", "*.S"]),
header_namespace = "",
deps = [
] + select({
"DEFAULT": [],
"darwin": select({
"DEFAULT": [],
"x86_64": select({
"DEFAULT": [],
"prelude//go/constraints:cgo_enabled[true]": [
"//third-party/go/github.com/example/darwin_dep:darwin_dep",
],
}),
}),
"linux": select({
"DEFAULT": [],
"arm64": select({
"DEFAULT": [],
"prelude//go/constraints:cgo_enabled[true]": [
"//third-party/go/github.com/example/linux_arm_dep:linux_arm_dep",
],
}),
"x86_64": [
"//third-party/go/github.com/example/linux_x86_dep:linux_x86_dep",
],
}),
}),
target_compatible_with = select({
"DEFAULT": ["config//:none"],
"darwin": select({
"DEFAULT": ["config//:none"],
"x86_64": select({
"DEFAULT": ["config//:none"],
"prelude//go/constraints:cgo_enabled[true]": [],
}),
}),
"linux": select({
"DEFAULT": ["config//:none"],
"arm64": select({
"DEFAULT": ["config//:none"],
"prelude//go/constraints:cgo_enabled[true]": [],
}),
"x86_64": select({
"DEFAULT": ["config//:none"],
"prelude//go/constraints:cgo_enabled[true]": [],
}),
}),
}),
visibility = ["PUBLIC"],
)`,
},
}
tmpl := template.Must(template.New("test").Funcs(template.FuncMap{
"TargetLabelFromImportPath": TargetLabelFromImportPath,
}).Parse(TargetTemplate))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf strings.Builder
err := tmpl.Execute(&buf, TemplateData{
Config: tt.config,
Target: tt.target,
})
if err != nil {
t.Fatalf("Template execution failed: %v", err)
}
if got, want := strings.TrimSpace(buf.String()), strings.TrimSpace(tt.expectedOutput); got != want {
t.Errorf("Template output mismatch.\nExpected:\n%s\n\nGot:\n%s", want, got)
}
})
}
}