| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| package bazel2gn_test | |
| import ( | |
| "strings" | |
| "testing" | |
| "github.com/google/go-cmp/cmp" | |
| "go.fuchsia.dev/fuchsia/build/tools/bazel2gn" | |
| ) | |
| func TestWriteFileToGN(t *testing.T) { | |
| for _, tc := range []struct { | |
| name string | |
| bazel string | |
| wantGN string | |
| }{ | |
| { | |
| name: "single line from a variable, used by a resource", | |
| bazel: `_SHEBANG = "#!resolve fuchsia-pkg://fuchsia.com/foo" | |
| write_file( | |
| name = "script", | |
| out = "test_bins/script", | |
| content = [_SHEBANG + "#bin/tool"], | |
| ) | |
| resource( | |
| name = "script-bin", | |
| src = ":script", | |
| dest = "bin/script", | |
| )`, | |
| wantGN: `_SHEBANG = "#!resolve fuchsia-pkg://fuchsia.com/foo" | |
| # To avoid "Assignment had no effect" from GN. | |
| # It's possible this variable is only used in if conditions (e.g. is_host). | |
| not_needed([ "_SHEBANG" ]) | |
| generated_file("script") { | |
| outputs = [ "$target_gen_dir/test_bins/script" ] | |
| contents = _SHEBANG + "#bin/tool" | |
| } | |
| resource("script-bin") { | |
| sources = [ | |
| "$target_gen_dir/test_bins/script", | |
| ] | |
| outputs = [ | |
| "bin/script", | |
| ] | |
| deps = [ | |
| ":script", | |
| ] | |
| }`, | |
| }, | |
| { | |
| // Skylib joins lines with newlines, without a trailing one. Bazel | |
| // doesn't expand "$", so it is escaped for GN. | |
| name: "several lines and all supported attributes", | |
| bazel: `write_file( | |
| name = "config", | |
| testonly = True, | |
| out = "config.txt", | |
| content = [ | |
| "a = $HOME", | |
| "b", | |
| ], | |
| newline = "unix", | |
| tags = ["manual"], | |
| visibility = ["//src/foo:__pkg__"], | |
| )`, | |
| wantGN: `generated_file("config") { | |
| testonly = true | |
| outputs = [ "$target_gen_dir/config.txt" ] | |
| contents = string_join("$0x0A", [ "a = \$HOME", "b" ]) | |
| visibility = [ | |
| ":*", | |
| "//src/foo:*", | |
| ] | |
| }`, | |
| }, | |
| { | |
| name: "no content", | |
| bazel: `write_file( | |
| name = "empty", | |
| out = "empty.txt", | |
| )`, | |
| wantGN: `generated_file("empty") { | |
| outputs = [ "$target_gen_dir/empty.txt" ] | |
| contents = "" | |
| }`, | |
| }, | |
| { | |
| name: "resource referencing write_file by output path", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "test_bins/script", | |
| content = ["#!/bin/sh"], | |
| ) | |
| resource( | |
| name = "script-bin-bare", | |
| src = "test_bins/script", | |
| dest = "bin/script-bare", | |
| ) | |
| resource( | |
| name = "script-bin-colon", | |
| src = ":test_bins/script", | |
| dest = "bin/script-colon", | |
| )`, | |
| wantGN: `generated_file("script") { | |
| outputs = [ "$target_gen_dir/test_bins/script" ] | |
| contents = "#!/bin/sh" | |
| } | |
| resource("script-bin-bare") { | |
| sources = [ | |
| "$target_gen_dir/test_bins/script", | |
| ] | |
| outputs = [ | |
| "bin/script-bare", | |
| ] | |
| deps = [ | |
| ":script", | |
| ] | |
| } | |
| resource("script-bin-colon") { | |
| sources = [ | |
| "$target_gen_dir/test_bins/script", | |
| ] | |
| outputs = [ | |
| "bin/script-colon", | |
| ] | |
| deps = [ | |
| ":script", | |
| ] | |
| }`, | |
| }, | |
| { | |
| name: "target_compatible_with", | |
| bazel: `write_file( | |
| name = "host_script", | |
| out = "host_script", | |
| content = ["ok"], | |
| target_compatible_with = HOST_OS_CONSTRAINTS, | |
| )`, | |
| wantGN: `if (is_host) { | |
| generated_file("host_script") { | |
| outputs = [ "$target_gen_dir/host_script" ] | |
| contents = "ok" | |
| } | |
| }`, | |
| }, | |
| { | |
| name: "skipped write_file", | |
| bazel: `# The GN copy is written by hand. | |
| # @bazel2gn:skip | |
| write_file( | |
| name = "script", | |
| out = "script", | |
| is_executable = True, | |
| )`, | |
| wantGN: ``, | |
| }, | |
| } { | |
| t.Run(tc.name, func(t *testing.T) { | |
| f := toSyntaxFile(t, tc.bazel) | |
| lines, err := bazel2gn.FileToGN(f) | |
| if err != nil { | |
| t.Fatalf("FileToGN() failed: %v", err) | |
| } | |
| if diff := cmp.Diff(tc.wantGN, strings.Join(lines, "\n")); diff != "" { | |
| t.Errorf("FileToGN() diff (-want +got):\n%s", diff) | |
| } | |
| }) | |
| } | |
| } | |
| func TestWriteFileToGNErrors(t *testing.T) { | |
| for _, tc := range []struct { | |
| name string | |
| bazel string | |
| wantErr string | |
| }{ | |
| { | |
| name: "missing out", | |
| bazel: `write_file( | |
| name = "script", | |
| content = ["x"], | |
| )`, | |
| wantErr: "missing `out` attribute in write_file", | |
| }, | |
| { | |
| name: "out outside the package", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "../script", | |
| )`, | |
| wantErr: "must be a path in the package", | |
| }, | |
| { | |
| name: "out is parent directory", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "..", | |
| )`, | |
| wantErr: "must be a path in the package", | |
| }, | |
| { | |
| name: "undefined or imported variable in content", | |
| bazel: `load("//foo:defs.bzl", "FROM_LOAD") | |
| write_file( | |
| name = "script", | |
| out = "script", | |
| content = [FROM_LOAD], | |
| )`, | |
| wantErr: `undefined or imported variable "FROM_LOAD"`, | |
| }, | |
| { | |
| name: "variable with dollar sign in content", | |
| bazel: `_VAL = "$HOME" | |
| write_file( | |
| name = "script", | |
| out = "script", | |
| content = [_VAL], | |
| )`, | |
| wantErr: "must not contain '$' or newlines", | |
| }, | |
| { | |
| name: "variable with newline in content", | |
| bazel: `_VAL = "a\nb" | |
| write_file( | |
| name = "script", | |
| out = "script", | |
| content = [_VAL], | |
| )`, | |
| wantErr: "must not contain '$' or newlines", | |
| }, | |
| { | |
| name: "variable with single-quoted string in content", | |
| bazel: `_VAL = 'hello' | |
| write_file( | |
| name = "script", | |
| out = "script", | |
| content = [_VAL], | |
| )`, | |
| wantErr: "must use double-quoted single-line syntax", | |
| }, | |
| { | |
| name: "unsupported attribute", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "script", | |
| is_executable = True, | |
| )`, | |
| wantErr: `attribute "is_executable" of write_file`, | |
| }, | |
| { | |
| name: "windows newlines", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "script", | |
| newline = "windows", | |
| )`, | |
| wantErr: `newline must be "auto" or "unix"`, | |
| }, | |
| { | |
| name: "content is not a list literal", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "script", | |
| content = LINES, | |
| )`, | |
| wantErr: "write_file content: expected list literal", | |
| }, | |
| { | |
| name: "content line is a function call", | |
| bazel: `write_file( | |
| name = "script", | |
| out = "script", | |
| content = [str(1)], | |
| )`, | |
| wantErr: "write_file content: unsupported expression", | |
| }, | |
| } { | |
| t.Run(tc.name, func(t *testing.T) { | |
| f := toSyntaxFile(t, tc.bazel) | |
| _, err := bazel2gn.FileToGN(f) | |
| if err == nil { | |
| t.Fatalf("FileToGN() succeeded, want error containing %q", tc.wantErr) | |
| } | |
| if !strings.Contains(err.Error(), tc.wantErr) { | |
| t.Errorf("FileToGN() error = %q, want substring %q", err.Error(), tc.wantErr) | |
| } | |
| }) | |
| } | |
| } |